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\n", ans[i]);
    return 0;
}

/*
*/
复制代码

 

posted @   NotNight  阅读(175)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示