简单题

Time Limit: 50 Sec  Memory Limit: 128 MB
[Submit][Status][Discuss]

Description

  你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

命令

参数限制

内容

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

  输入文件第一行一个正整数N。
  接下来每行一个操作。

Output

  对于每个2操作,输出一个对应的答案。
 

Sample Input

  4
  1 2 3 3
  2 1 1 3 3
  1 2 2 2
  2 2 2 3 4
  3

Sample Output

  3
  5

HINT

  1<=N<=500000,操作数不超过200000个,内存限制20M。
  对于100%的数据,操作1中的A不超过2000。

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】简单题 [分治][树状数组]的更多相关文章

  1. 洛谷 P5057 [CQOI2006]简单题(树状数组)

    嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...

  2. BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组

    BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组 Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加 ...

  3. BZOJ 2683 简单题 cdq分治+树状数组

    题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...

  4. BZOJ 1176 Mokia CDQ分治+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  5. BZOJ_3262_陌上花开_CDQ分治+树状数组

    BZOJ_3262_陌上花开_CDQ分治+树状数组 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的 ...

  6. 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组

    [BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...

  7. 【bzoj3262】陌上花开 CDQ分治+树状数组

    题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...

  8. 【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 ...

  9. BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组

    BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组 Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , , 之后, ...

随机推荐

  1. ASP.NET MVC 4 内容映射

    文章:ASP.NET MVC 4 内容映射 地址:https://msdn.microsoft.com/zh-cn/library/gg416514(v=vs.108).aspx 模型-视图-控制器 ...

  2. HDU 2132 An easy problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2132 Problem Description We once did a lot of recursional ...

  3. sublime text there are no packages available for installation 解决办法

    hosts  增加一行 : #50.116.33.29       sublime.wbond.net.

  4. java内存加载机制

    什么是java类加载? 类加载是指将.class类中的二进制数据存放到内存中,会在内存中的推中建立一个java.lang.String的引用对象来存放方法区的数据结构,而类中的数据会放到方法区中 类加 ...

  5. Redis 备份数据的两种方式

    既然是数据库,那就一定有数据备份方式了,而且 Redis 是内存形式的数据库,更需要数据备份了,要不然断电数据就全都丢失了. Redis 数据备份有两种方式: RDB(数据快照) AOF(记录操作日志 ...

  6. Android四大组件之Intent(续2)

    1.你如何通过一个intent来唤醒activity? this.startActivity(intent,request);      2.什么是显式.隐式的intents? 显式:指定组件名,通常 ...

  7. 为什么我再也不想和 Google HR 交谈了

    英文:yegor256,编译:伯乐在线/心灵是一棵开花的树 http://blog.jobbole.com/110340/ [伯乐在线导读]: 关于程序员面试时现场写代码,估计大家还记得 2015 年 ...

  8. 【MVVM Dev】ComboBox嵌入CheckBox的显示问题

    一.前言 在ComboBox中嵌入若干个CheckBox时,当我们勾选一些CheckBox,ComboBox会显示相应的勾选项. 例如:CheckBox项有A,B,C   那么勾选这三项,ComboB ...

  9. 常州day1p5

    给一个 n∗m 的矩阵,矩阵的每个格子上有一个不超过 30 的非负整数. 我们定义一条合法的路线是从(1,1)开始只能向右和向下移动到达(n,m)的路线. 定义数列 A1,A2,A3,..,An+m− ...

  10. BZOJ 4864: [BeiJing 2017 Wc]神秘物质 解题报告

    4864: [BeiJing 2017 Wc]神秘物质 Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子. ...