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 ;
}

最新文章

  1. android:ems的作用
  2. Spring、Spring MVC、MyBatis整合文件配置详解
  3. 【leetcode】Excel Sheet Column Title
  4. MUI - 上拉刷新/下拉加载
  5. charles 结合mocky 模拟数据
  6. Tomcat部署方式
  7. Grid画边框
  8. oracle系列--第二篇 oracle下载
  9. poj 1383 Labyrinth
  10. node.js的npm详解
  11. 启动php-fpm时报错
  12. Nginx分时段限制下载速度解决方案(原创)_于堡舰_新浪博客
  13. Vivado完成综合_实现_生成比特流后发出提醒声音-原创☺
  14. 【最短路】 ZOJ 1544 Currency Exchange 推断负圈
  15. 分布式代码管理系统Git实践
  16. MySQL之视图、触发器、事务、存储过程、函数
  17. 数据标记系列——标记工具Imagtagger
  18. dwSun带你选Python的编辑器/IDE
  19. 安装haproxy和haproxy命令
  20. 《剑指offer》-二叉搜索树与双向链表

热门文章

  1. 软RAID管理
  2. JS连续滚动幻灯片:原理与实现
  3. Akka源码分析-Remote-网络链接生命周期
  4. Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???
  5. python自动化测试学习笔记-5常用模块
  6. Spring Web MVC核心架构
  7. 大数据~说说Hadoop
  8. MYSQL创建用户和授权方法(测试mysql5.7)
  9. Java中 == 和 equals()
  10. [转]五个Linux下用户空间的调试工具