【题目链接】 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. [BZOJ3829][Poi2014]FarmCraft 贪心

    这个题应该是很容易想到贪心的,只要可是怎么贪才是科学的呢?我们分析一下题干,对于每个边只能一进一出因此,对于树上的一棵子树,我们只要一进子树就必须遍历完,因此我们只能进行一遍 dfs() 然后我们发现 ...

  2. 淡入淡出效果的js原生实现

    淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透 ...

  3. nginx proxy_buffer_size 解决后端服务传输数据过多,其实是header过大的问题

    nginx proxy_buffer_size 解决后端服务传输数据过多,其实是header过大的问题 这三个参数已设置就搞定了额 proxy_buffer_size 64k; proxy_buffe ...

  4. linux基础——关于chmod用户权限和文件的相关操作

    第一部分:1) 新建用户natasha,uid为1007,gid为555,备注信息为“master” 操作:useradd natasha新建natasha:修改uid是,usermod -u 100 ...

  5. Python 数据库连接池DButils

    常规的数据库链接存在的问题: 场景一: 缺点:每次请求反复创建数据库连接,连接数太多 import pymysql def index(): conn = pymysql.connect() curs ...

  6. [Leetcode Week6]Linked List Cycle

    Linked List Cycle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/linked-list-cycle/description/ Des ...

  7. linux内核分析笔记----中断和中断处理程序【转】

    转自:http://www.cnblogs.com/hanyan225/archive/2011/07/17/2108609.html 中断还是中断,我讲了很多次的中断了,今天还是要讲中断,为啥呢?因 ...

  8. 爬取genome的网页和图片

    # -*- coding: utf-8 -*- # @Time : 2018/03/08 10:32 # @Author : cxa # @File : gethtmlandimg.py # @Sof ...

  9. 【反演复习计划】【bzoj3994】约数个数和

    首先要用数学归纳证明一个结论,不过因为我实在是懒得打公式了... 先发代码吧. #include<bits/stdc++.h> #define N 50005 using namespac ...

  10. js面向对象编程(一):封装(转载)

    一. 生成对象的原始模式 假定我们把猫看成一个对象,它有"名字"和"颜色"两个属性. var Cat = { name : '', color : '' } 现 ...