【题目链接】 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. [lucene系列笔记3]用socket把lucene做成一个web服务

    上一篇介绍了用lucene建立索引和搜索,但是那些都只是在本机上运行的,如果希望在服务器上做成web服务该怎么办呢? 一个有效的方法就是用socket通信,这样可以实现后端与前端的独立,也就是不管前端 ...

  2. tengine的安装

    tengine的安装参考此博文: http://www.cnblogs.com/zlslch/p/6035145.html (1)下载tengine的压缩包 (2)解压缩 (3)进入目录./confi ...

  3. iBatis之Iterator的使用

    一:前言 现在这个项目使用的是iBatis,我刚刚开始的时候说是用MyBatis,因为我以前用过,觉得还是比较好用的啊,而且不像iBatis样,查什么一个字段不能多也不能少,觉得好无语啊. 二:内容 ...

  4. 网络(bzoj 4538)

    Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有服 ...

  5. 【STSRM12】夏令营(分治决策单调+主席树)

    [题意]n个数字分成k段,每一段的价值是段内不同数字的个数,求最大价值.n<=35000,k<=50. [算法]分治决策单调+主席树(可持久化线段树) [题解] f[i][j]表示前i天分 ...

  6. MySql binlog(理论篇)

    1.什么是binlog? binlog日志用于记录所有更新了数据的sql语句或保存被修改的记录Row: 有了binlog,可以用于实时备份,master/slave主从同步: 在5.0版本前支持文本格 ...

  7. python 读 excel 模块: xlrd

    主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python ...

  8. HDU 4320 Arcane Numbers 1 (数论)

    A - Arcane Numbers 1 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  9. linux下源码安装netcat

    linux下源码安装netcat http://blog.chinaunix.net/uid-20783755-id-4211230.html 1,下载netcat源码,netcat-0.7.1-13 ...

  10. golang的各种打印

    golang的打印方法太特么多了,下面就来区分一下之间的差异 package main import ( "fmt" "os" ) func main() { ...