CF 1051 G. Distinctification
G. Distinctification
分析:
线段树合并 + 并查集。
最后操作完后a连续递增的一段,b一定是递减的。最后的答案是$\sum (a_{new}-a_{odd}) \times b_i$,即改变后的a减去之前的a。
那么对于连续的一段考虑怎么求。按照bi建立权值线段树,线段树的一个节点的答案就是 左区间的答案+右区间的答案+左区间的和 × 右区间的个数。
即最大的$b_i$乘1,次大的乘2,...,最小的乘(n-1)分别是每个$b_i$的排名。这是对b排序后,新的答案,减去以前的即可得到答案。
如果存在相同的a,那么让a变成a+1,存在a+1,那么变成a+2……加上变后的贡献,并查集维护。注意扩大值域后,开两倍空间。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int ls[N * ], rs[N * ], siz[N * ], Root[N], R[N], fa[N], Index;
LL sum[N * ], Ans; void Insert(int l,int r,int &now,int p) {
if (!now) now = ++Index;
if (l == r) { siz[now] = , sum[now] = p; return ; }
int mid = (l + r) >> ;
if (p <= mid) Insert(l, mid, ls[now], p);
else Insert(mid + , r, rs[now], p);
sum[now] = sum[ls[now]] + sum[rs[now]], siz[now] = siz[ls[now]] + siz[rs[now]];
}
int Merge(int x,int y) {
if (!x || !y) return x | y;
Ans -= sum[ls[x]] * siz[rs[x]] + sum[ls[y]] * siz[rs[y]];
ls[x] = Merge(ls[x], ls[y]);
rs[x] = Merge(rs[x], rs[y]);
Ans += sum[ls[x]] * siz[rs[x]], siz[x] += siz[y], sum[x] += sum[y];
return x;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void solve(int x,int y) {
x = find(x), y = find(y); fa[y] = x;
Ans -= sum[Root[x]] * x + sum[Root[y]] * y;
Root[x] = Merge(Root[x], Root[y]);
Ans += sum[Root[x]] * x;
R[x] = R[y];
}
int main() {
int n = read();
for (int i = ; i <= ; ++i) fa[i] = i, R[i] = i;
for (int i = ; i <= n; ++i) {
int a = read(), b = read();
int p = Root[a] ? R[find(a)] + : a;
Ans -= 1ll * a * b;
Insert(, n, Root[p], b);
Ans += 1ll * p * b;
if (Root[p - ]) solve(p - , p);
if (Root[p + ]) solve(p, p + );
printf("%I64d\n", Ans);
}
return ;
}
CF 1051 G. Distinctification的更多相关文章
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- CF 724 G. Xor-matic Number of the Graph
G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...
- CF 1093 G. Multidimensional Queries
G. Multidimensional Queries 链接 分析: 考虑如何去掉绝对值符号. $\sum \limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|$,由于k ...
- CF 1051 F. The Shortest Statement
F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- CF 960 G
难受的1b,怎么会这样 先去学写一发 NTT 大概说一下斯特林数
- EF增删库查
public async Task<bool> Add(fu_ocrresult model) { using (var db = new GENEModel()) { //1.将实体对象 ...
- Understanding Convolutions
http://colah.github.io/posts/2014-07-Understanding-Convolutions/ Posted on July 13, 2014 neural netw ...
- Python 代码优化常见技巧
代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化.扩展以及文档相关的事情通常需要消耗 80% 的工作量.优化通常包含两方 ...
随机推荐
- 名词解释:Linux内存管理之RSS和VSZ
Linux内存管理中不管是top命令还是pmap命令,都会有RSS和VSZ这两个名词,这里解释一下: RSS( Resident Set Size )常驻内存集合大小,表示相应进程在RAM中占用了多少 ...
- 微信JSSDK与录音相关的坑
微信JSSDK与录音相关的坑 最近一直在做微信JSSDK与录音相关的功能开发, 遇到了各种奇尺大坑, 时不时冷不丁地被坑一道, 让我时常想嘶吼: "微信JSSDK就是个大腊鸡!!!!!!!! ...
- 用 Visual Studio Code 调试运行在 homestead 环境中的 laravel 程序
由于之前做 .net 开发比较熟悉 visualstudio,所以自 visualstudio code 发布后就一直在不同场合使用 vscode ,比如前端.node等等.最近在做 laravel ...
- ansible--03
一. Ad-hoc命令简介 1. 格式:ansible <host> [opion] 2. option参数: -v:输出详细的执行过程, -vvv最详细的结果 -i:指定inventor ...
- Kubernetes 初探
一.容器: 1. 容器是运行一个或一组进程的方法,使得这些进程和主机上其他进程相隔离 2. 容器类似于虚拟机,但不同于虚拟机 容器 ...
- npm发布插件步骤
开发好一个插件后,要想让其他人也能使用该插件需要将插件发布到npm上,具体步骤如下: 1.添加npm用户: npm adduser Username: your name Password: your ...
- ethereumjs/ethereumjs-common-2-API文档
https://github.com/ethereumjs/ethereumjs-common/blob/master/docs/index.md 该API的调用的详细例子可见ethereumjs/e ...
- new的三种形态
C++语言一直被认为是复杂编程语言中的杰出代表之一,不仅仅是因为其繁缛的语法规则,还因为其晦涩的术语.下面要讲的就是你的老熟人—new: 它是一个内存管理的操作符,能够从堆中划分一块区域,自动调用构造 ...
- DRCNN超分辨重建2016年
论文疑点: Embedding层是怎么操作的? https://gshtime.github.io/2018/06/01/tensorflow-embedding-lookup-sparse/ 这篇文 ...
- JSONP - 从理论到实践
同源策略 ajax之所以需要“跨域”,罪魁祸首就是浏览器的同源策略.即,一个页面的ajax只能获取这个页面相同源或者相同域的数据. 如何叫“同源”或者“同域”呢?——协议.域名.端口号都必须相同.例如 ...