题意

题目链接

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. Python实现——二层BP神经网络

    2019/4/23更新 下文中的正确率极高是建立在仅有50组训练数据的基础上的,十分不可靠.建议使用提供的另一个生成训练集的generate_all函数,能产生所有可能结果,更加可靠. 2019/4/ ...

  2. 北航软院2012级C#期末考试部分考题解答

    博主注:本渣渣水平有限,文中若有不对的地方敬请指出,谢谢. 本文中大部分图片来自老师的PPT,感谢邵老师,想要的可以点击右边QQ联系我:) 一.选择题(2*15=30) 1.In C# what is ...

  3. jQuery 与 js的入口函数写法

    //js的入口函数执行要比jQuery的入口函数执行得晚一些. //jq的入口函数会等待页面的加载完成才执行,但是不会等待图片的加载. //js的入口函数会等待页面加载完成,并且等待图片加载完成才开始 ...

  4. 【洛谷P3909】异或之积

    题目大意:给定一个 N 个数字组成的序列,求 \[ \left(6 \times \sum_{i=1}^{N} \sum_{j=i+1}^{N} \sum_{k=j+1}^{N} A_{i} \tim ...

  5. Navicat 连接腾讯云

    1.dos窗口下进入mysql,进行远程登录授权 (1)进行授权 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1 ...

  6. java HelloWorld时报错:"找不到或无法加载主类"问题的解决办法

    学习java的第一天: 当我在做Java入门的时候,根据教程写的第一个Java程序是: public class Hello{ public static void main(String args[ ...

  7. Oracle使用总结

    1. 在ORACLE中Service Name即为数据库名称: 2. 在做删除操作时,需要加Commit进行操作提交: 3. 使用sqlldr将数据进行批量导入到ORACLE中: 3.1  Sqlld ...

  8. c# xml序列化和反序列化。也就是xml的解析和反解析。

    用习惯了newTownSoft.json 的json反序列化.碰到xml是真的不习惯. 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名.然后直接ne ...

  9. mysql优化和sql语句优化总结

    mysql性能优化 1. EXPLAIN 你的 SELECT 查询.使用 EXPLAIN 关键字可以让你知道MySQL是如何处理你的SQL语句的.这可以帮你分析你的查询语句或是表结构的性能瓶颈. 2. ...

  10. [目录]ASP.NET web api开发实战

    第一章:Restful web service v.s. RPC style web service 第二章:ASP.NET web api v.s. WCF v.s. ASP.NET web ser ...