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 ...
随机推荐
- 《深入理解Java虚拟机》学习笔记(二)
垃圾回收的前提是判断对象是否存活,对象不再存活时将会被回收,下面是2种判断的方法. 引用计数法: 主流的Java虚拟机并没有使用引用计数法来管理内存,重要的原因就是循环引用的问题难以解决. 可达性分析 ...
- 数据提交成功后如何避免alert被window.location.reload()影响
数据提交成功用alert提示,但页面立马就重载了 alert("提交成功!"); window.location.reload(); 如何避免? 方法一: setTimeout 延 ...
- win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程
操作系统:Microsoft Windows 7 旗舰版(32位) 数据库版本:SQL Server 2005 简体中文开发板 数据库下载链接: https://pan.baidu.com/s/1cq ...
- 一个客户端一键安装环境和服务的shell脚本
#!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...
- centos7 网桥的配置
centos7下配置网桥,两个步骤:1.新建网桥配置2.修改网卡配置 新建br0 网桥配置 在/etc/sysconfig/network-scripts/目录下新建ifcfg-br0,添加如下配置信 ...
- 关于DOM与BOM的总结
1.什么是BOM,什么是DOM(基本概念) BOM: Browers Object MOdel 浏览器对象模型 DOM: Document Object MOdel ...
- .Net Core和jexus配置HTTPS服务
花了几天时间,看了好多篇博客,终于搞定了网站的HTTPS服务,借此写篇博客,来让有需要的朋友少走弯路. 一.环境介绍 1.Linux下在Docker容器中部署好了一个网站,该网站需要通过外部提供程序访 ...
- JetBrains Rider 破解 (ideaIU等等开发工具都通用)2018-02-27
贴一下Rider下载地址:(下载不了可以用百度云离线下载) Win:https://download.jetbrains.com/resharper/JetBrains.Rider-2017.3.1. ...
- Python自动化--语言基础6--模块操作之re、MySQL、Excel
1.Python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("aaa ...
- 优化Linux内核参数提高服务器负载能力
首先,编辑一下/etc/sysctl.conf 文件,调整一下以下参数,如果没有经过优化的Linux内核可能没有这些参数,那么把需要补充的复制添加进去即可,其他设置默认即可,不需要理解. 记得修改完成 ...