BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia
Time Limit: 30 Sec Memory Limit: 162 MB
Submit: 1854 Solved: 821
[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
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
5
HINT
保证答案不会超过int范围
Solution
CDQ分治+树状数组裸题,矩阵分成四个点统计,单点更新。
Code
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream> using namespace std; const int maxn = ;
const int maxm = +;
int s, w, ans[maxm];
struct Node
{
int op, id, x, y, k, to;
Node (int op = , int id = , int x = , int y = , int k = , int to = ):
op(op), id(id), x(x), y(y), k(k), to(to) {}
bool operator < (const Node &AI) const
{
if (x == AI.x && y == AI.y)
return op < AI.op;
if (x == AI.x)
return y < AI.y;
return x < AI.x;
}
}q[maxm], temp[maxm];
struct BIT
{
int c[maxn+];
int lowbit(int x)
{
return x & -x;
}
void update(int x, int d)
{
while (x <= w)
{
c[x] += d;
x += lowbit(x);
}
}
int query(int x)
{
int ret = ;
while (x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
}
}T; void cdq(int l, int r)
{
if (l == r)
return ;
int mid = (l+r)>>;
for (int i = l; i <= r; ++i)
{
if (q[i].id <= mid && q[i].op == )
T.update(q[i].y, q[i].k);
if (q[i].id > mid && q[i].op == )
ans[q[i].to] += q[i].k*T.query(q[i].y);
}
for (int i = l; i <= r; ++i)
if (q[i].id <= mid && q[i].op == )
T.update(q[i].y, -q[i].k);
int t1 = l-, t2 = mid;
for (int i = l; i <= r; ++i)
if (q[i].id <= mid)
temp[++t1] = q[i];
else
temp[++t2] = q[i];
for (int i = l; i <= r; ++i)
q[i] = temp[i];
cdq(l, mid), cdq(mid+, r);
} int main()
{
scanf("%d %d", &s, &w);
w += ;
int oper, cnt = , cnt_2 = ;
while (~scanf("%d", &oper) && oper != )
{
if (oper == )
{
int x, y, d;
scanf("%d %d %d", &x, &y, &d);
x += , y += ;
q[++cnt] = Node(, cnt, x, y, d, );
}
else
{
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1 += , y1 += , x2 += , y2 += ;
cnt_2 ++;
ans[cnt_2] = s*(x2-x1+)*(y2-y1+);
q[++cnt] = Node(, cnt, x2, y2, , cnt_2);
q[++cnt] = Node(, cnt, x1-, y2, -, cnt_2);
q[++cnt] = Node(, cnt, x2, y1-, -, cnt_2);
q[++cnt] = Node(, cnt, x1-, y1-, , cnt_2);
}
}
sort(q+, q+cnt+);
cdq(, cnt);
for (int i = ; i <= cnt_2; ++i)
printf("%d\n", ans[i]);
return ;
}
BZOJ 1176 Mokia CDQ分治+树状数组的更多相关文章
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- bzoj 3262 陌上花开 - CDQ分治 - 树状数组
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组
[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...
- 【bzoj3262】陌上花开 CDQ分治+树状数组
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...
- 【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 ...
- LOJ3146 APIO2019路灯(cdq分治+树状数组)
每个时刻都形成若干段满足段内任意两点可达.将其视为若干正方形.则查询相当于求历史上某点被正方形包含的时刻数量.并且注意到每个时刻只有O(1)个正方形出现或消失,那么求出每个矩形的出现时间和消失时间,就 ...
- BZOJ 4553 [Tjoi2016&Heoi2016]序列 ——CDQ分治 树状数组
考虑答案的构成,发现是一个有限制条件的偏序问题. 然后三个维度的DP,可以排序.CDQ.树状数组各解决一维. #include <map> #include <cmath> # ...
- BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ第一题,warush了好久.. CDQ分治推荐论文: 1 <从<C ...
随机推荐
- 从ISE14.7使用Micoblaze点亮led灯
1. ISE => new program => new source => embedded processor 2. XPS 2.1 create new xps program ...
- Go语言 2 变量、常量和数据类型
文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 2.1 变量 变量是对一块内存空间的命名,程序可以通 ...
- juery中循环遍历json数组
var dataList=[]; var stock0={stockcode:"007758",stockname:"商业政7",state:"1&q ...
- hdu 5914(斐波拉契数列)
Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- Tutorial: Reverse debugging with GDB 7 (转载)
Tutorial: Reverse debugging with GDB 7 Tutorial: Reverse debugging with GDB 7 by Jay Conrod posted o ...
- 转载:C++ typename的起源与用法
转载:http://feihu.me/blog/2014/the-origin-and-usage-of-typename/#typename 侯捷在Effective C++的中文版译序中提到: C ...
- 基于Token的授权(with srping mvc)
@Override public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOExce ...
- Java学习(简介,软件安装)
1. Java概述: Java的发展可以归纳如下的几个阶段. (1)第一阶段(完善期):JDK 1.0 ( 1995年推出)一JDK 1.2 (1998年推出,Java更名为Java 2): (2)第 ...
- vim 图册
网上看到的一些图,感觉不错,分享一下 我现在感觉配置文件,很多没有必要,反而很花哨,但是这些基础的东西,反而很高效,实在 VIM的列编辑操作 删除列 1.光标定位到要操作的地方. 2.CTRL+v 进 ...
- Java学习笔记之:Java Servlet 过滤器配置
一.步骤 1.创建一类实现Filter接口,重写父类的方法 public class SimpleFilter implements Filter { public void destroy() { ...