【BZOJ2683】简单题 [分治][树状数组]
简单题
Time Limit: 50 Sec Memory Limit: 128 MB
[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
Solution
首先把询问拆成4个,那么我们就只要维护一个点左下角权值和了。
然后对所有操作按照 x 升序排序。
对 y 用个树状数组求前缀和,(由于 x 升序,所以此时询问已经相当于对y求前缀和了)
以mid为分界线,考虑左区间对右区间的影响。
显然,我们可以把左区间的修改执行,然后执行右区间的询问。
这样我们就做完了这道题。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long s64; const int ONE = ;
const int INF = ; int get()
{
int res = , Q = ; char c;
while( (c = getchar()) < || c > )
if(c == '-') Q = -;
if(Q) res = c - ;
while( (c = getchar()) >= && c <= )
res = res * + c - ;
return res * Q;
} int n;
namespace BIT
{
int C[ONE];
int lowbit(int i) {return i & -i;}
void Add(int R, int x)
{
for(int i = R; i <= n; i += lowbit(i))
C[i] += x;
}
int Query(int R)
{
int res = ;
for(int i = R; i >= ; i -= lowbit(i))
res += C[i];
return res;
}
} int id, query_num, Ans[ONE];
struct power
{
int id, opt, from;
int x, y, val;
}oper[ONE], q[ONE]; bool cmp(const power &a, const power &b)
{
if(a.x != b.x) return a.x < b.x;
return a.opt < b.opt;
} void Deal(int x_1, int y_1, int x_2, int y_2)
{
query_num++;
oper[++id] = (power){id, , query_num, x_2, y_2, };
oper[++id] = (power){id, , query_num, x_1 - , y_1 - , };
oper[++id] = (power){id, , query_num, x_1 - , y_2, -};
oper[++id] = (power){id, , query_num, x_2, y_1 - , -};
} void Solve(int l, int r)
{
if(l >= r) return; int mid = l + r >> ;
for(int i = l; i <= r; i++)
{
if(oper[i].opt == && oper[i].id <= mid)
BIT::Add(oper[i].y, oper[i].val);
if(oper[i].opt == && oper[i].id > mid)
Ans[oper[i].from] += BIT::Query(oper[i].y) * oper[i].val;
} for(int i = l; i <= r; i++)
if(oper[i].opt == && oper[i].id <= mid)
BIT::Add(oper[i].y, -oper[i].val); int tl = l, tr = mid + ;
for(int i = l; i <= r; i++)
if(oper[i].id <= mid) q[tl++] = oper[i];
else q[tr++] = oper[i]; for(int i = l; i <= r; i++)
oper[i] = q[i]; Solve(l, mid), Solve(mid + , r);
} int opt, x_1, y_1, x_2, y_2; int main()
{
n = get();
for(;;)
{
opt = get();
if(opt == ) break;
if(opt == )
oper[++id].id = id, oper[id].opt = ,
oper[id].x = get(), oper[id].y = get(), oper[id].val = get();
if(opt == )
x_1 = get(), y_1 = get(),
x_2 = get(), y_2 = get(),
Deal(x_1, y_1, x_2, y_2);
} sort(oper + , oper + id + , cmp); Solve(, id); for(int i = ; i <= query_num; i++)
printf("%d\n", Ans[i]);
}
【BZOJ2683】简单题 [分治][树状数组]的更多相关文章
- 洛谷 P5057 [CQOI2006]简单题(树状数组)
嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...
- BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组
BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组 Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加 ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- BZOJ_3262_陌上花开_CDQ分治+树状数组
BZOJ_3262_陌上花开_CDQ分治+树状数组 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的 ...
- 【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 ...
- BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组
BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组 Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , , 之后, ...
随机推荐
- 【Leetcode】113Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 【beta】阶段会议记录汇总
第一次: http://www.cnblogs.com/yumiaomiao/p/6026752.html 第二次: http://www.cnblogs.com/liquan/p/6031802.h ...
- 解决java图形界面label中文乱码
第一:在你的具有main函数的类也即你应用运行的主类上点击右键,选择Run As中的Run Configurations,如下图:java,awt,中文方框,中文乱码第二,在Arguments标签下的 ...
- 微信小程序组件 客服
<!-- 话务 --> <view class='detail-tel flexca'> <image class='image-full' src='../../img ...
- 小工具xml生成记录
public partial class Form1 : Form { public Form1() { InitializeComponent ...
- Linux下编译程序时,经常会遇到“undefined reference to XXX” 报错,
Linux下编译程序时,经常会遇到“undefined reference to XXX” 报错, 这里总结一些可能的原因和解决方案,给需要的朋友: 说道undefined reference err ...
- phpisset()和empty()函数区别
PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...
- 第201天:js---实现继承的5种方式
一.构造函数方式 //构造函数 function People(){ this.race = '汉族'; } People.prototype={ eat:function(){ console.lo ...
- bzoj2669-局部极小值
题意 有一个 \(n\times m\) 的矩阵,其中每个数都是 \([1,n\times m]\) 中的一个,不会重复.有一些地方的值比周围的8个位置都小(如果有的话).给出这些位置,求这样的矩阵有 ...
- Day 3 学习笔记
Day 3 学习笔记 STL 模板库 一.结构体 结构体是把你所需要的一些自定义的类型(原类型.实例(:包括函数)的集合)都放到一个变量包里. 然后这个变量包与原先的类型差不多,可以开数组,是一种数据 ...