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个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
随机推荐
- 关于p命名空间和c命名空间 外加一个context
P命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:p="http://www.springframework.org/schema/p" 如 xmlns=& ...
- STL栈与队列
#include<queue>// 队列 #include<stack>//栈 stack<int> s;//参数也是数据类型,这是栈的定义方式 queue< ...
- element-ui table组件使用v-if时的问题
element-ui项目中经常遇到需要使用v-if指令来根据情况动态显示隐藏某些列情况,这时就会出现滚动条样式异常.列错乱.列宽错乱等问题 解决办法:在el-table上添加:key="Ma ...
- 2020.10.17【普及组】模拟赛C组 总结
总结 这次比赛 120 分,老师说上 200 是不容易的,但我觉得这不是我真的水平 改题情况 T1 题目大意:有 N 个小朋友,每个小朋友有 \(B_i\) 个朋友,问从中随机选 3 人使得 3 人关 ...
- CAP 6.1 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 6.1 版本正式版,在这个版本中我们主要针对目前已经发现的几个BUG进行了修复了以及添加了一些小特性. 那么,接下来我们具体看一下吧. 总览 可能有些人还不知 ...
- ssh打通
打通ssh https://www.cnblogs.com/yolanda-lee/p/4975453.html
- Redis 中的事务分析,Redis 中的事务可以满足ACID属性吗?
Redis 中的事务 什么是事务 1.原子性(Atomicity) 2.一致性(Consistency) 3.隔离性(Isolation) 4.持久性(Durability) 分析下 Redis 中的 ...
- JS:this关键字1
this 代表了当前的对象,哪个对象调用了this所在的函数,this就代表了哪个对象: 例1: function fn(){ var a = 1; console.log(this) } fn() ...
- ASP.NET MVC之读取服务器文件资源的两种方式
初次认识asp.net mvc时,以为所有文件都需要走一遍路由,然后才能在客户端显示, 所以我首先介绍这一种方式 比如说:我们在服务器上有图片: ~/resource/image/5.jpg 我们就需 ...
- Python使用腾讯云-短信服务发送手机短信
目前[腾讯云短信]为客户提供[国内短信].[国内语音]和[海外短信]三大服务,腾讯云短信SDK支持以下操作: 国内短信 国内短信支持操作: • 指定模板单发短信 • 指定模板群发短信 • 拉取短信回执 ...