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 44 52 42 52 2
Output
1433
Input
2 12 1
Output
1
Input
6 71 43 52 33 33 62 15 1
Output
567710118
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.
题意:给出一个初始值为1~n序列,然后给出q对 i 和 j ,将序列中第 i 个元素和第 j 个元素进行交换,打印每次交换之后的逆序对数
思路:每次交换之后他的逆序对数的变化之于 i 和 j 之间的变化有关,所以能够使用分块进行处理。从来都不知道这个算法怎么用的,就学习了一下别人的代码,顺便填充一下自己的知识空白。
知识点:关于分块处理的伪模板
1 const int maxn=200005; 2 int data[maxn],l[maxn],r[maxn],belong[maxn]; 3 vector<int> v[maxn]; 4 void build(int n) 5 { 6 int num=sqrt(n); 7 int block = n / num; 8 if(n % num) 9 block++; 10 for(int i = 1;i <= block; i++) { 11 l[i] = (i - 1) * num + 1; 12 r[i] = i * num; 13 } 14 for(int i = 1;i <= n;i++) { 15 belong[i] = (i - 1)/ num + 1; 16 } 17 for(int i = 1;i <= block; i++) { 18 for(int j = l[i];j <= r[i] ;j++) 19 v[i].push_back(j); 20 } 21 return ; 22 } 23 long long query(int ll,int rr,int now) { 24 if(ll>rr) return 0; 25 long long ans=0; 26 if(belong[ll]==belong[rr]) { 27 for(int i=ll;i <= rr;i++) 28 if(data[i] < now) 29 ans++; 30 } 31 else { 32 for(int i = ll;i <= r[belong[ll]];i++) 33 if(data[i] < now) 34 ans++; 35 for(int i = belong[ll] + 1;i < belong[rr];i++) { 36 int pos = upper_bound(v[i].begin(),v[i].end(),now)-v[i].begin(); 37 ans += pos; 38 } 39 for(int i=l[belong[rr]];i <= rr;i++) 40 if(data[i] < now) 41 ans++; 42 } 43 return ans; 44 } 45 void updata(int x,int y) { 46 int t=belong[x]; 47 v[t].erase(lower_bound(v[t].begin(),v[t].end(),data[x])); 48 v[t].insert(upper_bound(v[t].begin(),v[t].end(),data[y]),data[y]); 49 t=belong[y]; 50 v[t].erase(lower_bound(v[t].begin(),v[t].end(),data[y])); 51 v[t].insert(upper_bound(v[t].begin(),v[t].end(),data[x]),data[x]); 52 swap(data[x],data[y]); 53 }
AC代码:
1 #include <iostream> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int maxn=200005; 5 int data[maxn],l[maxn],r[maxn],belong[maxn]; 6 vector<int> v[maxn]; 7 void build(int n) 8 { 9 int num=sqrt(n); 10 int block = n / num; 11 if(n % num) 12 block++; 13 for(int i = 1;i <= block; i++) { 14 l[i] = (i - 1) * num + 1; 15 r[i] = i * num; 16 } 17 for(int i = 1;i <= n;i++) { 18 belong[i] = (i - 1)/ num + 1; 19 } 20 for(int i = 1;i <= block; i++) { 21 for(int j = l[i];j <= r[i] ;j++) 22 v[i].push_back(j); 23 } 24 return ; 25 } 26 long long query(int ll,int rr,int now) { 27 if(ll>rr) return 0; 28 long long ans=0; 29 if(belong[ll]==belong[rr]) { 30 for(int i=ll;i <= rr;i++) 31 if(data[i] < now) 32 ans++; 33 } 34 else { 35 for(int i = ll;i <= r[belong[ll]];i++) 36 if(data[i] < now) 37 ans++; 38 for(int i = belong[ll] + 1;i < belong[rr];i++) { 39 int pos = upper_bound(v[i].begin(),v[i].end(),now)-v[i].begin(); 40 ans += pos; 41 } 42 for(int i=l[belong[rr]];i <= rr;i++) 43 if(data[i] < now) 44 ans++; 45 } 46 return ans; 47 } 48 void updata(int x,int y) { 49 int t=belong[x]; 50 v[t].erase(lower_bound(v[t].begin(),v[t].end(),data[x])); 51 v[t].insert(upper_bound(v[t].begin(),v[t].end(),data[y]),data[y]); 52 t=belong[y]; 53 v[t].erase(lower_bound(v[t].begin(),v[t].end(),data[y])); 54 v[t].insert(upper_bound(v[t].begin(),v[t].end(),data[x]),data[x]); 55 swap(data[x],data[y]); 56 } 57 58 int main(){ 59 int n,q; 60 while(~scanf("%d%d",&n,&q)) { 61 for (int i = 1; i <= n; i++ ) { 62 data[i]=i; 63 } 64 build(n); 65 long long ans=0; 66 int x,y; 67 while(q--) { 68 scanf("%d%d",&x,&y); 69 if(x > y) 70 swap(x,y); 71 if(x==y) 72 printf("%lld\n",ans); 73 else { 74 long long sub = query(x + 1,y - 1,data[x]); 75 long long add = y - 1 - x - sub; 76 ans -= sub; 77 ans += add; 78 add = query(x + 1,y - 1,data[y]); 79 sub = y - 1 -x - add; 80 ans -= sub; 81 ans += add; 82 if(data[x] < data[y]) 83 ans++; 84 else 85 ans--; 86 printf("%lld\n",ans); 87 updata(x,y); 88 } 89 } 90 } 91 }