Anton and Permutation
time limit per test

4 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers{a1, a2, ..., an}, in which every number from 1 to n appears exactly once.

One day Anton got a new permutation and started to play with it. He does the following operation q times: he takes two elements of the permutation and swaps these elements. After each operation he asks his friend Vanya, how many inversions there are in the new permutation. The number of inversions in a permutation is the number of distinct pairs (i, j) such that 1 ≤ i < j ≤ n and ai > aj.

Vanya is tired of answering Anton's silly questions. So he asked you to write a program that would answer these questions instead of him.

Initially Anton's permutation was {1, 2, ..., n}, that is ai = i for all i such that 1 ≤ i ≤ n.

Input

The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 50 000) — the length of the permutation and the number of operations that Anton does.

Each of the following q lines of the input contains two integers li and ri (1 ≤ li, ri ≤ n) — the indices of elements that Anton swaps during the i-th operation. Note that indices of elements that Anton swaps during the i-th operation can coincide. Elements in the permutation are numbered starting with one.

Output

Output q lines. The i-th line of the output is the number of inversions in the Anton's permutation after the i-th operation.

Examples
input
5 4
4 5
2 4
2 5
2 2
output
1
4
3
3
input
2 1
2 1
output
1
input
6 7
1 4
3 5
2 3
3 3
3 6
2 1
5 1
output
5
6
7
7
10
11
8
Note

Consider the first sample.

After the first Anton's operation the permutation will be {1, 2, 3, 5, 4}. There is only one inversion in it: (4, 5).

After the second Anton's operation the permutation will be {1, 5, 3, 2, 4}. There are four inversions: (2, 3), (2, 4), (2, 5) and (3, 4).

After the third Anton's operation the permutation will be {1, 4, 3, 2, 5}. There are three inversions: (2, 3), (2, 4) and (3, 4).

After the fourth Anton's operation the permutation doesn't change, so there are still three inversions.

分析:主席树套树状数组,注意开始静态建树,防止爆内存;

   分块法待学,orz~

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=5e4+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,s[maxn**+N*],ls[maxn**+N*],rs[maxn**+N*],root[N*],lx[],rx[],rt[maxn<<],pos[maxn<<],sz;
ll ret;
void insert(int l,int r,int x,int &y,int z,int k,int t)
{
if(!t)y=++sz;
else if(t&&!y)y=++sz;
s[y]=s[x]+k;
if(l==r)return;
int mid=l+r>>;
ls[y]=ls[x],rs[y]=rs[x];
if(z<=mid)insert(l,mid,ls[x],ls[y],z,k,t);
else insert(mid+,r,rs[x],rs[y],z,k,t);
}
void add(int x,int y,int z)
{
while(y<=n)
{
insert(,n,rt[y],rt[y],x,z,);
y+=y&(-y);
}
}
int ask_more(int x,int y,int z)
{
int l=,r=n,ret=,i;
while(l!=r)
{
int mid=l+r>>;
if(x<=mid)
{
rep(i,,lx[])ret-=s[rs[lx[i]]],lx[i]=ls[lx[i]];
rep(i,,rx[])ret+=s[rs[rx[i]]],rx[i]=ls[rx[i]];
ret-=s[rs[y]],y=ls[y];
ret+=s[rs[z]],z=ls[z];
r=mid;
}
else
{
rep(i,,lx[])lx[i]=rs[lx[i]];
rep(i,,rx[])rx[i]=rs[rx[i]];
y=rs[y],z=rs[z];
l=mid+;
}
}
return ret;
}
int gao(int x,int l,int r)
{
int ret=,_l=l,_r=r;
lx[]=rx[]=;
while(l)lx[++lx[]]=rt[l],l-=l&(-l);
while(r)rx[++rx[]]=rt[r],r-=r&(-r);
ret+=ask_more(x,root[_l],root[_r]);
return ret;
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)pos[i]=i,insert(,n,root[i-],root[i],i,,);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
{
printf("%lld\n",ret);
continue;
}
if(pos[a]>pos[b])swap(a,b);
int x=gao(a,pos[a]-,pos[b]),y=gao(b,pos[a]-,pos[b]);
ret+=x-(pos[b]-pos[a]-x);
ret-=y-(pos[b]-pos[a]-y);
if(a<b)ret--;
else ret++;
printf("%lld\n",ret);
add(a,pos[a],-);
add(b,pos[b],-);
add(a,pos[b],);
add(b,pos[a],);
swap(pos[a],pos[b]);
}
return ;
}

Anton and Permutation的更多相关文章

  1. Codeforces785E - Anton and Permutation

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

  2. 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 ...

  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 785E. Anton and Permutation

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

  5. [CF785E]Anton and Permutation

    题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数 ...

  6. Codeforces 785E Anton and Permutation(分块)

    [题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...

  7. CodeForces 785E Anton and Permutation 分块

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

  8. 【codeforces 785E】Anton and Permutation

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

  9. 题解 CF785E 【Anton and Permutation】

    考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...

随机推荐

  1. [Codeplus 2017] 晨跑

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5105 [算法] 答案为三个数的最小公倍数 [代码] #include<bits ...

  2. 同一个Tomcat下不同项目之间的session共享

    最近发现项目运行过程中经常会抛出一个 NullPointerException的异常,经检查发现异常出现的地方是日志模板,一阵检查,正常无误 (把所有记录日志的地方都点了一遍,心里是崩溃的),万念俱灰 ...

  3. 微信小程序之商品发布+编辑功能(多图片上传功能)

    小程序的商品发布页面:功能有多图片上传 遇到的问题记录一下:1.uploadFile成功之后返回的参数是json字符串,一定要用JSON.parse转换为object格式 2.因为商品发布和编辑都是在 ...

  4. RocketMQ(1)--helloworld

    双Master方式: 服务器环境 序号 IP 角色 模式 1 192.168.32.135 nameServer1,brokerServer1  Master1 2 192.168.32.136 na ...

  5. 完整版本的停车场管理系统源代码带服务端+手机android客户端

    该源码是停车场管理软件附带源代码 J2EE服务端+android客户端,也是一套停车场管理车辆进出的管理软,喜欢的朋友可以看看吧. 应用的后台管理主要功能介绍:1  机构管理 ,机构有从属管理< ...

  6. echarts交叉关系图二

    echarts关系图表,此图是坐标关系图,此图用的随机坐标,此图可以拖拽,更方便整理关系, 引入echarts.js就可以实现 代码: var graph={ //这是数据项目中一般都是获取到的 no ...

  7. 4.用Redis Desktop Manager连接Redis(Windows)

    相比连接CentOS的Redis,在Windows中的操作简单得让人感动. 所以这里我们使用的服务器系统是Windows Server 2016 R2. 而Windows版本的Redis官方网站并没有 ...

  8. 如何解决Win10预览版一闪而过的disksnapshot.exe进程?

    Win10之家讯上周微软如约向Insider用户推送了Win10预览版10576更新,本次更新修复了之前版本中存在的一些问题,从日常使用的情况来看,对比之前的预览版系统要更稳定了一些,但是还是存在一些 ...

  9. (转)Arcgis for JS之Cluster聚类分析的实现

    http://blog.csdn.net/gisshixisheng/article/details/40711075 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来的,包含 ...

  10. C/C++ 之dll注入

    #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h> ...