简单题

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. lintcode-198-排列序号II

    198-排列序号II 给出一个可能包含重复数字的排列,求这些数字的所有排列按字典序排序后该排列在其中的编号.编号从1开始. 样例 给出排列[1, 4, 2, 2],其编号为3. 思路 和 lintco ...

  2. 继承&构造函数

    子父类中的构造函数的特点. 在子类构造对象时,发现,访问子类构造函数时,父类也运行了. 为什么呢? 原因是:在子类的构造函数中第一行有一个默认的隐式语句. super(); 子类的实例化过程:子类中所 ...

  3. 【Leetcode】445. Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  4. Windows连接Linux服务器中MySQL数据库-权限配置

    问题描述 在Windows系统中安装了监控MySQL数据库服务器性能的工具Spotlight on MySQL,利用Spotlight连接Linux服务器中的MySQL,进行相关配置如下:   点击& ...

  5. Win server 2016 升级 Win server 2019 [测试验证]

    . 给win server 2016 挂在 win server 2019 的安装盘 2. 点击setup 直接进行安装操作  选择不下载更新, 然后到达输入序列号的界面 序列号为: WMDGN-G9 ...

  6. 使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证

    使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证   目前WEB 前后端分离的开发模式比较流行,之前做过的几个小项目也都是前后分离的模式,后端使用asp.net weba ...

  7. ZooKeeper系列文章

    ZooKeeper FAQ ZooKeeper客户端事件串行化处理 ZooKeeper分布式锁避免羊群效应(Herd Effect) ZooKeeper管理员指南——部署与管理ZooKeeper Zo ...

  8. echarts tooltip 自定义formatter怎么设置颜色?

    formatter: function(params) { var result = ''; params.forEach(function (item) { result += item.marke ...

  9. Educational Codeforces Round 55 Div. 2 翻车记

    A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...

  10. [codeforces696B]Puzzles

    B. Puzzles time limit per test  1 second memory limit per test 256 megabytes input standard input ou ...