BZOJ3295/Luogu3157 [CQOI2011]动态逆序对 (CDQ or 树套树 )
/*
Dear friend, wanna learn CDQ?
As a surprice, this code is totally wrong.
You may ask, then why you put it on your blog, are you fucking crazy with the fans, isn't it clear you do not have one.
Well, I just found a anime picture in other's blog.
I love it, so I wanna put it on my blog, but, if I waste time for a picture, I'll feel that I'm a shame.jpg!.
So I cheated to myself that I just use this picture to decorate my code.
Good reason, isn't it.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ (a))
#define nR(a,b,c) for(register int a = (b); a >= (c); -- (a))
#define ll long long
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n---------------------\n\n");
#else
#define D_e_Line ;
#endif
struct ios{
	template<typename ATP>ios& operator >> (ATP &x){
		x = 0; int f = 1; char c;
		for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
		while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
		x *= f;
		return *this;
	}
}io;
using namespace std;
const int N = 100007;
int n,m;
int tim;
struct Element{
	int a,b,c;
	long long ans;
	bool operator< (const Element &com)const{
		if(a != com.a) return a < com.a;
		if(b != com.b) return b > com.b;
		return c < com.c;
	}
}a[N],tmp[N];
long long t[N];
inline void Updata(int x, int w){
	for(; x <= tim; x += x&-x) t[x] += w;
}
inline long long Query(int x){
	long long s = 0;
	for(; x; x -= x&-x) s += t[x];
	return s;
}
inline void CDQ(int l,int r){
	if(l == r) return;
	int mid = (l + r) >> 1;
	CDQ(l, mid), CDQ(mid + 1, r);
	int i = l, j = mid + 1, k = l;
	while(i <= mid && j <= r){
		if(a[i].b >= a[j].b){
			Updata(a[i].c, 1);
			tmp[k++] = a[i++];
		}
		else{
			a[j].ans += Query(a[j].c);
			tmp[k++] = a[j++];
		}
	}
	while(j <= r) a[j].ans += Query(a[j].c), tmp[k++] = a[j++];
	R(j, l, i - 1) Updata(a[j].c, -1);
	while(i <= mid) tmp[k++] = a[i++];
	R(i,l,r) a[i] = tmp[i];
}
long long tot[N];
int main(){
	io >> n >> m;
	tim = 1;
	R(i,1,n){
		io >> a[i].b;
		a[i].a = i;
		a[i].c = tim;
	}
	R(i,1,m){
		int x;
		io >> x;
		a[x].c = ++tim;
	}
	sort(a + 1, a + n + 1);
	CDQ(1, n);
	R(i,1,n){
		tot[a[i].c] += a[i].ans;
		//cout<<"$$"<<a[i].ans<<endl;
	}
	R(i,2,tim){
		printf("%lld\n", tot[i]);
	}
	return 0;
}

Another way is to use BIT to work with segment tree of value.
#define lson t[rt].l, l, mid
#define rson t[rt].r, mid + 1, r
struct SegmentTree{
	int l,r;
	int siz;
}t[N*100];
int treeIndex;
inline void Pushup(int &rt){//pointer, pointer, once again! pointer!!!!@!$#$!@%$!@%!@^!$#&*$(&
	t[rt].siz = t[t[rt].l].siz + t[t[rt].r].siz;
}
inline void SegModify(int &rt,int l,int r,int x,int val){//be careful, rt is a pointer for rt[]
	if(!rt) rt = ++treeIndex;
	if(l == r){
		t[rt].siz += val;
		return;
	}
	int mid = (l + r) >> 1;
	if(x <= mid)
		SegModify(lson, x, val);
	else
		SegModify(rson, x, val);
	Pushup(rt);
}
inline int SegQuery(int rt,int l,int r,int L,int R){
	if(!rt || L > R) return 0;
	if(L <= l && r <= R) return t[rt].siz;
	int mid = (l + r) >> 1, sum = 0;
	if(L <= mid) sum += SegQuery(lson, L, R);
	if(R > mid) sum += SegQuery(rson, L, R);
	return sum;
}
int rt[N];
inline void Updata(int x,int w,int val){
	for(; x <= n; x += x&-x) SegModify(rt[x], 1, n, w, val);
}
inline int Query(int x,int w){
	int s = 0;
	for(; x; x -= x&-x) s += SegQuery(rt[x], 1, n, 1, w);
	return s;
}
int pos[N];
int a[N];
int main(){
//	freopen("IN.txt","r",stdin);
//	freopen("OUT.txt","w",stdout);
	int m;
	io >> n >> m;
	R(i,1,n){
		io >> a[i];
		Updata(i, a[i], 1);
		pos[a[i]] = i;
	}
	long long ans = 0;
	R(i,1,n){
		ans += Query(i - 1, n) - Query(i - 1, a[i]);//QAQ
	}
	while(m--){
		int x;
		io >> x;
		printf("%lld\n", ans);
		ans -= Query(pos[x] - 1, n) - Query(pos[x] - 1, x) + Query(n, x - 1) - Query(pos[x], x - 1);//QTAQ
		Updata(pos[x], x, -1);
	}
	return 0;
}
												
											BZOJ3295/Luogu3157 [CQOI2011]动态逆序对 (CDQ or 树套树 )的更多相关文章
- 【BZOJ3295】[Cqoi2011]动态逆序对 cdq分治
		
[BZOJ3295][Cqoi2011]动态逆序对 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依 ...
 - bzoj3295 [Cqoi2011]动态逆序对 cdq+树状数组
		
[bzoj3295][Cqoi2011]动态逆序对 2014年6月17日4,7954 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数. ...
 - [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树
		
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...
 - BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治
		
题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 1 ...
 - bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)
		
3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...
 - 2018.07.01 BZOJ3295: [Cqoi2011]动态逆序对(带修主席树)
		
3295: [Cqoi2011]动态逆序对 **Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j& ...
 - P3157 [CQOI2011]动态逆序对 (CDQ解决三维偏序问题)
		
P3157 [CQOI2011]动态逆序对 题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任 ...
 - 【bzoj3295】[Cqoi2011]动态逆序对
		
题目描述: 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆 ...
 - 【bzoj3295】[Cqoi2011]动态逆序对  线段树套SBT
		
题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
 
随机推荐
- 三面阿里,被Java面试官虐哭!现场还原真实的“被虐”场景
			
前言 人人都有大厂梦,我也不例外,从大三开始,就一直想进入阿里工作,大毕竟是大厂,想想也没那么容易,不过好在自己学历还过得去,项目经验也有得讲,所以今年也斗胆尝试了一下,直接就投了阿里云计算.简历是过 ...
 - Vue出现Component template should ...
			
当运行vue出现错误Component template should contain exactly one root element. If you ...的时候,我们只需要将<templa ...
 - [学习笔记]使用Docker+Jenkin自动化流水线发布.Net应用
			
使用Docker容器方案可以快速安全地将项目部署到客户的服务器上,作为公司项目,需要解决两个问题: 1. 需要搭建一个私有的Docker仓库,以便安全的存储镜像 2. 需要一套自动化发布方案,实现代 ...
 - vue组件data函数
			
vue组件data通常定义为一个函数并return一个对象,对象中定义的就是组件数据,当然定义数据还有props.computed等方式. data如果直接定义为对象data: {message: ' ...
 - VTK 在WINDOWS上的安装使用
			
参考:http://www.vtk.org/Wiki/VTK/Building/Windows#Step_5_-_Open_the_Visual_Studio_project
 - C++ 炼气期之数据是主角
			
1. 前言 数据在程序中的重要性,怎么强调都不为过,程序的本质就是通过提供数据处理逻辑,把数据从一种状态变成另一种状态的过程.处理逻辑一定是有针对性的,针对的是数据本身的特性. 只有了解了数据本身的内 ...
 - 使用FileSystemWatcher监听文件状态
			
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 一.FileSystemWatcher类型介绍 在.NET中使用 FileSystemWatcher 类型可以进行监视指定 ...
 - C#中的枚举器
			
更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月28日. 一.先从可枚举类型讲起 1.1 什么是可枚举类型? 可枚举类型,可以简单的理解为: 有一个类,类中有挺多的数据,用一种统 ...
 - JS:!非
			
取非运算符: 开关思想:0为false,1为true: 把一个变量中保存一个布尔值 然后在业务执行时,修改这个变量的值: 为取反 然后通过变量的值执行分支业务 例子: var a = "12 ...
 - BUUCTF-被偷走的文件
			
被偷走的文件 这题刚开始还以为是单纯的流量题,看流量半天也没发现什么异常. 因为是文件传输过程的,所以我们看到ftp的流量就过滤下看看即可. 在第三个包发现flag.rar存在. 一开始我觉得没啥,后 ...