BZOJ 2141 排队 分块

来源:互联网 发布:linux 时间写入硬件 编辑:程序博客网 时间:2024/04/29 13:35

分块大法好,大力出奇迹!

先离散个化。lower_bound最近很少用了。。

既然静态可以用树状数组,那么我们分块,对每个块开个树状数组,保存块内的元素。

对于每个查询(x,y),可以确定内部包含的块,然后直接查询块的树状数组即可,x往右和y往左的一小段没有被包在块中的由于总数不超过O(sqrt(n)),所以暴力就好啦。

对于每个询问,O(sqrt(n)logn)。

#include <cstdio>#include <cmath>#include <algorithm>#define FOR(i,j,k) for(i=j;i<=k;i++)const int M = 20001;using namespace std;int n, a[M], h[M], d[M];struct BIT {    int c[M];    void add(int x, int v) {        for (; x <= n; x += x & -x) c[x] += v;    }    int sum(int x) {        int s = 0;        for (; x; x -= x & -x) s += c[x];        return s;    }    int sum(int x, int y) {        return sum(y) - sum(x - 1);    }} pre;template<typename T, int N>struct Block {    T a[N];    int block_size;    T &operator [] (int x) { return a[id(x)]; }    T &getByBlockId(int x) { return a[x]; }    int id(int x) { return (x - 1) / block_size; }    int begin(int blockId) { return blockId * block_size + 1; }    int end(int blockId) { return (blockId + 1) * block_size; }};Block<BIT, 200> block;void inversion_pair(int a[], int j, int x, int y, int &ans) {    if(a[j] < a[x]) ans--;    if(a[j] > a[x]) ans++;    if(a[j] < a[y]) ans++;    if(a[j] > a[y]) ans--;}bool cmp(int a, int b) { return h[a] < h[b]; }int main() {    int i, j, m, sum = 0, x, y, ans = 0;    scanf("%d", &n);    FOR(i,1,n) scanf("%d", &h[i]), d[i] = i;    sort(d + 1, d + n + 1, cmp);    FOR(i,1,n) {        if(h[d[i]] != h[d[i - 1]]) sum++;        a[d[i]] = sum;    }    for(i = n; i; i--) ans += pre.sum(a[i] - 1), pre.add(a[i], 1);    block.block_size = (int) (sqrt(n) + 1e-7);    FOR(i,1,n) block[i].add(a[i], 1);    printf("%d\n", ans);    scanf("%d", &m);    FOR(i,1,m) {        scanf("%d%d", &x, &y);        if (x > y) swap(x, y);        int x_id = block.id(x) + 1, y_id = block.id(y) - 1;        if (x_id <= y_id) {            FOR(j, x_id , y_id) {                BIT &bit = block.getByBlockId(j);                ans = ans - bit.sum(a[x] - 1) + bit.sum(a[x] + 1, n);                ans = ans + bit.sum(a[y] - 1) - bit.sum(a[y] + 1, n);            }            FOR(j, x + 1, block.begin(x_id) - 1) inversion_pair(a, j, x, y, ans);            FOR(j, block.end(y_id) + 1, y - 1) inversion_pair(a, j, x, y, ans);        } else            FOR(j, x + 1, y - 1) inversion_pair(a, j, x, y, ans);        if (a[x] < a[y]) ++ans;        else if(a[x] > a[y]) --ans;        printf("%d\n", ans);        block[x].add(a[x], -1); block[y].add(a[y], -1);        swap(a[x], a[y]);        block[x].add(a[x], 1); block[y].add(a[y], 1);    }    return 0;}



2141: 排队

Time Limit: 4 Sec  Memory Limit: 259 MB
Submit: 1022  Solved: 400
[Submit][Status][Discuss]

Description

排排坐,吃果果,生果甜嗦嗦,大家笑呵呵。你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和。红星幼儿园的小朋友们排起了长长地队伍,准备吃果果。不过因为小朋友们的身高有所区别,排成的队伍高低错乱,极不美观。设第i个小朋友的身高为hi,我们定义一个序列的杂乱程度为:满足ihj的(i,j)数量。幼儿园阿姨每次会选出两个小朋友,交换他们的位置,请你帮忙计算出每次交换后,序列的杂乱程度。为方便幼儿园阿姨统计,在未进行任何交换操作时,你也应该输出该序列的杂乱程度。

Input

第一行为一个正整数n,表示小朋友的数量;第二行包含n个由空格分隔的正整数h1,h2,…,hn,依次表示初始队列中小朋友的身高;第三行为一个正整数m,表示交换操作的次数;以下m行每行包含两个正整数ai和bi¬,表示交换位置ai与位置bi的小朋友。

Output

输出文件共m行,第i行一个正整数表示交换操作i结束后,序列的杂乱程度。

Sample Input

【样例输入】
3
130 150 140
2
2 3
1 3

Sample Output

1
0
3
【样例说明】
未进行任何操作时,(2,3)满足条件;
操作1结束后,序列为130 140 150,不存在满足ihj的(i,j)对;
操作2结束后,序列为150 140 130,(1,2),(1,3),(2,3)共3对满足条件的(i,j)。
【数据规模和约定】
对于100%的数据,1≤m≤2*103,1≤n≤2*104,1≤hi≤109,ai≠bi,1≤ai,bi≤n。

0 0
原创粉丝点击