中文题意

给出一共n个人,后面n行给出这n个人做第A题的得分以及第B题的得分。并且每次比赛只能安排两个人上场做这两道题最后得分是对应人做对应题目得分和。现在如果选定1号选手,那么他一定要和其他个人参与次比赛,我们可以安排的也就是1号选手做第一题还是第二题的问题,现在要团队总和最低,问你选定1到n全部选手的答案。

上面是不考虑m的情况,现在题目要求有m对人他们不能组队,也就是不一定每个人都能参与场,遇到不能组队的跳过就行。

Solution

很显然,如果选定了一个选手一定要上场,那么我们可以直接暴力遍历其他队友,依次累加贡献即可。但是会超时。有没有什么办法可以快速的判断我们选定的人要参加几场A题,几场B题呢。继续往下看。
对于选定的号选手,如果这一轮和他一起上的是号选手。那么他们的贡献就是


那么我们看到这种类似相邻项不同数作加法的都可以类似的作变换假设,我们假设这个时候作A更优。那么式子变形成为。,再做一次移项就可以得到。 ,那么我们对原数组进行一次快速排序,就可以得到第个人要选几次A了。
具体做法是我们记录排序前的每个人在排序后的什么位置,这里我假设第个人在第号位置,那么说明他和前面个人组队时一定是做B题,和后面的组队时一定是做A题,那么答案还要加上前面个人做A的贡献,后面人做B的贡献,前后缀处理即可。

现在再考虑那m对不能组队的情况,也就是我们多算了一次贡献罢了,把算好的答案减掉这次不能组队的两个人产生的贡献就行了。

#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define all(__vv__) (__vv__).begin(), (__vv__).end()
#define endl "\n"
#define pai pair<int, int>
#define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__))
#define rep(i, sta, en) for(int i=sta; i<=en; ++i)
#define repp(i, sta, en) for(int i=sta; i>=en; --i)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op)    putchar(op); return; }    char F[40]; ll tmp = x > 0 ? x : -x;    if (x < 0)putchar('-');    int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';        tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]);    if (op)    putchar(op); }
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 7;

ll n, m;
struct Node {
    ll x, y;
    int id;
    bool operator < (const Node& opt) const {
        return x - y < opt.x - opt.y;
    }
}p[N];
ll pre[N], suf[N];
ll ans[N];
int pos[N];

void solve() {
    n = read(), m = read();
    rep(i, 1, n) {
        p[i].x = read(), p[i].y = read();
        p[i].id = i;
    }
    sort(p + 1, p + 1 + n);
    rep(i, 1, n)    pos[p[i].id] = i; // 原来的第i个人现在在排序后的什么位置
    rep(i, 1, n)    pre[i] = pre[i - 1] + p[i].x;
    repp(i, n, 1)    suf[i] = suf[i + 1] + p[i].y;
    rep(i, 1, n) {
        int id = p[i].id;
        ll tmp = p[i].x * (n - i) + p[i].y * (i - 1) + pre[i - 1] + suf[i + 1];
        ans[id] = tmp;
    }
    while (m--) {
        int u = read(), v = read();
        ll tmp = min(p[pos[u]].x + p[pos[v]].y, p[pos[u]].y + p[pos[v]].x);
        ans[u] -= tmp;
        ans[v] -= tmp;
    }
    rep(i, 1, n)
        printf("%lld%c", ans[i], " \n"[i == n]);
}

int main() {
    //int T = read();    while (T--)
    solve();
    return 0;
}