[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. linux系统文件

    关于linux系统文件命令 (1)Linux的文件系统目录配置要遵循FHS规范,规范定义的两级目录规范如下:        /home  每个账号在该目录下都有一个文件夹,进行数据的管理        ...

  2.  Meltdown论文翻译【转】

    转自:http://www.wowotech.net/basic_subject/meltdown.html#6596 摘要(Abstract) The security of computer sy ...

  3. Python3 item系列

    一.前言 #在python中一切皆对象 ''' 创建了一个dict实例-->dic就是dict的实例对象 我们通过dic['k1']可以得到k1所对应的值 那么我们自定义一个类,可不可以使用对象 ...

  4. 【本地服务器】用nodejs搭建最简单、轻量化的http server

    1. 引言 前端程序猿主要关注的是页面,你可能根本就用不到.net,java,php等后台语言. 但是你制作出来的网页总要运行.总要测试吧?——那就免不了用到http server.我先前都是用vis ...

  5. Win10搜索不能用

    使用win10进行搜索时,一直显示win10特色的滚动条,但就是检索不出东西,我的主要是检索不到windows的内容: (个人感觉使用win10检索网页内容不太专业,就关闭了Web搜索) 最后有发现网 ...

  6. 客户端使用less方法

    <link rel="stylesheet/less" type="text/css" href="/css/style.less"& ...

  7. (二)Mybatis项目配置

    第一节:environments Mybatis支持多个环境,可以任意配置 <environments default="development"> <envir ...

  8. gtk+学习笔记(六)

    今天用到了滚动窗口和微调按钮,根据网上的信息,简单总结下用法. 滚动窗口只能添加一个控件到其中 scrolled=gtk_scrolled_window_new(NULL,NULL); /*创建滚动窗 ...

  9. HBase 入门笔记-数据落地篇

    一.前言 关于数据落地方面,HBase官网也有相关介绍.本文主要介绍一下实际工作中涉及的数据存储方面的一些经验和技巧,主要涉及表rowkey设计.数据落地方案 二.表设计 相对于MySQL等关系型数据 ...

  10. SSIS 学习之旅 FTP文件传输-脚本任务

    这一章主要讲解一下用脚本怎么把CSV文件抛送到FTP服务器上 设计:   通过Demon库的Users表数据生成CSV文件.   生成后的CSV文件抛送到FTP指定目录下. 控件的使用这里就不做详细讲 ...