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. bzoj3505 [Cqoi2014]数三角形——组合数+容斥

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3505 好题啊好题...好像还曾经出现在什么智力测试卷中来着...当时不会现在还是无法自己推出 ...

  2. 03_jni_helloworld_完成

    通过ndk-build编译C的代码.cd /d就是直接进到我的目录里面. 打开ANDROID-MK.HTML Introduction: This document describes the syn ...

  3. codeforces——数学

    codeforces 805A     http://codeforces.com/problemset/problem/805/A /* 题意:输入两个整数l,r,让你找一个因子 使得[l,r]里面 ...

  4. 5.3QBXT模拟赛

    出题人:钟惠兴 题目名称 讨厌整除的小明 吸血鬼 鱼的感恩 题目类型 传统型 传统型 传统型 题目目录/可执行文件名 ming vamp fool 输入文件名 ming.in vamp.in fool ...

  5. 洛谷P1341 无序字母对(欧拉回路)

    P1341 无序字母对 题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 ...

  6. [Swift通天遁地]四、网络和线程-(6)检测网络连接状态

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Akka源码分析-Remote-ActorSystem

    前面的文章都是基于local模式分析的,现在我们简要分析一下在remote模式下,ActorSystem的创建过程. final val ProviderClass: String = setup.g ...

  8. 涨知识 - II

    计算机网络相关 1.在无盘工作站向服务器申请IP地址时,使用的是(     )协议. A.ARP B.RARP C.ICMP D.IGMP 解析: ARP(地址解析协议)是设备通过自己知道的IP地址来 ...

  9. scala的枚举

    package com.test.scala.test /** * 枚举 */ object Enum extends Enumeration { val Red,Yellow,Green=Value ...

  10. 既可以输入也可以选择的input框

    datalist 是h5中既可以选择也可以输入的属性 具体代码如下 <input list="datalist"/><datalist id="data ...