[CC-CHEFINV]Chef and Swaps
[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的更多相关文章
- 跳跳棋(9018_1563)(BZOJ_2144)
题目: Hzwer的跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 某一天,黄金大神和cjy用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.他们要 ...
- 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] 表示第 ...
- 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 ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake
https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...
- 【CodeChef】Chef and Graph Queries
Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...
- codechef AUG17 T1 Chef and Rainbow Array
Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...
- CodeForces1006D-Two Strings Swaps
D. Two Strings Swaps time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Atitti.dw cc 2015 绿色版本安装总结
Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...
随机推荐
- 2016.5.18——leetcode:Majority Element
Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...
- Python标准库笔记(8) — pprint模块
struct模块提供了用于在字节字符串和Python原生数据类型之间转换函数,比如数字和字符串. Python版本: 2.x & 3.x 该模块作用是完成Python数值和C语言结构体的Pyt ...
- C# WebClient进行FTP服务上传文件和下载文件
定义WebClient使用的操作类: 操作类名称WebUpDown WebClient上传文件至Ftp服务: //// <summary> /// WebClient上传文件至Ftp服务 ...
- Windows 10安装MongoDB(安装&启动)
Windows 10家庭中文版,MongoDB 3.6.3, 最近在学习Scrapy,可以却从未将scraped data存储到数据库中.在看过一些文档后,Scrapy会和MongoDB结合使用(还有 ...
- 多路复用IO与NIO
最近在学习NIO相关知识,发现需要掌握的知识点非常多,当做笔记记录就下. 在学NIO之前得先去了解IO模型 (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(No ...
- JS验证表单中TEXT文本框中是否含有非法字符
<form id="form" action="" method="post"> <input type="hi ...
- Oracle常用sql语句。
最近工作中遇到了一些与oracle相关的问题,稍微整理一些工作中用到的SQL语句 时间相关 --查询距当前时间十分钟内的数据 select sysdate -interval '10' minute ...
- MongoDB查询用法大全
转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...
- 利用Metrics+influxdb+grafana构建监控平台
https://blog.csdn.net/fishmai/article/details/51817429
- day6 SYS模块
SYS模块 用于提供对Python解释器相关的操作: (1)sys.argv 命令行参数List,第一个元素是程序本身路径 >>> sys.argv [' ...