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 ...
随机推荐
- 2154: Crash的数字表格
2154: Crash的数字表格 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 3372 Solved: 1258[Submit][Status][ ...
- poj2046
Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1829 Accepted: 829 Description Le ...
- Oracle数据库 number 长度与 Short Integer Long BigDecimal 对应关系
转自:https://blog.csdn.net/edward9145/article/details/21398657 Oracle数据库 number 长度与 Short Integer Long ...
- (转)Linux-epoll
在Linux网络编程中,Linux内核2.6版本之前大多都是用 select() 作为非阻塞的事件触发模型,但是效率低,使用受限已经很明显的暴露了select()(包括poll)的缺陷,为了解决这些缺 ...
- 【Git和GitHub】学习笔记
1. 书籍推荐: 先看一本比较简单并且好的入门书籍 Git - Book https://git-scm.com/book/zh/v2 2. 书籍理解: Git 有三种状态,你的文件可能处于其中之一: ...
- 创建超小的Golang docker 镜像
原文: http://colobu.com/2015/10/12/create-minimal-golang-docker-images/ 本文对于创建超小的镜像非常有用 Docker是PaaS供应商 ...
- window异常处理——except_handler4以及栈展开分析
以前在15pb学习时候在看雪论坛发的一篇精华帖. 主要是分析在try块中发生嵌套异常时候堆栈是如何平衡的. 就不复制过来了,给个链接http://bbs.pediy.com/showthread.ph ...
- IO流入门-第七章-BufferedReader
BufferedReader基本用法和方法示例 /* 字节 BufferedInputStream BufferedOutputStream 字符 BufferedReader:带有缓冲区的字符输入流 ...
- Maven 整合SSH框架之pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 在VMW里安装Ghost操作系统遇到的问题及解决的办法
条件:Ghost系列系统镜像文件 遇到的问题:1.导入镜像文件时提示“无法检测此光盘映像中的操作系统”: 2.分区时提示“ezboot kernel not found” 解决办法:1.直接先创建一个 ...