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. mvc中使用uploadify批量上传的应用

    网上找了很多资料都没有发现一个好用.可以用的uploadify批量上传的应用,这里通过官方和自己的一些项目需要整理了一个出来. 希望能帮助到需要的人. 效果图:

  2. [Algorithms] Longest Common Substring

    The Longest Common Substring (LCS) problem is as follows: Given two strings s and t, find the length ...

  3. css 中的事件冒泡

    css伪类中的表现类似于事件冒泡的,举个例子,当你滑过一个元素时,他会认为你也滑过了该元素的父元素,即使该元素看起来并没有包含在父元素里面,此处以:hover例子: 效果图: 滑过前: 滑过后: CS ...

  4. 160628、利用Oracle rownum让表排序字段值连续

    利用Oracle rownum让表排序字段值连续 1.需求说明 表(eval_index)中有字段如下: 表字段 描述 说明 ID 主键 GROUP_ID 分组编号 SORT_NUM 排序序号 按照分 ...

  5. Intellij IDEA Ultimate Edition 14.1 破解

    key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7B4EW-U12L4 (2) key:huangwei value:97493-G3A41-0SO24-W57LI-Y2 ...

  6. ORACLE的测试用户Scott

    Oracle数据库的测试用户Scott的密码为什么是Tiger? 1977年6月,Larry Ellison 与 Bob Miner 和 Ed Oates 在硅谷共同创办了一家名为软件开发实验室(So ...

  7. easyui的datagrid无数据时下方滚动条不显示的解决办法(标题栏显示不完全)

    easyui在写datagrid的时候标题栏有时候因为太多.太长所以无法显示所有的列,而且没数据的时候下方的滚动条是不显示的,这样就无法显示所有的列了.解决办法如下: onLoadSuccess: f ...

  8. Howto: Performance Benchmarks a Webserver

    Howto: Performance Benchmarks a Webserver last updated June 9, 2006 in CategoriesApache, FreeBSD, Ho ...

  9. 2015-03-22——js常用其它方法

    Function Function.prototype.method = function (name, func) {    this.prototype[name] = func;  //此时th ...

  10. django之多表查询与创建

    https://www.cnblogs.com/liuqingzheng/articles/9499252.html # 一对多新增数据 添加一本北京出版社出版的书 第一种方式 ret=Book.ob ...