【题目链接】 http://codeforces.com/contest/785/problem/E

【题目大意】

  一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数

【题解】

  我们将序列进行分块。每块维护一个权值树状数组表示各个数字的情况,
  每次交换两个数之后我们计算前后贡献值进行加减,计算答案。

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=200010,M=650;
int n,q,l,r,x,y,arr[N],st[M],en[M],c[M][N],block[N];
void update(int k,int x,int val){while(x<N)c[k][x]+=val,x+=x&-x;}
int query(int k,int x){int s=0;while(x)s+=c[k][x],x-=x&-x;return s;}
long long ans=0;
int query(int l,int r,int x){
int res=0;
for(int i=block[l];i<=block[r];i++){
if(st[i]>=l&&en[i]<=r)res+=query(i,x);
else for(int j=max(st[i],l);j<=min(en[i],r);j++)res+=(arr[j]<=x);
}return res;
}
void cal(int x,int y){
ans-=x-1-query(1,x-1,arr[x]);
ans-=query(x+1,n,arr[x]-1);
update(block[x],y,1);
update(block[x],arr[x],-1);
arr[x]=y;
ans+=x-1-query(1,x-1,arr[x]);
ans+=query(x+1,n,arr[x]-1);
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)arr[i]=i;
for(int i=1,cur=0;i<=n;){
int j=i;
st[++cur]=i;
while(j<=n&&j<i+M){
block[j]=cur;
update(cur,arr[j++],1);
}en[cur]=j-1; i=j;
}
while(q--){
scanf("%d%d",&l,&r);
if(l!=r){
int t=arr[l];
x=l; y=arr[r]; cal(x,y);
x=r; y=t; cal(x,y);
}printf("%lld\n",ans);
}return 0;
}

Codeforces 785E Anton and Permutation(分块)的更多相关文章

  1. CodeForces 785E Anton and Permutation 分块

    题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqr ...

  2. Codeforces 785E. Anton and Permutation

    题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...

  3. Codeforces 785 E. Anton and Permutation(分块,树状数组)

    Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...

  4. Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)

    E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...

  5. Codeforces785E - Anton and Permutation

    Portal Description 对一个长度为\(n(n\leq2\times10^5)\)的数列\(a\)进行\(m(m\leq5\times10^4)\)次操作,数列初始时为\(\{1,2,. ...

  6. Anton and Permutation

    Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input standa ...

  7. 【codeforces 785E】Anton and Permutation

    [题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. Codeforces 593B Anton and Lines

    LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...

随机推荐

  1. 【NOIP 模拟赛】中值滤波 打表找规律

    对于这样看起来不像什么算法也没什么知识点的题,一脸懵逼的话不是手推规律就是打表找规律......... 当然还有一些超出你能力之外的数学题...... #include <cstdio> ...

  2. [BZOJ2090/2089] [Poi2010]Monotonicity 2/Monotonicity 树状数组优化dp

    这个dp乍看不科学,仔细一看更不科学,所以作为一个执着BOY,我决定要造数据卡死波兰人民,但是我造着造着就......证出来了......... 这个就是把 < > =分开讨论每次找到f[ ...

  3. 收藏一个漂亮的Flash焦点图切换

    网上闲逛的时候发现一个Flash焦点图效果,跟喜欢,然后就下载回来,收集在这里,以便以后方便取用.这个Flash使用方法也是相当简单的,如果你喜欢,也可以从这里查看源代码下载. Flash 焦点图效果 ...

  4. Codeforces Round #525 (Div. 2)B. Ehab and subtraction

    B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...

  5. POJ2912:Rochambeau(带权并查集)

    Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5208   Accepted: 1778 题目链接:h ...

  6. 怎么利用idea自带的工具,不需要 重启tomcat或则其他服务,js代码自动生效

    idea中有一个工具:可以直接upload,能让你修改的界面直接可以看到,不需要重启服务. 依次点击的按钮如下: 点击进入的界面这个填的只是一个示例,在各位的电脑上肯定不行,大家依据实际情况填写.

  7. bzoj 5094 [Lydsy1711月赛]硬盘检测 概率dp

    [Lydsy1711月赛]硬盘检测 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 273  Solved: 75[Submit][Status][Dis ...

  8. 转:RBAC权限控制

    名词解释: RBAC:Role-Based Access Control,基于角色的访问控制   关键词: RBAC,Java Shiro,Spring Security,   一. RBAC 要解决 ...

  9. mavne问题解决---Dynamic Web Module 2.3 or newer

    一:前沿 maven问题的bug,其实是很烦人的,因为每次都是很纠结的去改这个bug,特别的烦人,这个bug也是使得我纠结了好久的,那个星期五自己搞了几个小时都没有解决下,之后星期一来百度Google ...

  10. Bzoj4197 寿司晚宴

    Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同 ...