题意

题目链接

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;
}

cf1043E. Mysterious Crime(二分 前缀和)的更多相关文章

  1. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  2. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  3. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

  4. CodeForces 1043D Mysterious Crime 区间合并

    题目传送门 题目大意: 给出m个1-n的全排列,问这m个全排列中有几个公共子串. 思路: 首先单个的数字先计算到答案中,有n个. 然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个 ...

  5. [题解]Codeforces Round #519 - D. Mysterious Crime

    [题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...

  6. 【BZOJ-1926】粟粟的书架 二分 + 前缀和 + 主席树

    1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Sec  Memory Limit: 552 MBSubmit: 616  Solved: 238[Submit][Statu ...

  7. [NOIP2011] 聪明的质监员 二分+前缀和

    考试的时候打的二分但没有用前缀和维护.但是有个小细节手误打错了结果挂掉了. 绝对值的话可能会想到三分,但是注意到w增大的时候y是减小的,所以单调性很明显,用二分就可以.但注意一个问题,就是二分最后的结 ...

  8. 【CF587D】Duff in Mafia 二分+前缀优化建图+2-SAT

    [CF587D]Duff in Mafia 题意:给你一张n个点m条边的无向图,边有颜色和边权.你要从中删去一些边,满足: 1.任意两条删掉的边没有公共的顶点.2.任意两条剩余的.颜色相同的边没有公共 ...

  9. Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀

    D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 megabytes ...

随机推荐

  1. 最短路 CF954D Fight Against Traffic

    CF954D Fight Against Traffic 题意描述: 给你一张无向图,一共有n个点(2 <= n <= 1000),由m条边连接起来(1 <= m <= 100 ...

  2. PHP将多维数组变成一维数组

    function reduceArray($array) { $return = []; array_walk_recursive($array, function ($x) use (&$r ...

  3. docker-compose部署mysql配置

    docker-compose部署mysql配置文件如下 version: ' services: mysql: image: mysql environment: - MYSQL_ROOT_PASSW ...

  4. 如何实现java的四则运算

    很多语言底层对四则运算都有内部封装, 我们还是要重复造下轮子,不为别的, 就是为了面试可以多装一分 b, 假设你已经了解了什么是二进制, 什么是异或, 什么是移位运算, 什么是与, 这些不懂就别硬上( ...

  5. Numpy随机数(一):超几何分布

    超几何分布 产品抽样检查中经常遇到一类实际问题,假定在N件产品中有M件不合格品,即不合格率 . 在产品中随机抽n件做检查,发现k件不合格品的概率为 ,k=0,1,2,...,min{n,M}. Num ...

  6. [USACO19FEB]Cow Dating

    Luogu5242 通过观察数据,我们可以发现,右端点的取值是单调递增的.于是,我们可以极限一波,用一个双指针法,类似于队列. 右端点的取值满足以下公式: (1-p1)(1-p2)..(1-pn) * ...

  7. 认识CSS中盒子模型

    前端之HTML,CSS(六) CSS 盒子模型 CSS中的重点,理解盒子模型对于CSS才能有更清晰的认识.网页说简单一点其实就是一块一块的拼接出来的,可以想象成拼图,所有图块拼接在一起就成了一幅图像. ...

  8. Shiro源码解析-登录篇

    本文以循序渐进的方式解析Shiro整个login过程的处理,读者可以边阅读本文边自己看代码来思考体会,如有问题,欢迎评论区探讨! 笔者shiro的demo源码路径:https://github.com ...

  9. post调试postman

    载地址:https://www.getpostman.com/ 教程地址:http://www.cnblogs.com/s380774061/p/4624326.html

  10. Python爬虫:微博粉丝列表

    前言 本来打算做一个关于微博粉丝列表的爬虫,可以统计一下某个微博账号的粉丝里面,僵尸粉(水军)的数量,大V数量. 结果写完爬虫才发现,现在微博只给人看粉丝列表的前5页.......哈哈,好吧.挺无奈的 ...