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的更多相关文章

  1. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  2. CF 724 G. Xor-matic Number of the Graph

    G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...

  3. CF 1093 G. Multidimensional Queries

    G. Multidimensional Queries 链接 分析: 考虑如何去掉绝对值符号. $\sum \limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|$,由于k ...

  4. CF 1051 F. The Shortest Statement

    F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...

  5. CF 914 G Sum the Fibonacci —— 子集卷积,FWT

    题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...

  6. CF 960 G

    难受的1b,怎么会这样 先去学写一发 NTT 大概说一下斯特林数

  7. EF增删库查

    public async Task<bool> Add(fu_ocrresult model) { using (var db = new GENEModel()) { //1.将实体对象 ...

  8. Understanding Convolutions

    http://colah.github.io/posts/2014-07-Understanding-Convolutions/ Posted on July 13, 2014 neural netw ...

  9. Python 代码优化常见技巧

    代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化.扩展以及文档相关的事情通常需要消耗 80% 的工作量.优化通常包含两方 ...

随机推荐

  1. Oracle诊断工具 - ORA-4030 Troubleshooting Tool

    ORA-4030 说明Oracle服务器进程(server process)无法在操作系统(OS)上分配到足够的内存.   导致ORA-4030 的主要原因有: -物理内存不足 -OS kernel/ ...

  2. [控件] 创建出条形间隔效果的背景LineBackgroundView

    创建出条形间隔效果的背景LineBackgroundView 效果: 使用: // // ViewController.m // LineBackgroundView // // Created by ...

  3. [翻译] GCDObjC

    GCDObjC https://github.com/mjmsmith/gcdobjc GCDObjC is an Objective-C wrapper for the most commonly ...

  4. NSCopying简析

    NSCopying简析 用到NSCopying的时候并不多,但还是有必要知道最基本的用途,比方说数组的拷贝操作,需要注意的是,数组的拷贝操作并不是执行了 copy 方法,而是需要执行 initWith ...

  5. 生成器(generator),迭代器(yield)

    g=(i for i in range(10)) #小括号表示生成一个迭代生成器.[]是列表生成器 g.__next__() yield将一个函数变成生成器 import time def f(): ...

  6. zabbix日常监控项java(四)

    yum install net-tools netstat命令 yum -y install bash-completion 命令自动补全包 https://github.com/qiueer/zab ...

  7. Hadoop HBase概念学习系列之HBase里的列式数据库(十七)

    列式数据库,从数据存储方式上有别于行式数据库,所有数据按列存取. 行式数据库在做一些列分析时,必须将所有列的信息全部读取出来 而列式数据库由于其是按列存取,因此只需在特定列做I/O即可完成查询与分析, ...

  8. November 20th 2016 Week 47th Sunday

    Learn from yesterday, live for today, look to tomorrow. 学习昨天,活在今天,展望明天. There is always room at the ...

  9. Spring 利用PropertyPlaceholderConfigurer占位符

      Hey Girl   博客园    首页    博问    闪存    新随笔    订阅     管理 posts - 42,  comments - 3,  trackbacks - 0 Sp ...

  10. 智能指针shared_ptr新特性shared_from_this及weak_ptr

    enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...