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 [国家集训队]排队的更多相关文章

  1. 「Luogu P1975 [国家集训队]排队」

    题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...

  2. luogu P2757 [国家集训队]等差子序列

    题目链接 luogu P2757 [国家集训队]等差子序列 题解 线段树好题 我选择暴力 代码 // luogu-judger-enable-o2 #include<cstdio> inl ...

  3. luogu P2619 [国家集训队2]Tree I

    题目链接 luogu P2619 [国家集训队2]Tree I 题解 普通思路就不说了二分增量,生成树check 说一下坑点 二分时,若黑白边权有相同,因为权值相同优先选白边,若在最有增量时出现黑白等 ...

  4. 【LG1975】[国家集训队]排队

    [LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...

  5. [Luogu P1829] [国家集训队]Crash的数字表格 / JZPTAB (莫比乌斯反演)

    题面 传送门:洛咕 Solution 调到自闭,我好菜啊 为了方便讨论,以下式子\(m>=n\) 为了方便书写,以下式子中的除号均为向下取整 我们来颓柿子吧qwq 显然,题目让我们求: \(\l ...

  6. Luogu P1297 [国家集训队]单选错位

    P1297 [国家集训队]单选错位 题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上 ...

  7. Luogu P2619 [国家集训队2]Tree I(WQS二分+最小生成树)

    P2619 [国家集训队2]Tree I 题意 题目描述 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有\(need\)条白色边的生成树. 题目保证有解. 输入输出格式 输入格式 ...

  8. 【luogu P1494 [国家集训队]小Z的袜子】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1494 #include <cstdio> #include <algorithm> ...

  9. 【luogu P1903 [国家集训队]数颜色】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1903 裸的...带修莫队... 比较麻烦吧(对我来说是的) 两个变量分开记录查询和修改操作. #includ ...

随机推荐

  1. 苹果 App Store 申请和管理相关知识

    本文转载至 http://blog.csdn.net/yu0089/article/details/8027669 app store为开发者提供四种类型的申请: 个人ios开发者计划$99/年 公司 ...

  2. 存一些可能会用得到的vue的UI框架

    VUX 项目主页:https://vux.li/#/ github地址:https://github.com/airyland/vux element UI(饿了么后台) Element 是由饿了么U ...

  3. winsxs清理

    win7的网友经常问:C:Windowswinsxs文件夹下是什么文件,可以删除吗? 答案是不建议你删除的,如果你特别想删除的话我推荐你一个工具. 本人亲测系统正常运行. 可以为C盘剩下将近4G空间 ...

  4. 在Scrapy中使用IP池或用户代理(python3)

    一.创建Scrapy工程 scrapy startproject 工程名 二.进入工程目录,根据爬虫模板生成爬虫文件 scrapy genspider -l # 查看可用模板 scrapy gensp ...

  5. Centos6.5下Samba服务器的安装和配置

    1.安装samba服务 # yum install samba samba-client samba-swat 2.安装包说明 samba-3.6.23-43.el6_9.x86_64----> ...

  6. Taylor's theorem

    w https://en.wikipedia.org/wiki/Taylor_series

  7. centos7 Docker Compose 的安装

    [root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose ...

  8. requests 中response如何改变编码格式

    查看初始编码 首先查看拿到的response编码格式: (就不放代码了,因为此例需要用到cookie,可自行找个网站具体测试) 可见初始编码为:ISO-8859-1 修改编码 初始编码: 修改后编码: ...

  9. 《深入理解Linux内核》阅读笔记 --- Chapter 3 Processes

    Process Switching 1.The set of data that must be loaded into the registers before the process resume ...

  10. java jacob调用打印,word,excel横向打印

    public static boolean printOfficeFile(File f) { if (f != null && f.exists()) { String fileNa ...