2141: Queue
Time Limit: 4 Sec Memory Limit: 259 MBSubmit: 2506 Solved: 982
[ Submit ][ Status ][ Discuss ]
Description
Sitting in rows, eating fruit, the fruit is sweet and long, and everyone laughs. One for you, one for me, the big one will be given to you, and the small one for me. After eating Guoguo, you will sing a song, and everyone will be happy and harmonious. The children of Hongxing Kindergarten lined up in a long line, ready to eat fruit. However, because the heights of the children are different, the ranks of the children are disordered, which is extremely unsightly. Let the height of the i-th child be hi, and we define the disorder degree of a sequence as: the number of (i, j) satisfying ihj. The kindergarten aunt will select two children each time and exchange their positions. Please help to calculate the clutter of the sequence after each exchange. In order to facilitate the statistics of the kindergarten aunt, you should also output the clutter of the sequence when no swap operation is performed.
Input
The first line is a positive integer n, indicating the number of children; the second line contains n positive integers h1, h2, ..., hn separated by spaces, which in turn indicate the height of the children in the initial queue; the third line is a positive integer m, Indicates the number of swap operations; each of the following m lines contains two positive integers ai and bi¬, representing the children who exchange positions ai and bi.
Output
The output file has m lines in total, and a positive integer in the i-th line indicates the disorder degree of the sequence after the exchange operation i is completed.
Sample Input
3
130 150 140
2
2 3
1 3
Sample Output
0
3
[Example description]
When no operation is performed, (2,3) satisfies the condition;
after operation 1 ends, the sequence is 130 140 150, and there is no (i,j) pair that satisfies ihj;
after operation 2 ends, The sequence is 150 140 130, (1,2), (1,3), (2,3), a total of 3 pairs of (i,j) that satisfy the conditions.
[Data scale and convention]
For 100% data, 1≤m≤2*103, 1≤n≤2*104, 1≤hi≤109, ai≠bi, 1≤ai, bi≤n.
HINT
Source
#include <bits/stdc++.h> #define pb push_back using namespace std; const int N = 20010; int n, m; int bits[N]; int ma[N]; int cp[N];///每个块排序后储存的数组 int block; int num; int bo[N]; int bl[N]; int br[N]; int ans; int len; vector<int> id; int getid(int x) { return lower_bound(id.begin(), id.end(), x) - id.begin() + 1; } int lowbit(int x) { return x & -x; } int quary(int x) { int ans = 0; while(x > 0){ ans += bits[x]; x -= lowbit(x); } return ans; } void update(int x, int v) { while(x <= len){ bits[x] += v; x += lowbit(x); } } void build() { block = sqrt(n); num = n / block; if(n % block){ num ++; } for(int i = 1; i <= n; i ++){ bo[i] = (i - 1) / block + 1; } for(int i = 1; i <= num; i ++){ bl[i] = (i - 1) * block + 1; br[i] = i * block; } br[num] = n; for(int i = 1; i <= num; i ++){ for(int j = bl[i]; j <= br[i]; j ++){ cp[j] = ma[j]; } sort(cp + bl[i], cp + br[i] + 1); } } void quary_block(int l, int r, int v, int t) ///交换之前就计算,比ma[l]小的ans-- 比 ma[r]小的ans++ 这样就能把两个合并在一起 { int x = bo[l]; int y = bo[r]; if(x == y){ for(int i = l; i <= r; i ++){ if(ma[i] < v){ ans -= t; } else if(ma[i] > v){ ans += t; } } } else{ for(int i = l; i <= br[x]; i ++){ if(ma[i] < v){ ans -= t; } else if(ma[i] > v){ ans += t; } } for(int i = x + 1; i < y; i ++){ ans -= t * (lower_bound(cp + bl[i], cp + br[i] + 1, v) - cp - bl[i]); ans += t * ((br[i] - bl[i] + 1) - (upper_bound(cp + bl[i], cp + br[i] + 1, v) - cp - bl[i])); } for(int i = bl[y]; i <= r; i ++){ if(ma[i] < v){ ans -= t; } else if(ma[i] > v){ ans += t; } } } } void exchange(int l, int r) { if(l > r){ return ; } int t; int opx; int opy; int x = bo[l]; int y = bo[r]; if(ma[l] < ma[r]){ ans ++; } else if(ma[l] > ma[r]){ ans --; } swap(ma[l], ma[r]); opx = lower_bound(cp + bl[x], cp + br[x] + 1, ma[l]) - cp; opy = lower_bound(cp + bl[y], cp + br[y] + 1, ma[r]) - cp; cp[opx] = ma[r]; cp[opy] = ma[l]; t = opx; while(t - 1 >= bl[x] && cp[t - 1] > cp[t]){ swap(cp[t], cp[t - 1]); t --; } while(t <= br[x] && cp[t + 1] < cp[t]){ swap(cp[t], cp[t + 1]); t ++; } t = opy; while(t - 1 >= bl[y] && cp[t - 1] > cp[t]){ swap(cp[t], cp[t - 1]); t --; } while(t <= br[y] && cp[t + 1] < cp[t]){ swap(cp[t], cp[t + 1]); t ++; } } int main(int argc, char const *argv[]) { while(scanf("%d", &n) == 1){ ans = 0; memset(bits, 0, sizeof(bits)); for(int i = 1; i <= n; i ++){ scanf("%d", &ma[i]); id.pb(ma[i]); } sort(id.begin(), id.end()); id.erase(unique(id.begin(), id.end()), id.end()); len = id.size(); for(int i = 1; i <= n; i ++){ update(getid(ma[i]), 1); ans += i - quary(getid(ma[i])); } printf("%d\n", ans); scanf("%d", &m); for(int i = 0; i < m; i ++){ int l, r; scanf("%d%d", &l, &r); if(r < l){ swap(r,l); } quary_block(l + 1, r - 1, ma[l], 1); quary_block(l + 1, r - 1, ma[r], -1); exchange(l, r); printf("%d\n", ans); } } return 0; }