【BOI2007】【BZOJ1176】Mokia
1176: [Balkan2007]Mokia
Time Limit: 30 Sec Memory Limit: 162 MB
Submit: 1059 Solved: 432
[Submit][Status][Discuss]
Description
维护一个W*W的矩阵,初始值均为S.每次操作能够添加某格子的权值,或询问某子矩阵的总权值.改动操作数M<=160000,询问数Q<=10000,W<=2000000.
Input
第一行两个整数,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:表示输入结束
Output
对于每一个输入2,输出一行,即输入2的答案
Sample Input
0 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
保证答案不会超过int范围
Source
cdq分治的模板题然而我如今才做这个题。
。。
差分一下询问操作。对全部操作按y排序。
树状数组维护一下。
然后就能够了。
自从看过了Tsinsen上的姿势分
我写代码都開始丧心病狂了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200000
#define SIZE 2000010
#define lowbit(x) (x&(-x))
using namespace std;
int w;
int top,opt,L,R,l,r,delta,Top;
struct Query
{
int op;
int x,y,A;
int t,id;
bool operator <(const Query& a)const
{
if (x == a.x && y == a.y) return op < a.op;
if (x == a.x) return y < a.y;
return x < a.x;
}
}que[MAXN],newq[MAXN];
int ans[MAXN],c[SIZE];
inline void in(int &x)
{
x=0;char ch = getchar();
while (!(ch >= '0' && ch <= '9')) ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0',ch = getchar();
}
inline void add(int i,int x)
{
while (i && i <= w) c[i] += x,i += lowbit(i);
}
inline int query(int i)
{
int ret = 0;
while (i) ret += c[i],i -= lowbit(i);
return ret;
}
inline void Solve(int l,int r)
{
int mid = (l + r) >> 1,tp1 = l,tp2 = mid + 1;
if (l == r) return;
for (int i = l;i <= r;i++)
{
if (que[i].t <= mid && que[i].op == 1) add(que[i].y,que[i].A);
if (que[i].t > mid && que[i].op == 2) ans[que[i].id] += query(que[i].y) * que[i].A;
}
for (int i = l;i <= r;i++)
if (que[i].t <= mid && que[i].op == 1) add(que[i].y,-que[i].A);
for (int i = l;i <= r;i++)
if (que[i].t <= mid) newq[tp1++] = que[i];
else newq[tp2++] = que[i];
memcpy(que+l,newq+l,sizeof(Query)*(r - l + 1));
Solve(l,mid);Solve(mid+1,r);
}
int main()
{
freopen("mokia.in","r",stdin);
freopen("mokia.out","w",stdout);
in(opt);in(w);
while (1)
{
in(opt);
if (opt == 3) break;
switch (opt)
{
case 1:
in(L);in(R);in(delta);
que[++top].op = opt;que[top].x = L;que[top].y = R;que[top].A = delta;que[top].t = top;
break;
case 2:
in(L);in(R);in(l);in(r);
que[++top].op = opt;que[top].x = L - 1;que[top].y = R - 1;que[top].t = top;que[top].A = 1;que[top].id = ++Top;
que[++top].op = opt;que[top].x = L - 1;que[top].y = r;que[top].t = top;que[top].A = -1;que[top].id = Top;
que[++top].op = opt;que[top].x = l;que[top].y = R - 1;que[top].t = top;que[top].A = -1;que[top].id = Top;
que[++top].op = opt;que[top].x = l;que[top].y = r;que[top].t = top;que[top].A = 1;que[top].id = Top;
break;
}
}
sort(que + 1,que + top + 1);
Solve(1,top);
for (int i = 1;i <= Top;i++) printf("%d\n",ans[i]);
}
【BOI2007】【BZOJ1176】Mokia的更多相关文章
- 洛谷题解 P4392 【[BOI2007]Sound 静音问题】
题目链接 其实写线段树的题还是比较的令我开心的因为不用脑子 怎么判断这题是要写线段树的? 1.暴力只能拿50分 2.这题是个绿题 3 .看数据范围 #include <cstdio> #i ...
- 【BZOJ1176】[BOI2007]Mokia 摩基亚
[BZOJ1176][BOI2007]Mokia 摩基亚 题面 bzoj 洛谷 题解 显然的\(CDQ\)\(/\)树套树题 然而根本不想写树套树,那就用\(CDQ\)吧... 考虑到点\((x1,y ...
- 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治
[BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...
- 【BZOJ1176】Mokia(CDQ分治)
[BZOJ1176]Mokia(CDQ分治) 题面 BZOJ权限题啊,,,, dbzoj真好 Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的 ...
- 【疯狂造轮子-iOS】JSON转Model系列之二
[疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...
- 【疯狂造轮子-iOS】JSON转Model系列之一
[疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...
- 【原创分享·支付宝支付】HBuilder打包APP调用支付宝客户端支付
前言 最近有点空余时间,所以,就研究了一下APP支付.前面很早就搞完APP的微信支付了,但是由于时间上和应用上的情况,支付宝一直没空去研究.然后等我空了的时候,发现支付宝居然升级了支付逻辑,虽然目前还 ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【Win 10 应用开发】应用预启动
所谓预启动,其实你一看那名字就知道是啥意思了,这是直接译,也找不到比这个叫法更简练的词了.在系统资源允许的情况下(比如电池电量充足,有足够的内存空间),系统会把用户常用的应用程序在后台启动,但不会显示 ...
- 【Win 10 应用开发】启动远程设备上的应用
这个功能必须在“红石-1”(build 14393)以上的系统版中才能使用,运行在一台设备上的应用,可以通过URI来启动另一台设备上的应用.激活远程应用需要以下前提: 系统必须是build 14393 ...
随机推荐
- 在word中批量制作条形码
条码打印软件可以批量生成条形码然后直接打印,但是有些客户不需要直接打印,而是想将生成的条形码在word中进行排版,发给自己的客户或者下属部门来打印.那么如何实现在word中批量制作条形码呢? 操作很简 ...
- Mongodb 设置密码
Mongodb 配置用户密码: 首先创建admin数据库的用户密码 再创建pics的用户名密码 > show databases; admin 0.203125GB local 0.078125 ...
- BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课
题目 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec Memory Limit: 64 MB Description 考虑一 ...
- layer 的常用属性
layer的各种属性代码示例: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading ...
- UITableView 协议中常用的方法
UITableViewDataSource 协议中常用方法 1.设置右边 索引值 - ( NSArray *)sectionIndexTitlesForTableView:( UITableView ...
- grub2的/etc/default/grub文件详解
# If you change this file, run 'update-grub' afterwards to update# /boot/grub/grub.cfg.GRUB_DEFAULT= ...
- MyEclipse 引用其他项目及其jar包
倘若在工作区有两个项目A和B,B项目引用A项目及其jar包(防止调用时A项目的方法出现NoClassFound),步骤如下: 在A项目上点右键看属性,点击Build Path--->Concon ...
- django cookie
设置:auth.login(request, user) response = HttpResponseRedirect(reverse("index" ...
- Tri_integral Summer Training 8 总结
比赛链接 题目 B C E F G I 这是孟加拉国的区域赛. 开场ss读懂了c发现是个水题,于是去敲,结果手贱wa了一炮,不过很快就修正了错误.B题过了不少,我去读,发现是个水题,意识让Moor敲. ...
- POJ 2485:Highways(最小生成树&&prim)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21628 Accepted: 9970 Descrip ...