简单题

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. Android工程方法数超过64k,The number of method references in a .dex file cannot exceed 64K.

    最近将一个老的Eclipse项目转到Android Studio后,用gradle添加了几个依赖,项目可以make,但是一旦run就报错 Error:The number of method refe ...

  2. purcell的emacs配置中的自动补全功能开启

    标记一下,原文参看purcell的emacs配置中的自动补全功能开启 修改init-auto-complete.el文件 ;;(setq-default ac-expand-on-auto-compl ...

  3. Ubuntu系统下adb devices 不能显示手机设备

    1. 查看usb设备,命令:lsusb 结果如下:Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub B ...

  4. BZOJ 2141 排队(树状数组套主席树)

    解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...

  5. day 008 文件操作

    08. 万恶之源-⽂文件操作本节主要内容:1. 初识⽂文件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操 ...

  6. (转)rabbitmq的web管理界面无法使用guest用户登录

    转至http://www.cnblogs.com/mingaixin/p/4134920.html 安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的 ...

  7. Luogu4897 【模板】最小割树

    没事干写一发模板. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...

  8. 二维RMQ模板

    int main(){ ; i <= n; i++) ; j <= m; j++) { scanf("%d", &val[i][j]); dp[i][j][][ ...

  9. JS中数组和字符串具有的方法,以及substring,substr和slice的用法与区别

     String 对象属性 属性 描述 constructor 对创建该对象的函数的引用 length 字符串的长度 prototype 允许您向对象添加属性和方法 String 对象方法 方法 描述 ...

  10. [洛谷P4626]一道水题 II

    题目大意:求$lcm(1,2,3,\cdots,n)\pmod{100000007}$,$n\leqslant10^8$ 题解:先线性筛出质数,然后求每个质数最多出现的次数,可以用$\log_in$来 ...