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个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
随机推荐
- 【仿真】Carla介绍与基本使用 [1] (附代码 基础版)
0. 参考与前言 主要介绍无人驾驶的仿真环境CARLA,开源社区维护,以下为相关参考链接: Carla官方文档 建议后续找的时候 先按好版本号,有些功能/api 是新版本里有的 Carla官方gith ...
- 用HMS Core地图服务自定义地图样式,给你的应用制作专属个性化地图
不同行业的开发者对地图样式的展示需求差异很大.例如,物流类应用希望地图样式简洁一些,重点突出城市分布和快递路径:AR游戏类应用中的地图色彩需要和游戏UI适配,做的更酷炫一些:景区导览应用中的地图样式要 ...
- 【SpringSecurity系列3】基于Spring Webflux集成SpringSecurity实现前后端分离无状态Rest API的权限控制
源码传送门: https://github.com/ningzuoxin/zxning-springsecurity-demos/tree/master/02-springsecurity-state ...
- 架构师必备:HBase行键设计与应用
首先要回答一个问题,为何要使用HBase? 随着业务不断发展.数据量不断增大,MySQL数据库存在这些问题: MySQL支持的数据量为TB级,不能一直保留历史数据.而HBase支持的数据量为PB级,适 ...
- vue-property-decorator
vue-property-decorator使我们能在vue组件中写TypeScript语法,依赖于vue-class-component 装饰器:@Component.@Prop.@PropSync ...
- c++ 树状数组
关于树状数组 树状数组,即 Binary Indexed Tree ,主要用于维护查询前缀和 属于 log 型数据结构 和线段树比较 都是 log 级别 树状数组常数.耗费的空间.代码量都比线段树小 ...
- 技术分享 | Appium 用例录制
原文链接 下载及安装 下载地址: https://github.com/appium/appium-desktop/releases 下载对应系统的 Appium 版本,安装完成之后,点击 " ...
- UiPath屏幕抓取Screen Scraping的介绍和使用
一.屏幕抓取(Screen Scraping)的介绍 屏幕抓取使您能够在特定的UI元素或文档(如.PDF文档)中提取数据 二.屏幕抓取(Screen Scraping)在UiPath中的使用 [if ...
- 比我的脸还干的gan货——Python Flask Web 框架入门
Flask是一个轻量级的基于Python的web框架. 本文适合有一定HTML.Python.网络基础的同学阅读. 1. 简介 这份文档中的代码使用 Python 3 运行.是的,所以读者需要自己在电 ...
- gslb(global server load balance)技术的一点理解
gslb(global server load balance)技术的一点理解 前言 对于比较大的互联网公司来说,用户可能遍及海内外,此时,为了提升用户体验,公司一般会在离用户较近的地方建立机房,来服 ...