主席树 其实暴力二维树状还更快

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2005; int N,M,K,Q;
struct Node{
int x,y,w;
Node(int a=0, int b=0, int c=0):x(a),y(b),w(c){}
bool operator < (Node T) {
if(x != T.x) return x < T.x;
else return y < T.y;
}
};
struct Ask{
int x1,y1,x2,y2; int ty;
}Do[1000005];
ll ans[1000005]; int vis[MAXN];
vector<Node> gar[MAXN];
vector<int> has[MAXN]; int root[MAXN];
struct Pode{
int ls,rs; ll sum;
}tree[MAXN*30];
int tot;
int Update(int pos,int num,int l,int r,int pre){
int rt = ++tot;
tree[rt] = tree[pre];
tree[rt].sum += num;
if(l == r) return rt;
int m = (l+r)>>1;
if(pos <= m) tree[rt].ls = Update(pos,num,l,m,tree[pre].ls);
else tree[rt].rs = Update(pos,num,m+1,r,tree[pre].rs);
return rt;
}
ll Query(int L,int R,int l,int r,int rt) {
if(!rt) return 0;
if(L <= l && r <= R) return tree[rt].sum;
int m = (l+r) >>1;
ll ans = 0;
if(L <= m) ans += Query(L,R,l,m,tree[rt].ls);
if(R > m) ans += Query(L,R,m+1,r,tree[rt].rs);
return ans;
} int main(){
while(~scanf("%d %d %d",&N,&M,&K)) {
memset(ans,0,sizeof(ans));
for(int i = 1; i <= K; ++i) gar[i].clear(), has[i].clear(); for(int i = 1; i <= K; ++i) {
vis[i] = 1;
int a; scanf("%d",&a);
for(int j = 0; j < a; ++j) {
int b,c,d; scanf("%d %d %d",&b,&c,&d);
gar[i].push_back(Node(b,c,d));
}
sort(gar[i].begin(), gar[i].end());
} scanf("%d",&Q);
for(int i = 1; i <= Q; ++i) {
char a[10]; scanf("%s",a);
if(a[0] == 'S') {
int b; scanf("%d",&b); Do[i].ty = 2;
vis[b] ^= 1;
}else {
scanf("%d %d %d %d",&Do[i].x1,&Do[i].y1,&Do[i].x2,&Do[i].y2); Do[i].ty = 1;
for(int j = 1; j <= K; ++j) {
if(vis[j]) {
has[j].push_back(i);
}
}
}
} tree[0].ls = tree[0].rs = tree[0].sum = 0;
for(int i = 1; i <= K; ++i) {
tot = 0; root[0] = 0;
for(int j = 0; j < (int)gar[i].size(); ++j) {
root[j+1] = Update(gar[i][j].y, gar[i][j].w, 1,M,root[j]);
}
for(int j = 0; j < (int)has[i].size(); ++j) {
int tt = has[i][j];
int x1 = Do[tt].x1; int y1 = Do[tt].y1; int x2 = Do[tt].x2; int y2 = Do[tt].y2; Node t1 = Node(x2,M+1);
int pos = lower_bound(gar[i].begin(), gar[i].end(), t1)-gar[i].begin();
if(pos) ans[tt] += Query(y1,y2,1,M,root[pos]); Node t2 = Node(x1,0);
int _pos = lower_bound(gar[i].begin(), gar[i].end(), t2)-gar[i].begin();
if(_pos) ans[tt] -= Query(y1,y2,1,M,root[_pos]);
}
}
for(int i = 1; i <= Q; ++i) {
if(Do[i].ty == 1) printf("%lld\n",ans[i]);
}
}
return 0;
}

CF368 E - Garlands的更多相关文章

  1. Codeforces 707E Garlands

    Garlands 我怎么感觉好水啊. 因为询问只有2000组, 离线询问, 枚举联通块再枚举询问, 二维树状数组更新答案. #include<bits/stdc++.h> #define ...

  2. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  3. Three Garlands~Educational Codeforces Round 35

    C. Three Garlands time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Garlands

    题意: n个数分成m段,每段偶数个数,最小化和最大段的半个区间的数字和. 分析: 先想到了二分,dp求能分成的最小段数. #include <map> #include <set&g ...

  5. Codeforces 707 E. Garlands (二维树状数组)

    题目链接:http://codeforces.com/problemset/problem/707/E 给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值. 有q个操作,操作ask问 ...

  6. CF368 D - Persistent Bookcase

    re了20多发 还是我在测试数据上操作最后了10多发才发现的 其实只需要多加一句就好了 真的愚蠢啊,要不都能进前100了 #include<bits/stdc++.h> using nam ...

  7. Garlands CodeForces - 707E (离线树状数组)

    大意: 给定n*m矩阵, k条链, 链上每个点有权值, 每次操作可以关闭或打开一条链或询问一个子矩阵内未关闭的权值和. 关键询问操作比较少, 可以枚举每条链, 暴力算出该条链对每个询问的贡献. 最后再 ...

  8. [CF911C]Three Garlands

    题目大意: 给你三个灯,分别以k1秒一次,k2秒一次和k3秒一次的频率闪烁着. 你可以自定义三个灯开启的时间,问是否有一种方案,使得max(k1,k2,k3)秒之后,每秒钟都至少有一盏灯闪烁. 思路: ...

  9. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

随机推荐

  1. BZOJ 4407: 于神之怒加强版 [莫比乌斯反演 线性筛]

    题意:提前给出\(k\),求\(\sum\limits_{i=1}^n \sum\limits_{j=1}^m gcd(i,j)^k\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d|D ...

  2. WPF: 深入理解 Weak Event 模型

    在之前写的一篇文章(XAML: 自定义控件中事件处理的最佳实践)中,我们曾提到了在 .NET 中如果事件没有反注册,将会引起内存泄露.这主要是因为当事件源会对事件监听者产生一个强引用,导致事件监听者无 ...

  3. [Python Study Notes]字典操作

    字典操作 a.增加 >>> info["stu1104"] = "abc" >>> info {'stu1102': 'x5 ...

  4. CENTOS6.6下mysql MMM架构搭建

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn MMM(Master-Master replication mana ...

  5. linux 下yum使用技巧

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 经常会遇上一些linux系统允许你上外网,而一些是不允许的,这时我们 ...

  6. 统计输入的汉字,数字,英文,other数量

    主要用正则表达式在完成对汉字,数字,英文数量的验证. import java.util.Scanner; /* * 统计汉字,数字,英文,other * */ public class Test { ...

  7. javascript selenium全套教程发布

    为什么有这个系列 目前javascript生态非常丰富,越来越多的人开始用js去做前端的ui测试了.而selenium是web ui测试的标准解决方案,所以一套js的selenium教程是很有必要的. ...

  8. 微信小程序(一)

    开发流程 注册微信小程序并申请微信支付-->制作小程序-->上传并提交审核-->审核通过,小程序上线   开发微信小程序需要准备 企业公众号(被认证)以及申请小程序.微信开发技术.S ...

  9. 在SpringBoot中配置定时任务

    前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...

  10. SIFT解析(一)建立高斯金字塔

    SIFT(Scale-Invariant Feature Transform,尺度不变特征转换)在目标识别.图像配准领域具有广泛的应用,下面按照SIFT特征的算法流程对其进行简要介绍对SIFT特征做简 ...