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

#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. ES6知识点大汇总

    1 //1.搭建ES6的开发环境: 2 let a=1; 3 console.log(a); 4 //2.var 声明的是全局变量 全局变量会污染外部的区块 5 //let 局部变量 6 //cons ...

  2. IntelliJ IDEA使用心得之插件篇

    今天和大家分享下插件的安装方法,顺便推荐几个非常好用的插件. 1.安装插件 在工具栏中,点击进入IDE设置界面. 插件仓库界面: 值得注意的是,每次安装/卸载插件后,需要重启IDE. 2.好用的插件 ...

  3. php读取文件内容的三种方法

    <?php //**************第一种读取方式***************************** 代码如下: header("content-type:text/h ...

  4. javamail+ical4j发送会议提醒

    本篇讲述小编在使用ical4j时对其的理解与使用,留作笔记的同时希望能帮助到大家! 初学者可以先了解下ical4j的基本信息: iCalender编程基础,了解与使用ical4j:https://ww ...

  5. WPF ----在UserControl的xaml里绑定依赖属性

    场景:在定义wpf 用户控件的时候,希望使用时设置自定义的属性来改变用户控件里的状态或内容等. 下面直接上实例代码: 用户控件的后台代码,定义依赖属性 public partial class MyU ...

  6. PHP判断SQL语句是否合法:mysqli_error()

    假设现在有条update语句,有时候update语句正确,但是受影响的行数是0. 那么怎么判断这条SQL语句到底是否正确?使用 mysqli_error($Conn); create table us ...

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

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

  8. PHP中一种sign计算方法

    一言不合上代码......... <?php function getsign($data,$key){ $key=MD5("KEY_".$key."_K" ...

  9. Invoke 与 BeginInvoke 应用场景

    1.委托中 Invoke , BeginInvoke 特点 Invoke  : 同步调用 , 委托在当前线程执行 BeginInvoke : 异步调用 , 通常使用线程池资源执行委托. 2. UI  ...

  10. Yaf框架的配置

    http://www.laruence.com/manual/yaf.ini.html //先看一下惠新宸鸟哥yaf官网的配置说明 我们可以在php.ini中定义开发环节配置项,把本地开发设置成dev ...