BZOJ 2683 简单题 ——CDQ分治
【题目分析】
感觉CDQ分治和整体二分有着很本质的区别。
为什么还有许多人把他们放在一起,也许是因为代码很像吧。
CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件。
排序解决了x ,分治解决了t ,树状数组解决了y。
时间复杂度,排序log,分治log,树状数组也是log
分治中加入了树状数组,所以复杂度带两个log
而整体二分完全没有时间的先后,所以只有一个log。
CDQ分治,分治的是时间。
整体二分,分治的是答案。
还是很不同的算法。
【代码】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 800010
#define SIZE 500010
#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];
long long 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,long long x)
{
while (i && i <= w) c[i] += x,i += lowbit(i);
}
inline long long query(int i)
{
long long 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()
{
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("%lld\n",ans[i]);
}
BZOJ 2683 简单题 ——CDQ分治的更多相关文章
- BZOJ 2683: 简单题 [CDQ分治]
同上题 那你为什么又发一个? 因为我用另一种写法又写了一遍... 不用排序,$CDQ$分治的时候归并排序 快了1000ms... #include <iostream> #include ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- BZOJ 2683: 简单题(CDQ 分治)
题面 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: ...
- bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- bzoj2683简单题 cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1803 Solved: 731[Submit][Status][Discuss] ...
- BZOJ 2683: 简单题(CDQ分治 + 树状数组)
BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...
- 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治
[BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...
随机推荐
- Eclipse 导入外部项目无法识别为web项目并且无法在部署到tomcat下
uss_web如果没有左上角那个球,tomcat就识别不出来的. 1.进入项目目录,找到.project文件,打开. 2.找到...代码段,加入如下标签内容并保存: <nature>org ...
- Hadoop基础——第一弹:Hadoop介绍
一.基础 1.了解Java.Linux操作系统相关知识 2.如需精进,应为水平要达到一定标准,能够阅读国外相关技术网站,eg:http://hadoop.apache.org/ 二.什么是Hadoop ...
- js构造函数的方法与原型prototype
把方法写在构造函数内的情况我们简称为函数内方法,把方法写在prototype属性上的情况我们简称为prototype上的方法 函数内的方法: 使用函数内的方法我们可以访问到函数内部的私有变量,如果我们 ...
- href="javascript:;" 作用
<a href="javascript:;" onclick="doExport(this)" class="easyui-linkbutton ...
- Linux C 收藏
某招聘要求:熟悉高性能分布式网络服务端设计开发,熟悉epoll.多线程.异步IO.事件驱动等服务端技术: <UNIX环境高级编程(第3版)>apue.h等源码文件的编译安装 <UNI ...
- 安装 sublime2 (包括插件)
1.下载地址:http://www.sublimetext.com/2,注意选择不同的平台: 2.安装后,打开sublime,在菜单栏 help -- enter license 打开一个窗口,复制 ...
- CentOS光盘挂载命令以及安装软件
最近又学习了一个命令:mount 挂载命令,我们在安装软件的时候,直接敲命令install 包名,但是这里其实是联网安装的, 如果使用光盘,从本地安装就要使用mount命令. 1.我的linux系统是 ...
- 复选框css
input, select, button, textarea{ -webkit-appearance:none; }该属性会导致复选框失去选择效果
- log4net 2.0.4有问题,AdoNetAppender会报错
坑死老子了 <appSettings> <add key="log4net.Internal.Debug" value="true"/> ...
- [转载]jquery版小型婚礼(可动态添加祝福语)
原文链接:http://www.cnblogs.com/tattoo/p/3788019.html 前两天在网上不小心看到“js许愿墙”这几个字,我的神经就全部被调动了.然后就开始我 的百度生涯,一直 ...