[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. CodeForces 1096E: The Top Scorer

    一道经典组合数学+容斥题. 题目传送门:CF1096E. 题意简述: \(p\) 个人,每个人有得分 \(a_i\). 总得分 \(\sum a_i = s\). 第一个人得分 \(a_1 \ge r ...

  2. discuz过滤词语无效

    1.是由于一些特殊的字导致serialize序列化错误.过滤词语在表 common_word中,序列化房子common_syscache的censor中,看看是否有特殊符号.

  3. Sublime2编译Python程序EOFError:EOF when reading a line解决方法【转】

    在Sublime2中编译运行Python文件时,如果代码中包含用户输入的函数时(eg. raw_input()),Ctrl+b编译运行之后会提示以下错误: 解决方法:安装SublimeREPL打开Su ...

  4. Linux学习笔记-文件处理和权限命令

    目录 文件处理命令 touch cat tac more less head tail 链接命令 ln 权限命令 chmod 权限管理命令 chown chgrp umask 文件处理命令 touch ...

  5. poj1077

    题意:给出一个八数码问题,求解法,不可解则输出unsolvable. 分析:可以用ida*算法,估价函数可以使用每个数码到其最终位置的最短距离之和.对于不可解的判断,我这里用迭代深度大于100时判定为 ...

  6. Python 正则表达式提高

    re模块的高级用法 search re.search(pattern, string[, flags]) ​ 若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果 ...

  7. 分别使用docx4j,jacob将文字与图片插入word中书签位置

    项目中需要将一段文字,与人员的签名(图片)插入到上传的word中,上网查询了一下,有许多种方式可以向word中插入文字,发现docx4j与jacob都为比较常见的解决方案,于是就先使用的docx4j进 ...

  8. java容器---Comparable & Comparator

    1.接口Comparable<T> API    参数类型:T ---可以与此对象进行比较的那些对象的类型 此接口强行对实现它的每个类的对象进行整体排序.这种排序被称为类的自然排序,类的c ...

  9. SQL Server 2000 系统存储过程

    SQL Server 2000 系统存储过程 在 Microsoft? SQL Server? 中,许多管理和信息活动可以通过系统存储过程执行.系统存储过程按这些分类分组. 分类 描述 Active ...

  10. js中的Object.seal()与Object.freeze()

    关键字:seal, freeze, property descriptor. 1.Object.seal() 参考文档(2)中这样描述: The Object.seal() method seals ...