BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176
CDQ第一题,warush了好久。。
CDQ分治推荐论文:
1 《从<Cash>谈一类分治算法的应用》 陈丹琦
2 《浅谈数据结构题的几个非经典解法》 许昊然
关于CDQ分治,两种要求:①操作不相互影响 ②可以离线处理
题目描述是有问题的,,初始时 全部为0,不是s
题意:二维平面内,两种操作,1 x y v ,位于(x,y)的值加上v.。。2 x1,y1,x2,y2,,(x1,y1) (x2,y2)分别矩形的左上角右下角,查询矩形内所有元素的和。
大致思路,在x这一维上进行分治,然后y这一维直接就可以用树状数组乱搞了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxb = 2e6+;
struct Node
{
int x,y,delt;
int flag,idx;
Node(){}
Node(int _x,int _y,int _delt,int _flag,int _idx):
x(_x),y(_y),delt(_delt),flag(_flag),idx(_idx){};
bool operator < (const Node &rhs)const
{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
}a[];
struct BIT
{
int c[maxb],MAX;
inline int lowbit(int x)
{
return x & -x;
}
void add(int x,int val)
{
while (x <= MAX)
{
c[x] += val;
x += lowbit(x);
}
}
int sum(int x)
{
int res = ;
while (x > )
{
res += c[x];
x -= lowbit(x);
}
return res;
}
}arr; //---------------------------------------------
int ans[];
void CDQ(int l,int r)
{
if (l == r)
return;
int mid = (l + r) >> ;
CDQ(l,mid);
CDQ(mid+,r);
int j = l;
for (int i = mid + ; i <= r; i++)
{
if (a[i].flag == )
{
for ( ; j <= mid && a[j].x <= a[i].x; j++)
{
if (a[j].flag == )
{
arr.add(a[j].y,a[j].delt);
}
}
ans[a[i].idx] += arr.sum(a[i].y) * a[i].delt;
}
}
for (int i = l; i < j; i++)
if (a[i].flag == )
arr.add (a[i].y,-a[i].delt);
inplace_merge (a+l,a+mid+,a+r+); // 合并区间 [l,mid+1) [mid+1,r+1)
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int s,w;
while (~scanf ("%d%d",&s,&w))
{
int op;
int tot = ,totq = ;
arr.MAX = w;
memset(ans,,sizeof(ans));
while (scanf ("%d",&op), op != )
{
if (op == )
{
int x,y,delt;
scanf ("%d%d%d",&x,&y,&delt);
a[tot] = Node(x,y,delt,,tot);
tot++;
}
if (op == )
{
int x1,y1,x2,y2;
scanf ("%d%d%d%d",&x1,&y1,&x2,&y2);
a[tot] = Node(x1-, y1-, , , totq); tot++;
a[tot] = Node(x2, y1-, -, , totq); tot++;
a[tot] = Node(x1-, y2, -, , totq); tot++;
a[tot] = Node(x2, y2, , , totq); tot++;
totq++;
}
}
CDQ (,tot-);
for (int i = ; i < totq; i++)
printf("%d\n",ans[i]);
}
return ;
}
BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)的更多相关文章
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- 【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 ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- LOJ3146 APIO2019路灯(cdq分治+树状数组)
每个时刻都形成若干段满足段内任意两点可达.将其视为若干正方形.则查询相当于求历史上某点被正方形包含的时刻数量.并且注意到每个时刻只有O(1)个正方形出现或消失,那么求出每个矩形的出现时间和消失时间,就 ...
- BZOJ 4553 [Tjoi2016&Heoi2016]序列 ——CDQ分治 树状数组
考虑答案的构成,发现是一个有限制条件的偏序问题. 然后三个维度的DP,可以排序.CDQ.树状数组各解决一维. #include <map> #include <cmath> # ...
- Hdu4742-Pinball Game 3D(cdq分治+树状数组)
Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...
随机推荐
- Android 基础组件
基础组件 所有的控件都可以在java代码中创建出来,并且大部分的属性都对应set和get方法,比如 View view = new View(Context context) context是上下文 ...
- 怎样绕过oracle listener 监听的password设置
怎样绕过oracle 监听的password设置: 1.找到监听进程pid ,并将它kill 掉 ps -ef|grep tns [oracle@lixora admin]$ ps -ef|gr ...
- ViewPager 详解(一)---基本入门
前言:这两天研究研究ViewPager滚动功能,现在很多的app都有用到这个功能,我们的大虾米也有这个模块.要研究就彻底的研究研究,我从不满足于一个功能只是简单的应用,要学就学的彻底,所以我打算将Vi ...
- Lesson 5: Typography in Product Design
Lesson 5: Typography in Product Design Article 1: Interactive Guide to Blog Typography 布局(Layout) 用空 ...
- 沙盒操作的核心函数 - NSSearchPathForDirectoriesInDomains用法
1. iPhone会为每一个应用程序生成一个私有目录,这个目录位于: /Users/sundfsun2009/Library/Application Support/iPhone Simulator/ ...
- IOS静态库和Framework区别
一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系 ...
- access 2007 vba (亖)
OpenReport方法执行 OpenReport 操作在 Visual Basic 中. 语法 表达式 .OpenReport(ReportName, View, FilterName, Whe ...
- java事件处理5(窗口,窗口坐监视器
WindowEvent窗口事件 添加接口 addWindowListener(WindowEvent e) 接口有七个方法 public void windowActivated(WindowEven ...
- Convert String to Long
问题: Given a string, write a routine that converts the string to a long, without using the built in f ...
- Oracle错误问题---- 《ora-12638:身份证明检索失败》
使用客户端可以连接,但只有一个站点出现此问题,非常郁闷,网上查了一下,发现是用户认证问题,解决办法如下: 在ORACLE客户端目录下 NETWORK/ADMIN下的sqlnet.ora,使用记事本打开 ...