BZOJ2683: 简单题(cdq分治 树状数组)
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 2142 Solved: 874
[Submit][Status][Discuss]
Description
命令 |
参数限制 |
内容 |
1 x y A |
1<=x,y<=N,A是正整数 |
将格子x,y里的数字加上A |
2 x1 y1 x2 y2 |
1<=x1<= x2<=N 1<=y1<= y2<=N |
输出x1 y1 x2 y2这个矩形内的数字和 |
3 |
无 |
终止程序 |
Input
Output
Sample Input
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
5
HINT
Source
cdq分治裸题。
首先我们对询问点拆为$4$个前缀和查询
再把所有的询问/修改离线,按照$x$轴排序
直接上cdq分治,用树状数组为护$y$轴的贡献
归并排序版太难写了直接上sort
#include<cstdio>
#include<algorithm>
#define lowbit(x) ((x) & (-x))
using namespace std;
const int MAXN = * 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, Qnum;
struct Query {
int opt, x, y, v, ans, xxx;
bool operator < (const Query &rhs) const {
return x == rhs.x ? y < rhs.y : x < rhs.x;
}
}Q[MAXN], Tp[MAXN];
int T[MAXN];
void Add(int pos, int val) {
for(int i = pos; i <= N; i += lowbit(i)) T[i] += val;
}
int Sum(int pos) {
int ans = ;
for(int i = pos; i; i -= lowbit(i)) ans += T[i];
return ans;
}
int num = , out[MAXN];
void CDQ(int l, int r) {
if(l == r) return ;
int mid = l + r >> ;
CDQ(l, mid);
CDQ(mid + , r);
int i = l, j = mid + , p = l, last = ;
sort(Q + l, Q + mid + );
sort(Q + mid + , Q + r + );
while(j <= r) {
while(Q[i].opt == && i <= mid) i++;
while(Q[j].opt == && j <= r) j++;
if(i <= mid && Q[i].x <= Q[j].x) Add(Q[i].y, Q[i].v), last = i++;
else if(j <= r) Q[j].ans += Sum(Q[j].y), j++;
}
for(int i = l; i <= last; i++)
if(Q[i].opt == )
Add(Q[i].y, -Q[i].v); }
int main() {
N = read();
for(;;) {
int op = read();
if(op == ) break;
if(op == ) {Q[++num].opt = op; Q[num].x = read(), Q[num].y = read(), Q[num].v = read();}
else {
int xx1 = read(), yy1 = read(), xx2 = read(), yy2 = read();
Qnum++;
Q[++num] = (Query) {op, xx2, yy2, , , Qnum};
Q[++num] = (Query) {op, xx1 - , yy1 - , , , Qnum};
Q[++num] = (Query) {op, xx1 - , yy2, , , Qnum};
Q[++num] = (Query) {op, xx2, yy1 - , , , Qnum};
}
}
CDQ(, num);
for(int i = ; i <= num; i++) {
if(Q[i].opt == ) {
if(Q[i].v == || Q[i].v == ) out[Q[i].xxx] += Q[i].ans;
else out[Q[i].xxx] -= Q[i].ans;
}
}
for(int i = ; i <= Qnum; i++)
printf("%d\n", out[i]);
return ;
}
BZOJ2683: 简单题(cdq分治 树状数组)的更多相关文章
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- 【bzoj1176】[Balkan2007]Mokia/【bzoj2683】简单题 CDQ分治+树状数组
bzoj1176 题目描述 维护一个W*W的矩阵,初始值均为S(题目描述有误,这里的S没有任何作用!).每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组
[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...
- 【bzoj3262】陌上花开 CDQ分治+树状数组
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...
- 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组
题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...
- LOJ3146 APIO2019路灯(cdq分治+树状数组)
每个时刻都形成若干段满足段内任意两点可达.将其视为若干正方形.则查询相当于求历史上某点被正方形包含的时刻数量.并且注意到每个时刻只有O(1)个正方形出现或消失,那么求出每个矩形的出现时间和消失时间,就 ...
- BZOJ 4553 [Tjoi2016&Heoi2016]序列 ——CDQ分治 树状数组
考虑答案的构成,发现是一个有限制条件的偏序问题. 然后三个维度的DP,可以排序.CDQ.树状数组各解决一维. #include <map> #include <cmath> # ...
- BZOJ 2683: 简单题(CDQ分治 + 树状数组)
BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...
随机推荐
- ruby逻辑判断符号
puts true and false #相当于 (puts true) and false Use &&/|| for boolean expressions, and/or fo ...
- JavaScript Date 学习心得
1.要创建一个日期对象,使用new 操作符和Date构造函数即可: var date=new Date() 在调用Date构造函数而不传递参数的情况下,新创建的对象可以自动获得当前日期和时间.必须传入 ...
- pat06-图7. How Long Does It Take (25)
06-图7. How Long Does It Take (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- Tomcat启动时报错:“ Error starting static Resources”问题解决
部署测试环境的时候,需要用到Tomcat.故在Linux上部署了Tomcat,并将开发提供的工程包部署到Tomcat的webapps目录下,启动Tomcat,部署成功.第二天修改工程配置文件时,发现w ...
- Myeclipse修改jdk版本流程
Myeclipse修改jdk版本流程 很多时候,项目没有用对jdk版本时候,项目报错,在MyEclipse中,要修改JDK版本 有三处地方需要注意:!! 1.第一处 2.第二处 3.第三处
- .net 视图格式化
昨天在做一个功能,要在界面上按照规定的格式显示一个时间,如果直接在expression那里格式化的话(如下:) @Html.DisplayFor(c => Convert.ToDateTime( ...
- CentOS7中添加新硬盘
cp /etc/fstab /etc/fstab.bak echo /dev/vdb1 /mnt/disk1 ext4 defaults 0 0 >> /etc/fstab mkdir ...
- 利用COM组件实现对WORD书签处写入值
using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop.Wo ...
- Akka探索第二个例子by fsharp
本文重度借鉴了github上akkabootcamp教程. 先上代码 open Akka open Akka.Actor open System type Message = | ContinuePr ...
- Spring Boot 2.x 自定义metrics 并导出到influxdb
Step 1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...