Anton and Permutation
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.
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 q lines. The i-th line of the output is the number of inversions in the Anton's permutation after the i-th operation.
5 4
4 5
2 4
2 5
2 2
1
4
3
3
2 1
2 1
1
6 7
1 4
3 5
2 3
3 3
3 6
2 1
5 1
5
6
7
7
10
11
8
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+10; const int N=2e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;} int n,m,k,t,s[maxn*20*20+N*20],ls[maxn*20*20+N*20],rs[maxn*20*20+N*20],root[N*20],lx[20],rx[20],rt[maxn<<2],pos[maxn<<2],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>>1; ls[y]=ls[x],rs[y]=rs[x]; if(z<=mid)insert(l,mid,ls[x],ls[y],z,k,t); else insert(mid+1,r,rs[x],rs[y],z,k,t); } void add(int x,int y,int z) { while(y<=n) { insert(1,n,rt[y],rt[y],x,z,1); y+=y&(-y); } } int ask_more(int x,int y,int z) { int l=1,r=n,ret=0,i; while(l!=r) { int mid=l+r>>1; if(x<=mid) { rep(i,1,lx[0])ret-=s[rs[lx[i]]],lx[i]=ls[lx[i]]; rep(i,1,rx[0])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,1,lx[0])lx[i]=rs[lx[i]]; rep(i,1,rx[0])rx[i]=rs[rx[i]]; y=rs[y],z=rs[z]; l=mid+1; } } return ret; } int gao(int x,int l,int r) { int ret=0,_l=l,_r=r; lx[0]=rx[0]=0; while(l)lx[++lx[0]]=rt[l],l-=l&(-l); while(r)rx[++rx[0]]=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,1,n)pos[i]=i,insert(1,n,root[i-1],root[i],i,1,0); 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]-1,pos[b]),y=gao(b,pos[a]-1,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],-1); add(b,pos[b],-1); add(a,pos[b],1); add(b,pos[a],1); swap(pos[a],pos[b]); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步