#404 (div2)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.

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 }

时间: 2024-10-07 05:22:31

#404 (div2)Anton and Permutation (分块处理)的相关文章

Codeforces 785 E. Anton and Permutation(分块,树状数组)

Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求将索引为l,r的两个数交换位置,并给出交换后数组中的逆序对数. 思路:此题用到了分块的思想,即将这组数分为bsz块,在每一块上建Fenwick树,对于每次查询,只需要处理l,r中间的块和l,r所在块受影响的部分.具体实现见代码及注释. #include<iostream> #include<

codeforces785E Anton and Permutation

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! 题目链接:codeforces785E 正解:分块 解题报告: 考虑每次的交换,只会影响到$[l,r]$这个区间的数,那么分块维护每次的$update$操作就好了,又暴力又好写. //It is made by ljh2000 //有志者,事竟成,破釜沉舟

CodeForces 785E Anton and Permutation

分块,暴力. 将序列分成$sqrt(n)$块,每块$sqrt(n)$个元素,每块内排序. 每次操作要计算这个区间中比$a[p1]$大的有几个,小的有几个,比$a[p2]$大的有几个,小的有几个,端点的块内暴力找,中间的块内二分找. 交换完数字之后,可以直接重新$sort$排个序. 总体时间复杂度$O(m*log(sqrt(n))*sqrt(n))$. #include <cstdio> #include <cmath> #include <cstring> #inclu

[CF785E]Anton and Permutation

题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数. 题解:发现交换位置为$l,r$的数后,逆序对的变化只和区间$(l,r)$内的数与$s_l,s_r$的大小关系有关,设$S_i$表示区间$(l,r)$中比$s_i$小的数,$B_i$表示区间$(l,r)$中比$s_i$大的数,$ans'=ans+S_r-B_r-S_l+B_l$.设$len=r-l

CF785CAnton 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

【Codeforces Round #404 (Div. 2)】题解

A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 1000 #define LL long long using namespace std; char s[10000]; int main() { int n; LL sum=0; scanf("%d&qu

#404 (div2)Anton and School - 2

As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (w

Codeforces Round #404 (Div. 2) C 二分,水 D 数学,好题 E 分块

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 题意:仓库容量n,每天运来m粮食,第 i 天被吃 i 粮食,问第几天仓库第一次空掉. tags:==SB题 注:二分边界判断,数据范围爆long long判断. // CF404 C #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000&

CF #404 (Div. 2) D. Anton and School - 2 (数论+范德蒙恒等式)

题意:给你一个由'('和')'组成的字符串,问你有多少个子串,前半部分是由'('组成后半部分由')'组成 思路:枚举这个字符串中的所有'('左括号,它左边的所有'('左括号的个数为num1,它的右边的所有')'右括号的个数为num2, 根据范德蒙恒等式计算得出 代码: #include <bits/stdc++.h> #define ll long long #define maxn 200000 #define mod 1000000007 using namespace std; ll j