CF368 E - Garlands
主席树 其实暴力二维树状还更快
#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的更多相关文章
- Codeforces 707E Garlands
Garlands 我怎么感觉好水啊. 因为询问只有2000组, 离线询问, 枚举联通块再枚举询问, 二维树状数组更新答案. #include<bits/stdc++.h> #define ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Three Garlands~Educational Codeforces Round 35
C. Three Garlands time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Garlands
题意: n个数分成m段,每段偶数个数,最小化和最大段的半个区间的数字和. 分析: 先想到了二分,dp求能分成的最小段数. #include <map> #include <set&g ...
- Codeforces 707 E. Garlands (二维树状数组)
题目链接:http://codeforces.com/problemset/problem/707/E 给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值. 有q个操作,操作ask问 ...
- CF368 D - Persistent Bookcase
re了20多发 还是我在测试数据上操作最后了10多发才发现的 其实只需要多加一句就好了 真的愚蠢啊,要不都能进前100了 #include<bits/stdc++.h> using nam ...
- Garlands CodeForces - 707E (离线树状数组)
大意: 给定n*m矩阵, k条链, 链上每个点有权值, 每次操作可以关闭或打开一条链或询问一个子矩阵内未关闭的权值和. 关键询问操作比较少, 可以枚举每条链, 暴力算出该条链对每个询问的贡献. 最后再 ...
- [CF911C]Three Garlands
题目大意: 给你三个灯,分别以k1秒一次,k2秒一次和k3秒一次的频率闪烁着. 你可以自定义三个灯开启的时间,问是否有一种方案,使得max(k1,k2,k3)秒之后,每秒钟都至少有一盏灯闪烁. 思路: ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
随机推荐
- JdbcTemplate的使用
NamedParameterJdbcTemplate中包含了一个JdbcTemplate,NamedParameterJdbcTemplate中的很多方法实际上还是交由JdbcTemplate去完成. ...
- HDU [P1281]棋盘游戏
二分图求最大匹配 我们以每一个格子为边,以行和列为两个集合,那么求二分图的最大匹配数就是最多能放车的数目,那么什么是重要点呢?就是删掉后会影响最大匹配数的匹配边. 我们求出最大匹配数后,枚举匹配边,将 ...
- bzoj 4871: [Shoi2017]摧毁“树状图” [树形DP]
4871: [Shoi2017]摧毁"树状图" 题意:一颗无向树,选两条边不重复的路径,删去选择的点和路径剩下一些cc,求最多cc数. update 5.1 : 刚刚发现bzoj上 ...
- linux使用i/o内存访问外设
一.linux中访问外设的方法. 1.IO端口(IO port) linux系统给外设分配不同的端口号,linux利用端口号来访问设备(驱动) (cpu x86) cpu访问外设通过端号,访问通过地址 ...
- gitlab10.0安装手记
+ +exec chpst -e /opt/gitlab/etc/gitlab-workhorse/env -P \ + -U git \ + -u git \ + /opt/gitlab/embed ...
- IQKeyboardManager 自动处理键盘事件的第三方库
我们写界面要考虑很多用户体验问题,键盘事件的响应就是比较麻烦的一种.我们需要监听键盘事件,考虑点击背景收起键盘.考虑键盘遮挡输入框问题等等,而且每个界面都要做这么一套.这个库帮我们解决了这个事情. 这 ...
- win7下通过easyBCD引导安装Ubuntu16.04(并处理遇到的坑)
Ubuntu16.04作为目前最新版本的ubuntu系统,相信很多人都想在自己的电脑上安装一下,然而系统的安装方法各式各样,u盘法.grub引导法等等,这里我将介绍在win7系统下用easyBCD软件 ...
- 【ASP.NET Core】解决“The required antiforgery cookie "xxx" is not present”的错误
当你在页面上用 form post 内容时,可能会遇到以下异常: The required antiforgery cookie "????????" is not present ...
- 使用Netbeans内置的Git工具
在 NetBeans IDE 中使用 Git 支持 NetBeans IDE 为 Git 版本控制客户端提供支持.通过利用 IDE 的 Git 支持,您可以从 IDE 内的项目中直接执行版本控制任务. ...
- windows 下 Symfony的下载与安装
初始化项目 本篇教程我尽量按照Windows/*nix都可以运行的方式来讲解. 得益于Symfony installer,我们目前可以很方便的初始化一个Symfony2项目.不过首先,你得有一个Sym ...