【bzoj1176】[Balkan2007]Mokia
题目描述:
维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.
输入:
第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小
接下来每行为一下三种输入之一(不包含引号):
"1 x y a"
"2 x1 y1 x2 y2"
"3"
输入1:你需要把(x,y)(第x行第y列)的格子权值增加a
输入2:你需要求出以左上角为(x1,y1),右下角为(x2,y2)的矩阵内所有格子的权值和,并输出
输入3:表示输入结束
输出:
对于每个输入2,输出一行,即输入2的答案
题解:
本蒟蒻的第一道cdq分治题。。。“cdq分治不就是归并排序吗?”写完这道题以后我对这句话有了更深的理解。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif #define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
#define maxn 200010
#define maxm 2000010
struct event
{
int x, y, pos, opet, ans;
inline bool operator < (const event &that) const {return pos < that.pos ;}
}t[maxn], q[maxn];
#define lowbit(_x) ((_x) & -(_x))
int bit[maxm], last[maxm], s, w, cnt, now;
inline void add(R int x, R int val)
{
for (; x <= w; x += lowbit(x))
{
if (last[x] != now)
bit[x] = 0;
bit[x] += val;
last[x] = now;
}
}
inline int query(R int x)
{
R int ans = 0;
for (; x ; x -= lowbit(x))
{
if (last[x] == now)
ans += bit[x];
}
return ans;
}
void cdq(R int left, R int right)
{
if (left == right) return ;
R int mid = left + right >> 1;
cdq(left, mid); cdq(mid + 1, right);
++now;
for (R int i = left, j = mid + 1; j <= right; ++j)
{
for (; i <= mid && q[i].x <= q[j].x; ++i)
if (!q[i].opet)
add(q[i].y, q[i].ans);
if (q[j].opet)
q[j].ans += query(q[j].y);
}
R int i, j, k = 0;
for (i = left, j = mid + 1; i <= mid && j <= right; )
{
if (q[i].x <= q[j].x)
t[k++] = q[i++];
else
t[k++] = q[j++];
}
for (; i <= mid; )
t[k++] = q[i++];
for (; j <= right; )
t[k++] = q[j++];
for (R int i = 0; i < k; ++i)
q[left + i] = t[i];
}
int main()
{
// setfile();
s = FastIn();
w = FastIn();
while (1)
{
R int opt = FastIn();
if (opt == 1)
{
R int x = FastIn(), y = FastIn(), a = FastIn();
q[++cnt] = (event){x, y, cnt, 0, a};
}
if (opt == 2)
{
R int x = FastIn() - 1, y = FastIn() - 1, a = FastIn(), b = FastIn();
q[++cnt] = (event) {x, y, cnt, 1, x * y * s};
q[++cnt] = (event) {a, b, cnt, 2, a * b * s};
q[++cnt] = (event) {x, b, cnt, 2, x * b * s};
q[++cnt] = (event) {a, y, cnt, 2, a * y * s};
}
if (opt == 3) break;
}
cdq(1, cnt);
std::sort(q + 1, q + cnt + 1);
for (R int i = 1; i <= cnt; ++i)
if (q[i].opet == 1)
printf("%d\n",q[i].ans + q[i + 1].ans - q[i + 2].ans - q[i + 3].ans ), i += 3;
return 0;
}
【bzoj1176】[Balkan2007]Mokia的更多相关文章
- 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治
[BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...
- 【bzoj1176】[Balkan2007]Mokia/【bzoj2683】简单题 CDQ分治+树状数组
bzoj1176 题目描述 维护一个W*W的矩阵,初始值均为S(题目描述有误,这里的S没有任何作用!).每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数 ...
- 【BZOJ1176】[BOI2007]Mokia 摩基亚
[BZOJ1176][BOI2007]Mokia 摩基亚 题面 bzoj 洛谷 题解 显然的\(CDQ\)\(/\)树套树题 然而根本不想写树套树,那就用\(CDQ\)吧... 考虑到点\((x1,y ...
- 【BZOJ1176】Mokia(CDQ分治)
[BZOJ1176]Mokia(CDQ分治) 题面 BZOJ权限题啊,,,, dbzoj真好 Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的 ...
- 【BOI2007】【BZOJ1176】Mokia
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MB Submit: 1059 Solved: 432 [Submit][St ...
- 【BZOJ1176】Mokia
题目大意:给定一个 N*N 的矩形,有 Q 次操作,每个操作可以是矩形单点修改或查询子矩形的权值和. 题解:CDQ分治适合处理修改操作之间互不影响且支持离线的题目. 满足以上操作条件的显然可以树套树来 ...
- 【cdq分治】【P4390】[BOI2007]Mokia 摩基亚
Description 给你一个 \(W~\times~W\) 的矩阵,每个点有权值,每次进行单点修改或者求某子矩阵内权值和,允许离线 Input 第一行是两个数字 \(0\) 和矩阵大小 \(W\) ...
- BZOJ1176:[Balkan2007]Mokia——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1176 Description(题面本人自行修改了一下) 维护一个W*W的矩阵,初始值均为0.每次操作 ...
- 【BZOJ1174】: [Balkan2007]Toponyms
→原题← ↑这样子我就不复制题面啦~ 就是一题很裸的字典树而已,不过空间卡的很死,直接开个数组 tr[N][52] 什么之类的一定会RE的(惨痛的教训) 当字典树空间不够而时间限制又比较宽松时,我们可 ...
随机推荐
- if you wanna the rainbow, you have to deal with the rain.
bulk. n. 大量 reluctant. adj. 不情愿的 terrorist. n. 恐怖分子 recognition. n. 认出 tout.v. 兜售 conceal.v. 隐藏 dras ...
- java正则匹配正则表达式
1.简单匹配小案例 public static void main( String[] args ){ // 按指定模式在字符串查找 String line = "This order wa ...
- 复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中
cp -r /etc/p*[a-Z] /tmp/mytest1
- [Web 前端] 021 js 初识 Javascript
1. Javascript 简介 1.1 定位 JS 是运行在浏览器端的脚本语言 1.1.1 关于浏览器 JS 由浏览器解释执行 JS 通常被直接嵌入 HTML 页面 1.1.2 关于脚本语言 JS ...
- 自己手动用原生实现bind/call/apply
自己手动用原生实现bind/call/apply:https://www.cnblogs.com/LHLVS/p/10595784.html
- 安装配置php及fastadmin
FastAdmin教程之准备运行环境 一.Node.js http://nodejs.cn/download/ https://npm.taobao.org/mirrors/node/v8.4.0 ...
- HDU 1594 find the max
数序问题. 题意是说 一个数列 a1,a2,--ai,--an; x=i , y = ai:找两个点斜率绝对值.!最大. 第一次没找绝对值,--认真读题. .. x 每次加1 . 仅仅须要找 相邻的 ...
- sping data jpa 共享主键 OneTonOne 延时加载
当我们使用spring boot创建项目时,系统默认使用的是如下parent. <parent> <groupId>org.springframework.boot</g ...
- C语言双向链表讲解
一.双向链表的概念 双向链表基于单链表.单链表是单向的,有一个头结点,一个尾结点,要访问任何结点,都必须知道头结点,不能逆着进行.而双链表添加了一个指针域,通过两个指针域,分别指向结点的前结点和后结点 ...
- Django之cookie 和session
---恢复内容开始--- 一.cookie 前戏.cookie 的由来 由于http协议是无状态的 无法记录用户状态 cookie就是保存在客户端浏览器上的键值对 工作原理:当你登陆成功之后 浏览器会 ...