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% 的工作量.优化通常包含两方 ...
随机推荐
- 分析VoltDB内存数据库
转自https://blog.csdn.net/olidrop/article/details/7065384 https://blog.csdn.net/ransom0512/article/det ...
- 铁乐学python_Day42_线程-信号量事件条件
铁乐学python_Day42_线程-信号量事件条件 线程中的信号量 同进程的一样,Semaphore管理一个内置的计数器, 每当调用acquire()时内置计数器-1:调用release() 时内置 ...
- Spring 如何在 WEB 应用中使用
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...
- n = 3 , while n , continue
- n=n+1 放在print(s)的上面的影响 (2) n=n=+1在前面,则不满足前面<100条件时候,才跳出while的循环,这时候while循环结束, 到了外面的下一步-->print()
1+2+3+....+100= ? n=1 s = 0 while n < =100: s = s+n n= n+1 # n=n+1 在print(s)上面的情况 print(s)
- (转)em重建全过程
该问题遇到N次,被郁闷N次,特此记录以备不时之需 由于n久不用em,而本机在公司使用dhcp自动获取ip,导致ip变化,而使em启动报出ora-12514 DBD ERROR: OCIServerAt ...
- centos6.2/6.3/6.4+nginx+mysql5.5+php5.3.14
一.安装所需软件包yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...
- 输入一批考生的的准考证号码,如果是 15 位,表示输入正确,否则重新输入。然后判断这个人的考试类别(号码中如果是以奇数结尾则考试类别为“A 类”,否则为“B 类”),最后输出此准考证的前 5 位和后 4 位,其他位用“*”来代替。说明:使用 StringBuffer 类的相关方法完成实验内容。
因为是一批考生,所以先创建一个字符数组存放一组准考证号. 此外这个程序涉及到包装类与基本数据类型的互相转换. string的substring方法,返回一个字符串是该字符串的子串.从第一个参数开始,第 ...
- 开源 免费 java CMS - FreeCMS1.9 全文检索
项目地址:http://code.google.com/p/freecms/ 全文检索 从FreeCMS 1.7開始支持 仅仅有创建过索引的对象才干被lucene类标签查询到. 信息类数据会在信息更新 ...
- Red Hat Linux 挂载外部资源
在我们安装的Red Hat Linux 中.当中一半机器为最主要的server配置,没有桌面环境.在从U盘上复制文件的时候可就犯难了.在网上查了查才知道.要訪问U盘就必须先将它们挂载到Linux系统的 ...