Codeforces 707E Garlands

Garlands

我怎么感觉好水啊。

因为询问只有2000组, 离线询问, 枚举联通块再枚举询问, 二维树状数组更新答案。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 2000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

int n, m, k, q, cntq;
LL ans[N];
pair<PII, PII> qus[N];
bool on[N], can[N][N];
vector<pair<PII, int>> vc[N];
char op[10];

struct Bit {
    LL a[N][N];
    void modify(int x, int y, int v) {
        for(int i = x; i < N; i += i & -i)
            for(int j = y; j < N; j += j & -j)
                a[i][j] += v;
    }
    LL sum(int x, int y) {
        LL ans = 0;
        for(int i = x; i; i -= i & -i)
            for(int j = y; j; j -= j & -j)
                ans += a[i][j];
        return ans;
    }
    LL query(int x1, int y1, int x2, int y2) {
        return sum(x2, y2) - sum(x2, y1 - 1) - sum(x1 - 1, y2) + sum(x1 - 1, y1 - 1);
    }
} bit;

int main() {
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= k; i++) {
        int len; scanf("%d", &len);
        for(int j = 1; j <= len; j++) {
            int x, y, w;
            scanf("%d%d%d", &x, &y, &w);
            vc[i].push_back(mk(mk(x, y), w));
        }
    }
    memset(on, true, sizeof(on));
    scanf("%d", &q);
    while(q--) {
        scanf("%s", op);
        if(op[0] == 'A') {
            cntq++;
            scanf("%d%d", &qus[cntq].fi.fi, &qus[cntq].fi.se);
            scanf("%d%d", &qus[cntq].se.fi, &qus[cntq].se.se);
            memcpy(can[cntq], on, sizeof(on));
        } else {
            int x;
            scanf("%d", &x);
            on[x] = !on[x];
        }
    }
    for(int i = 1; i <= k; i++) {
        for(auto& t : vc[i]) bit.modify(t.fi.fi, t.fi.se, t.se);
        for(int j = 1; j <= cntq; j++) {
            if(!can[j][i]) continue;
            ans[j] += bit.query(qus[j].fi.fi, qus[j].fi.se, qus[j].se.fi, qus[j].se.se);
        }
        for(auto& t : vc[i]) bit.modify(t.fi.fi, t.fi.se, -t.se);
    }
    for(int i = 1; i <= cntq; i++) printf("%lld
", ans[i]);
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/10486919.html