欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

cf1043E. Mysterious Crime(二分 前缀和)

程序员文章站 2022-08-12 09:05:58
题意 "题目链接" Sol 考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了。 看了看E发现好像很可做?? 又仔细看了几眼发现这不是sb题么。。。 先考虑两个人,假设贡献分别为$(x, y) (a, b)$ 有两种组合方式,一种是$x + b$,另一种是$y + a$ 若$x + b ......

题意

题目链接

sol

考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了。

看了看e发现好像很可做??

又仔细看了几眼发现这不是sb题么。。。

先考虑两个人,假设贡献分别为\((x, y) (a, b)\)

有两种组合方式,一种是\(x + b\),另一种是\(y + a\)

\(x + b >= y + a\)

那么\(x - y >= a - b\)

因此我们按照\(x - y\)排序,对于每个位置,肯定是某一个前缀全选\(x+b\),除此之外都是\(y+a\)

二分之后前缀和后缀和安排一下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define ll long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? eof : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *o = obuf;
//void print(int x) {if(x > 9) print(x / 10); *o++ = x % 10 + '0';}
//#define os  *o++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 6e5 + 10, inf = 1e9 + 10, mod = 998244353;
const int base = 137;
const double eps = 1e-9;
inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m, x[maxn], y[maxn], cha[maxn];
struct node {
    int x, y, val;
    bool operator < (const node &rhs) const {
        return val < rhs.val;
    }
}a[maxn];
int sx[maxn], sy[maxn], ans[maxn];
int calc(int x, int y, int a, int b) {
    return min(x + b, y + a);
}
 main() {
    n = read(); m = read();
    for(int i = 1; i <= n; i++) {
        x[i] = a[i].x = read(), y[i] = a[i].y = read();
        cha[i] = a[i].val = a[i].x - a[i].y;
    }   
    for(int i = 1; i <= m; i++) {
        int p = read(), q = read();
        int mn = calc(a[p].x, a[p].y, a[q].x, a[q].y);
        ans[p] -= mn; ans[q] -= mn;
    }
    sort(a + 1, a + n + 1);
    sort(cha + 1, cha + n + 1);
    for(int i = 1; i <= n; i++) sx[i] = sx[i - 1] + a[i].x;
    for(int i = n; i >= 1; i--) sy[i] = sy[i + 1] + a[i].y;
    for(int i = 1; i <= n; i++) {
        int pos = upper_bound(cha + 1, cha + n + 1, x[i] - y[i]) - cha - 1;
        ans[i] += y[i] * pos + sx[pos] + (n - pos) * x[i] + sy[pos + 1] - calc(x[i], y[i], x[i], y[i]);
    }
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
    return 0;
}