【题目链接】 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. org.apache.hadoop.hdfs.server.datanode.DataNode: Exception in receiveBlock for block

    Hbase依赖的datanode日志中如果出现如下报错信息:DataXceiverjava.io.EOFException: INFO org.apache.hadoop.hdfs.server.da ...

  2. IDEA新建时选项没有java class问题

    解决办法: 点击你的project F4打开project stucture 点击左边的module 点击右边的source后添加src就可以了  添加的src就是源码可以放置的地址

  3. 2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)

    Description Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dime ...

  4. 最短路算法详解(Dijkstra,Floyd)

    最短路径 在一个无权的图中,若从一个顶点到另一个顶点存在着一条路径,则称该路径长度为该路径上所经过的边的数目,它等于该路径上的顶点数减1.由于从一个顶点到另一个顶点可能存在着多条路径,每条路径上所经过 ...

  5. algorithm ch2 insertsort

    刚开始看到insertsort,思路就是使用新来的元素与前述已经排好序的元素比较.然后进行插入或者跳到下一次比较. 实现的代码如下: void InsertSort(int *pArray, int ...

  6. HDU2819(二分图匹配,记录过程)

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. Download PuTTY: latest development snapshot

    Download PuTTY: latest development snapshot https://www.chiark.greenend.org.uk/~sgtatham/putty/lates ...

  8. SuSE Linux10.1 网络设置以及和主机通信

    SuSE Linux10.1 网络设置以及和主机通信 http://www.cnblogs.com/kevintian/articles/1086994.html 在VMWare上安装好SuSE之后, ...

  9. Basic-Paxos原理

    Basic-Paxos //参考paxos made sample 核心是一致性协议算法. 问题 假定一个集合中的提案者可以产生一项决议. 一致性算法保证在提案者中只有一个提案可以形成决议. 如果没有 ...

  10. 【洛谷P3708】Koishi的数学题

    可以很显然的看出分块的性质…… 看不出来的打个表也能看出来. 然后就是随手做做就行了. #include<bits/stdc++.h> #define N 1000005 typedef ...