Codeforces Round #404 (Div. 2) -- E. Anton and Permutation(分块xjb 搞)
来源:互联网 发布:云数据库语言 编辑:程序博客网 时间:2024/05/21 18:07
题意:
给你n 个数,一开始是1,2,3,,n。 给你q 个操作,每个操作可以交换两个数,每次操作输出逆序对数?
思路:
这种区间用线段树,树状数组不好搞的 区间xjb询问,xjb修改的 就用分块好了
把n 个数分成根号n 个区间,每个区间有根号n 个数。
假如 交换a 位置和b位置(假设a < b),那么只需要统计 a+1 到b-1 这个区间内 比a位置的数 大的有几个 小的有几个, 比b位置的数 大的有几个 小的有几个即可、
这个操作 可以二分每一块。
因为刚开始肯定排好序的,用插入排序会很快了。 800多ms就过了。
#include <bits/stdc++.h>#define ps push_back#define Siz(x) (int)x.size()using namespace std;typedef long long LL;LL ans = 0LL;const int maxn = 200000 + 7;int n,q;int num;int block;int L[maxn], R[maxn];int a[maxn];int belong[maxn];vector<int> bit[maxn];void init(){ block = sqrt(n); num = n / block; if (n % block) ++num; for (int i = 1; i <= n; ++i){ L[i] = (i-1)*block + 1; R[i] = i*block; } R[num] = n; for (int i = 1; i <= n; ++i){ belong[i] = (i-1)/block + 1; } for (int i = 1; i <= num; ++i){ for (int j = L[i]; j <= R[i]; ++j){ bit[i].ps(j); } }}int query(int l,int r,int v){// printf("v = %d\n",v); if (l > r) return 0; int ans = 0; if (belong[l] == belong[r]){ for (int i = l; i <= r; ++i){ if (a[i] < v)++ans; } return ans; }// print(); int id = belong[l]; for (int i = l; i <= R[id]; ++i){ if (a[i] < v)++ans;// printf("******** %d\n",a[i]); } for (int i = belong[l]+1; i <= belong[r]-1; ++i){ int p2 = lower_bound(bit[i].begin(),bit[i].end(),v) - bit[i].begin(); ans += p2;// printf("^^^ %d\n",p2); } id = belong[r]; for (int i = L[id]; i <= r; ++i){ if (a[i] < v)++ans; } return ans;}void update(int l,int r){ int uu = a[l]; int vv = a[r]; int id = belong[l]; bit[id].erase(lower_bound(bit[id].begin(),bit[id].end(),uu)); bit[id].insert(upper_bound(bit[id].begin(),bit[id].end(),vv),vv); id = belong[r]; bit[id].erase(lower_bound(bit[id].begin(),bit[id].end(),vv)); bit[id].insert(upper_bound(bit[id].begin(),bit[id].end(),uu),uu); swap(a[l],a[r]);}int main(){ scanf("%d %d",&n, &q); for (int i = 1; i <= n; ++i) a[i] = i; init(); while(q--){ int u,v; scanf("%d %d",&u, &v); if (u == v){ printf("%lld\n",ans); continue; } if (u > v) swap(u,v); int t1 = query(u+1,v-1,a[u]); int t2 = v-1-u-1+1-t1;// printf("%d %d\n",t1,t2); ans -= t1; ans += t2; t1 = query(u+1,v-1,a[v]); t2 = v-1-u-1+1-t1; ans += t1; ans -= t2; if (a[u] < a[v])++ans; else --ans; printf("%lld\n",ans); update(u,v); } return 0;}/**5 44 52 42 52 2**/
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 44 52 42 52 2
1433
2 12 1
1
6 71 43 52 33 33 62 15 1
567710118
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.
- Codeforces Round #404 (Div. 2) -- E. Anton and Permutation(分块xjb 搞)
- Codeforces Round #404 (Div. 2)E. Anton and Permutation(分块)
- Codeforces Round #404 (Div. 2):E. Anton and Permutation(分块)
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(分块+二分)
- CodeForces 785E Anton and Permutation 分块
- codeforces 785 E. Anton and Permutation(分块)
- CodeForces 785E Anton and Permutation (分块)
- Codeforces Round #309 (Div. 2) D - Kyoya and Permutation ,和dp相关的xjb搞的题
- [排列组合]Codeforces Round #324 (Div. 2)E - Anton and Ira
- Codeforces Round #404 (Div. 2)(D)Anton and School
- Codefores 785E Anton and Permutation(分块)
- Codeforces Round #324 (Div. 2) E. Anton and Ira(贪心)
- Codeforces Round #404 (Div. 2) D. Anton and School
- Codeforces Round #404(Div. 2)D. Anton and School
- Codeforces Round #404 (Div. 2) D. Anton and School
- Codeforces Round #404 (Div. 2) A. Anton and Polyhedrons
- Codeforces Round #404 (Div. 2) B. Anton and Classes
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
- string库之模拟实现strlen与memset
- Android UI-SlidingMenu侧滑菜单效果
- 【C++】:拷贝构造函数
- 外观模式
- 【J2EE】JDBC+JSP+Servlet
- Codeforces Round #404 (Div. 2) -- E. Anton and Permutation(分块xjb 搞)
- Codeforces Round #404 (Div. 2) 题解
- Java核心技术:卷一——前言
- windows 下编译ffmpeg
- string库之模拟实现strchr、strrchr与memchr
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- tar解压失败:gzip: stdin: not in gzip format
- 【codeforces 785A】Anton and Polyhedrons
- 【codeforces 785B】Anton and Classes