[CC-CHEFINV]Chef and Swaps

题目大意:

长度为\(n(n\le2\times10^5)\)的数列,\(q(q\le2\times10^5)\)次询问,每次问交换\(A_x\)和\(A_y\)后逆序对个数。询问互相独立。

思路:

一开始先把逆序对求好,然后用主席树计算交换对答案的影响即可。

时间复杂度\(\mathcal O((n+q)\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=2e5+1,logN=20;
int n,m,a[N];
std::vector<int> v;
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p) {
for(;p<=m;p+=lowbit(p)) val[p]++;
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) ret+=val[p];
return ret;
}
};
FenwickTree bit;
class FotileTree {
#define mid ((b+e)>>1)
private:
struct Node {
int val,left,right;
};
Node node[N*logN];
int sz,new_node(const int &p) {
node[++sz]=node[p];
return sz;
}
public:
int root[N];
void insert(int &p,const int &b,const int &e,const int &x) {
p=new_node(p);
node[p].val++;
if(b==e) return;
if(x<=mid) insert(node[p].left,b,mid,x);
if(x>mid) insert(node[p].right,mid+1,e,x);
}
int query(const int &p,const int &q,const int &b,const int &e,const int &l,const int &r) const {
if(b==l&&e==r) return node[q].val-node[p].val;
int ret=0;
if(l<=mid) ret+=query(node[p].left,node[q].left,b,mid,l,std::min(mid,r));
if(r>mid) ret+=query(node[p].right,node[q].right,mid+1,e,std::max(mid+1,l),r);
return ret;
}
#undef mid
};
FotileTree t;
int main() {
n=getint();
const int q=getint();
for(register int i=1;i<=n;i++) {
a[i]=getint();
v.push_back(a[i]);
}
std::sort(v.begin(),v.end());
v.resize(m=std::unique(v.begin(),v.end())-v.begin());
for(register int i=1;i<=n;i++) {
a[i]=std::lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;
}
int64 tot=0;
for(register int i=n;i>=1;i--) {
tot+=bit.query(a[i]-1);
bit.modify(a[i]);
}
for(register int i=1;i<=n;i++) {
t.insert(t.root[i]=t.root[i-1],1,m,a[i]);
}
for(register int i=0;i<q;i++) {
int x=getint(),y=getint();
if(x>y) std::swap(x,y);
const int tmp1=a[x]!=1?t.query(t.root[x],t.root[y],1,m,1,a[x]-1):0;
const int tmp2=a[x]!=m?t.query(t.root[x],t.root[y],1,m,a[x]+1,m):0;
const int tmp3=a[y]!=1?t.query(t.root[x],t.root[y],1,m,1,a[y]-1):0;
const int tmp4=a[y]!=m?t.query(t.root[x],t.root[y],1,m,a[y]+1,m):0;
printf("%lld\n",tot-tmp1+tmp2+tmp3-tmp4);
}
return 0;
}

[CC-CHEFINV]Chef and Swaps的更多相关文章

  1. 跳跳棋(9018_1563)(BZOJ_2144)

    题目: Hzwer的跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 某一天,黄金大神和cjy用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.他们要 ...

  2. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  3. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

    https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...

  4. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

    https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...

  5. CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

    https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...

  6. 【CodeChef】Chef and Graph Queries

    Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...

  7. codechef AUG17 T1 Chef and Rainbow Array

    Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...

  8. CodeForces1006D-Two Strings Swaps

    D. Two Strings Swaps time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Atitti.dw cc 2015 绿色版本安装总结

    Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...

随机推荐

  1. 对web标准的理解,以及对w3c组织的认识

    (1)web标准规范要求,书写标签必须闭合.标签小写.不乱嵌套,可提高搜索机器人对网页内容的搜索几率.--- SEO(2)建议使用外链css和js脚本,从而达到结构与行为.结构与表现的分离,提高页面的 ...

  2. solr4.10.3部署到tomcat——(十)

    0. 准备环境:

  3. sql____001

    题目: create table my_001 (id int,value int); insert into my_001 values(1,10): insert into my_001 valu ...

  4. socket相关系统调用的调用流程

    最近一直在读内核网络协议栈源码,这里以ipv4/tcp为例对socket相关系统调用的流程做一个简要整理,这些相关系统调用的内部细节虽然各有不同,但其调用流程则基本一致: 调用流程: (1)系统调用 ...

  5. centos7连接阿里云长时间连接不上

    一.手动修改网卡配置 手上有几台centos7的linux,当连接阿里云的ecs服务器时候长时间连接不上,最后失败的问题. 使用 -vvv参数到如下语句就卡着不动了 ssh -vvv XXX.XXX. ...

  6. 洛谷P1525关押罪犯

    传送门啦 想让最大值最小,所以,这题可以用二分法,排序之后发现可以并查集,因为要使最大值最小,排序后这个最大值是存在的. 对于会冲突的两个罪犯,我们连一条无向边,然后按权值从大到小排序,从大到小枚举每 ...

  7. (三)HttpClient 抓取图片

    第一节: HttpClient 抓取图片 这里pom.xml需要用到io输入输出: <dependency> <groupId>commons-io</groupId&g ...

  8. C/C++经典面试题

    1.指向数组的指针 和 指向数组首元素的指针 2018-03-07 昨天在牛客上看到这么一道C语言面试题,挺经典的,特来分享给大家. 程序如下,问输出结果 #include <stdio.h&g ...

  9. drools7 (二、agenda-group 的使用)

    几个关键点: 1. 如果没有指定agenda-group 则默认把所有未指定agenda-group的 rules 都执行一遍 2. 如果指定了agenda-group 使用的时候必须指定该name才 ...

  10. require demo 记录备份

    预览地址 http://127.0.0.1:8020/requireDemo/myNEW/index.html 注意 远程的 非模块的 empty: demo2