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 ...
随机推荐
- 大数据技术大合集:Hadoop家族、Cloudera系列、spark、storm【转】
大数据我们都知道hadoop,可是还会各种各样的技术进入我们的视野:Spark,Storm,impala,让我们都反映不过来.为了能够更好 的架构大数据项目,这里整理一下,供技术人员,项目经理,架构师 ...
- box-sizing与calc()与flex
1,Syntax: /* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ ...
- 【BZOJ4896】[Thu Summer Camp2016]补退选 Trie树
[BZOJ4896][Thu Summer Camp2016]补退选 Description X是T大的一名老师,每年他都要教授许多学生基础的C++知识.在T大,每个学生在每学期的开学前都需要选课,每 ...
- java如何遍历当月所有的日期?
转自:https://zhidao.baidu.com/question/360215274.html import java.util.ArrayList;import java.util.Cale ...
- Leetcode-Populating Next Right Pointer in Binary Tree II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Canvas-三角函数曲线图
以本图为例,要做这张图,需要一些数学知识(三角函数sin,cos),有canvas的基础知识 Html <!DOCTYPE html> <html> <head> ...
- python进程间数据不共享(示例演示)
import multiprocessing data_list = [] def task(arg): data_list.append(arg) print(data_list) def run( ...
- swift 值得学习的项目
http://www.php100.com/html/it/biancheng/2015/0112/8329.html
- Spring的IoC模式
1.依赖 依赖就是有联系,有地方使用到它就是有依赖它,一个系统不可能完全避免依赖.如果你的一个类或者模块在项目中没有用到它,恭喜你,可以从项目中剔除它或者排除它了,因为没有一个地方会依赖它.下面看一个 ...
- xxxservlet继承HttpServlet类
"HttpServlet类被定义为抽象类,但是源码里面没有抽象方法.所以没有一定要求实现的方法.之所以定义为抽象类,是因为他继承了GenericServlet这个抽象类.并没有全部实现里面的 ...