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**/

E. Anton and Permutation
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.



0 0
原创粉丝点击