bzoj1176: [Balkan2007]Mokia cdq

链接

bzoj

思路

cdq入门题,拆成4个矩阵,然后cdq。

代码

/**************************************************************
    Problem: 1176
    User: gryz2016
    Language: C++
    Result: Accepted
    Time:2652 ms
    Memory:13012 kb
****************************************************************/
 
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&-x)
const int N = 2e5 + 7;
int read() {
    int x = 0, f = 1; char s = getchar();
    for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
    for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
    return x * f;
}
int n, a[N], ans[N];
struct ask {
    int opt, x, y, w, id;
    ask(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) {
        opt = a, x = b, y = c, w = d, id = e;
    }
    bool operator < (const ask &b) const {
        return x == b.x ? opt < b.opt : x < b.x;
    }
} Q[N], tmp[N];
int lsh_y[N << 1];
namespace BIT {
    int sum[N], maxn;
    void add(int id, int w) {
        for (int i = id; i <= maxn; i += lowbit(i)) sum[i] += w;
    }
    int query(int x) {
        int ans = 0;
        for (int i = x; i >= 1; i -= lowbit(i)) ans += sum[i];
        return ans;
    }
}
void cdq(int l, int r) {
    if (l == r) return;
    int mid = (l + r) >> 1;
    cdq(l, mid), cdq(mid + 1, r);
    int p = l, q = mid + 1, js = l;
    while (p <= mid && q <= r) {
        if (Q[p] < Q[q]) {
            if (Q[p].opt == 1) BIT::add(Q[p].y, Q[p].w);
            tmp[js++] = Q[p++];
        } else {
            if (Q[q].opt == 2) ans[Q[q].id] += Q[q].w * BIT::query(Q[q].y);
            tmp[js++] = Q[q++];
        }
    }
    if (p <= mid) {
        for (int i = l; i < p; ++i) if (Q[i].opt == 1) BIT::add(Q[i].y, -Q[i].w);
        while (p <= mid) tmp[js++] = Q[p++];
    } else {
        while (q <= r) {
            if (Q[q].opt == 2) ans[Q[q].id] += Q[q].w * BIT::query(Q[q].y);
            tmp[js++] = Q[q++];
        }
        for (int i = l; i <= mid; ++i) if (Q[i].opt == 1) BIT::add(Q[i].y, -Q[i].w);
    }
    for (int i = l; i <= r; ++i) Q[i] = tmp[i];
}
int main() {
//  freopen("a.in", "r", stdin);
    int S = read(), W = read(), n = 0, DSR = 0;
    while (233) {
        int opt = read();
        if (opt == 3) break;
        if (opt == 1) {
            int x = read(), y = read(), w = read();
            Q[++n] = ask(opt, x, y, w), lsh_y[++lsh_y[0]] = Q[n].y;
        } else {
            ++DSR;
            int a = read(), b = read(), x = read(), y = read();
            if (x && y) Q[++n] = ask(opt, x, y, 1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
            if (a - 1 && b - 1) Q[++n] = ask(opt, a - 1, b - 1, 1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
            if (a - 1 && y) Q[++n] = ask(opt, a - 1, y, -1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
            if (x && b - 1) Q[++n] = ask(opt, x, b - 1, -1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
        }
    }
    sort(lsh_y + 1, lsh_y + 1 + lsh_y[0]);
    lsh_y[0] = unique(lsh_y + 1, lsh_y + 1 + lsh_y[0]) - lsh_y - 1;
    for (int i = 1; i <= n; ++i) Q[i].y = lower_bound(lsh_y + 1, lsh_y + 1 + lsh_y[0], Q[i].y) - lsh_y;
    BIT::maxn = lsh_y[0] + 1;
    cdq(1, n);
    for (int i = 1; i <= DSR; ++i) printf("%d\n", ans[i]);
    return 0;
}
全部评论

相关推荐

诨号无敌鸭:恭喜佬,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了
点赞 评论 收藏
分享
我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务