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. idea常用到的命令

    idea大小写转换:ctr+shift+u ctrl+alt+b,跳转到接口的方法实现处

  2. asp.net MVC4 框架揭秘 读书笔记系列2

    1.2 MVC 变体 MVC 是一种Pattern 另外一种说法是ParaDigm 范例 模式和范例的区别在于前者可以应用到具体的应用上,而后者则仅仅提供一些指导方针 1.2.1 MVP Model ...

  3. 剑指offer 07斐波那契数列

    现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 java版本: public class Solution { public static void m ...

  4. Beanstalkd 的理解

    Beanstalkd 的理解 Beanstalkd 是一个轻量级的内存型队列,利用了和Memcache 类似的协议.其官网beanstakkd官网 下方的感谢语说: Many thanks to me ...

  5. Linux----CentOS-7搭建免流服务器(iOS 端)

    本次实验采用腾讯云服务器:https://cloud.tencent.com/ 大学生身份的可以看看有没有什么活动购买 其他身份的78一个月 关于腾讯云服务器的使用可以看看腾讯云的使用手册 本博客涉及 ...

  6. 026.1 网络编程 获取IP地址

    前面提及的:OSI,TCP-IP,IP地址,端口,协议概念我都清楚,所以我直接跳过前面,来到使用这里. //获取本机IP InetAddress ip = InetAddress.getLocalHo ...

  7. mysql 索引分类以及用途分析

    MySQL索引分为普通索引.唯一性索引.全文索引.单列索引.多列索引等等.这里将为大家介绍着几种索引各自的用途. 一. MySQL: 索引以B树格式保存 Memory存储引擎可以选择Hash或BTre ...

  8. jquery ajax跨域解决

    双十一开发了一个抽奖API,最近上线了,各个事业部的大神们需要前台页面,异步调用我的抽奖API,要我提供js. js 提供之后发现不对,跨域了.之前也碰到过跨域的问题,研究过这个问题,三种方法解决. ...

  9. Java 处理cookie的方法

    一.java创建cookie 方法一: Response.Cookies["userName"].Value = "patrick"; Response.Coo ...

  10. mvc, web mvc, spring web mvc 区别

    1. MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据显示分离的 ...