Luogu-1975 [国家集训队]排队
Luogu-1975 [国家集训队]排队
题面
题解
题意:给出一个长度为n的数列以及m个交换两个数的操作,问每次操作后逆序对数量
时间,下标和数的大小三维偏序,,,把交换操作看成是减去两个数再加上两个数,套板子就好了
发现这种计数类型的CDQ一般有两种写法:
按a排序,CDQ内先递归左右两边让b有序后扫一遍用左边更新右边
按b排序,CDQ内先按照a与mid的关系分为两部分,用前面更新后面,然后递归两边
感觉应该都差不多,但有些题目用两种方式写也有一些优劣之分,比如这道题,用第二种方式感觉就特别的好写。。。
代码
#include<map>
#include<queue>
#include<cmath>
#include<ctime>
#include<stack>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
inline char gc(){
//static char buf[100000],*p1,*p2;
//return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
}
inline int read(){
int ans=0,fh=1;
char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-') fh=-1; ch=gc();}
while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=gc();
return ans*fh;
}
const int maxn=3e6+100;
struct node{
int tim,a,b,ms,bh;
}c[maxn],tmp[maxn];
int n,m,b[maxn],cr[maxn],tre[maxn],cl,tot,qtot,d[maxn];
ll ans[maxn];
map<int,int>bh;
bool cmp(node x,node y){return x.a<y.a||(x.a==y.a&&x.tim<y.tim);}
void revise(int x,int z){
for(int i=x;i<maxn;i+=i&(-i))
if(cr[i]==cl) tre[i]+=z;
else cr[i]=cl,tre[i]=z;
}
int query(int x,int Ans=0){
for(int i=x;i;i-=i&(-i))
if(cr[i]==cl) Ans+=tre[i];
return Ans;
}
void cdq(int l,int r){
if(l==r) return;
int mid=l+r>>1;cl++;
for(int i=l;i<=r;i++)
if(c[i].tim<=mid) revise(c[i].b,c[i].ms);
else ans[c[i].bh]+=c[i].ms*(query(maxn-1)-query(c[i].b));
cl++;
for(int i=r;i>=l;i--)
if(c[i].tim<=mid) revise(c[i].b,c[i].ms);
else ans[c[i].bh]+=c[i].ms*query(c[i].b-1);
int lc=l-1,rc=mid;
for(int i=l;i<=r;i++)
if(c[i].tim<=mid) tmp[++lc]=c[i];
else tmp[++rc]=c[i];
for(int i=l;i<=r;i++) c[i]=tmp[i];
cdq(l,mid),cdq(mid+1,r);
}
int main(){
// freopen("1975.in","r",stdin);
n=read();int x,y;
for(int i=1;i<=n;i++){
b[i]=d[i]=x=read();
c[i]=(node){i,i,x,1,0};
}
sort(b+1,b+n+1);b[0]=-1;
for(int i=1;i<=n;i++)
if(b[i]!=b[i-1]) bh[b[i]]=++tot;
for(int i=1;i<=n;i++)
c[i].b=d[i]=bh[c[i].b];
m=read();
for(int i=1;i<=m;i++){
x=read(),y=read(),++qtot;
c[++n]=(node){n,x,d[y],1,qtot};
c[++n]=(node){n,y,d[x],1,qtot};
c[++n]=(node){n,x,d[x],-1,qtot};
c[++n]=(node){n,y,d[y],-1,qtot};
swap(d[x],d[y]);
}
sort(c+1,c+n+1,cmp);
cdq(1,n);
for(int i=1;i<=qtot;i++) ans[i]+=ans[i-1];
for(int i=0;i<=qtot;i++) printf("%lld\n",ans[i]);
return 0;
}
Luogu-1975 [国家集训队]排队的更多相关文章
- 「Luogu P1975 [国家集训队]排队」
题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...
- luogu P2757 [国家集训队]等差子序列
题目链接 luogu P2757 [国家集训队]等差子序列 题解 线段树好题 我选择暴力 代码 // luogu-judger-enable-o2 #include<cstdio> inl ...
- luogu P2619 [国家集训队2]Tree I
题目链接 luogu P2619 [国家集训队2]Tree I 题解 普通思路就不说了二分增量,生成树check 说一下坑点 二分时,若黑白边权有相同,因为权值相同优先选白边,若在最有增量时出现黑白等 ...
- 【LG1975】[国家集训队]排队
[LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...
- [Luogu P1829] [国家集训队]Crash的数字表格 / JZPTAB (莫比乌斯反演)
题面 传送门:洛咕 Solution 调到自闭,我好菜啊 为了方便讨论,以下式子\(m>=n\) 为了方便书写,以下式子中的除号均为向下取整 我们来颓柿子吧qwq 显然,题目让我们求: \(\l ...
- Luogu P1297 [国家集训队]单选错位
P1297 [国家集训队]单选错位 题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上 ...
- Luogu P2619 [国家集训队2]Tree I(WQS二分+最小生成树)
P2619 [国家集训队2]Tree I 题意 题目描述 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有\(need\)条白色边的生成树. 题目保证有解. 输入输出格式 输入格式 ...
- 【luogu P1494 [国家集训队]小Z的袜子】 题解
题目链接:https://www.luogu.org/problemnew/show/P1494 #include <cstdio> #include <algorithm> ...
- 【luogu P1903 [国家集训队]数颜色】 题解
题目链接:https://www.luogu.org/problemnew/show/P1903 裸的...带修莫队... 比较麻烦吧(对我来说是的) 两个变量分开记录查询和修改操作. #includ ...
随机推荐
- [Go语言]从Docker源码学习Go——结构和函数的定义
Docker在最近很火,而作为Docker的开发语言-Go也再次被大家提到. 已经使用Docker一段时间了,但是对于源码,尤其是其开发语言Go却一直是一知半解. 最近准备利用空余时间从Docker源 ...
- 【BZOJ5060】魔方国 特判
[BZOJ5060]魔方国 Description 小奇和魔法猪打开了战狂的遗迹,穿越到了东元20年.东元元年,战狂率领一千万士兵毁灭了一个又一个文明,并建立起了新文明——昌和帝国,招募了八位伟人:大 ...
- Leetcode-Convert Sorted List to BST.
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- maven pom.xml常用标签 Exclusions plugins是什么意思
Exclusions maven的依赖(dependencies)有传递性,为了解决兼容性问题,就用exclusions来排除造成兼容性问题的依赖. 写法如下: 加入项目A依赖项目B,项目B依赖项目C ...
- ubuntu 使用串口picocom
连上USB转串口 查看是否识别串口 dmesg | grep ttyUSB0 安装(mint / ubuntu): $ sudo apt-get install picocom 使用: 先赋予 ...
- MVC4 WebAPI中如何返回一张图片
public HttpResponseMessage Get(string imageName, int width, int height) { Image img = GetImage(image ...
- 多个StoryBoard之间的跳转
iOS项目中可以将同一业务流程的页面归置到一个StoryBoard中,项目中必然会包含多个StroryBoard,可以利用跳转,实现项目的不同业务流程页面间的跳转切换. 实现思路: 1,项目(Proj ...
- Open System Interconnection
https://zh.wikipedia.org/wiki/Secure_Shell Secure Shell(缩写为SSH),由IETF的网络工作小组(Network Working Group)所 ...
- python学习笔记(二)— 数据类型
一.变量.数据类型 1.计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同 ...
- Java 集合框架查阅技巧
如何记录每一个容器的结构和所属体系呢? List ArrayList LinkedList Set HashSet TreeSet 其中,后缀名就是该集合所属的体系,前缀名就是该集合的数据结构. 看到 ...