ACdream 1157 (cdq分治)
Segments
Time Limit: 4000/2000MS (Java/Others)Memory Limit: 20000/10000KB (Java/Others)Problem Description
由3钟类型操作:
1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]
2)C i (1-base) 删除第i条增加的线段,保证每条插入线段最多插入一次,且这次删除操作一定合法
3) Q L R(1 <= L <= R <= 1000000000) 查询目前存在的线段中有多少条线段完全包含[L,R]这个线段,线段X被线段Y完全包含即LY <= LX<= RX <= RY)
给出N,接下来N行,每行是3种类型之一Input
多组数据,每组数据N
接下来N行,每行是三种操作之一(1 <= N <= 10^5)
Output
对于每个Q操作,输出一行,答案Sample Input
6
D 1 100
D 3 8
D 4 10
Q 3 8
C 1
Q 3 8Sample Output
2
1Hint
注意,删除第i条增加的线段,不是说第i行,而是说第i次增加。
比如
D 1 10
Q 1 10
D 2 3
D 3 4
Q 5 6
D 5 6
C 2是删除D 2 3
C 4是删除D 5 6
第一次听说cdq分治,cdq是陈丹琦orz。。
在我们平常使用的分治中,每一个子问题只解决它本身(可以说是封闭的)。
而在cdq分治中,对于划分出来的两个子问题,前一个子问题用来解决后一个子问题而不是它本身。
具体算法流程如下:
1.将整个操作序列分为两个长度相等的部分(分)
2.递归处理前一部分的子问题(治1)
3.计算前一部分的子问题中的修改操作对后一部分子问题的影响(治2)
4.递归处理后一部分子问题(治3)
另外,能使用常量引用的地方尽量使用,可以提高效率。
Accepted Code:
/*************************************************************************
> File Name: 1157.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月10日 星期日 08时24分10秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
int c[maxn<<], l[maxn], r[maxn], ans[maxn];
struct node {
int t, id, l, r;
node() {}
node(int a, int b, int c, int d) {
t = a; id = b; l = c; r = d;
}
}a[maxn];
vector<int> xs;
int w; bool cmp1(const node &a, const node &b) {
return a.id < b.id;
} bool cmp2(const node &a, const node &b) {
if (a.l != b.l) return a.l < b.l;
return a.r > b.r;
} int lowbit(int x) {
return x & -x;
} void add(int x, int v) {
x = *maxn - x;
while (x < maxn*) {
c[x] += v;
x += lowbit(x);
}
} int sum(int x) {
int res = ;
x = *maxn - x;
while (x > ) {
res += c[x];
x -= lowbit(x);
}
return res;
} void solve(int l, int r) {
if (l >= r) return ;
int mid = (l + r) >> ;
solve(l, mid);
sort(a+l, a+r+, cmp2);
for (int i = l; i <= r; i++) {
if (a[i].id <= mid) {
if (a[i].t == ) add(a[i].r, );
else if (a[i].t == -) add(a[i].r, -);
} else {
if (a[i].t == ) ans[a[i].id] += sum(a[i].r);
}
}
for (int i = l; i <= r; i++) if (a[i].id <= mid) {
if (a[i].t == ) add(a[i].r, -);
if (a[i].t == -) add(a[i].r, );
}
sort(a+l, a+r+, cmp1);
solve(mid+, r);
} int main(void) {
int n;
while (~scanf("%d", &n)) {
int cnt = ;
xs.clear();
for (int i = ; i <= n; i++) {
char s[];
scanf("%s", s);
if (s[] == 'D') {
int x, y;
scanf("%d %d", &x, &y);
xs.push_back(x);
xs.push_back(y);
l[cnt] = x;
r[cnt++] = y;
a[i] = node(, i, x, y);
} else if (s[] == 'Q') {
int x, y;
scanf("%d %d", &x, &y);
xs.push_back(x);
xs.push_back(y);
a[i] = node(, i, x, y);
} else {
int id;
scanf("%d", &id);
a[i] = node(-, i, l[id], r[id]);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
w = xs.size();
for (int i = ; i <= n; i++) {
a[i].l = lower_bound(xs.begin(), xs.end(), a[i].l)-xs.begin()+;
a[i].r = lower_bound(xs.begin(), xs.end(), a[i].r)-xs.begin()+;
//printf("%d %d\n", a[i].l, a[i].r);
}
//memset(c, 0, sizeof(c));
memset(ans, , sizeof(ans));
solve(, n);
for (int i = ; i <= n; i++) if (!a[i].t) printf("%d\n", ans[i]);
}
return ;
}
ACdream 1157 (cdq分治)的更多相关文章
- acdream 1157Segments cdq分治
题目链接 #include <iostream> #include <vector> #include <cstdio> #include <cstring& ...
- ACdream 1157 Segments(CDQ分治)
题目链接:http://acdream.info/problem?pid=1157 Problem Description 由3钟类型操作:1)D L R(1 <= L <= R < ...
- ACdream 1157 Segments CDQ分治
题目链接:https://vjudge.net/problem/ACdream-1157 题意: Problem Description 由3钟类型操作: 1)D L R(1 <= L < ...
- 【ACdream】1157 Segments cdq分治
Segments Problem Description 由3钟类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i ...
- ACdream 1157 Segments
Segments Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on ACdream. Original I ...
- 【教程】简易CDQ分治教程&学习笔记
前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦! CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...
- BZOJ 2683 简单题 ——CDQ分治
[题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...
- HDU5618 & CDQ分治
Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...
- 初识CDQ分治
[BZOJ 1176:单点修改,查询子矩阵和]: 1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 200 ...
随机推荐
- webpack 兼容低版本浏览器,转换ES6 ES7语法
ES6,ES7真的太棒了,async +await+Promise,让我阅读代码的时候不用再从左拉到右了(异步太多,一层套一层真的太头痛) 但是有个问题,打包后低版本浏览器运行不了,还有我用了一些混淆 ...
- Emgucv图像处理工具
此工具是当年自己在学习Emgucv的时候,慢慢积累的,包含了常用的图像处理算法,非常适合新人学习,现放出源码,由于是以前做的,功能不全. 当时Emgucv的学习资料非常之少,没有一本书是讲Emgucv ...
- “void * __cdecl operator new(unsigned int)”(??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
转自VC错误:http://www.vcerror.com/?p=1377 问题描述: 当 C 运行时 (CRT) 库和 Microsoft 基础类 (MFC) 库的链接顺序有误时,可能会出现以下 L ...
- ArcMap基于Oracle出现sde.instances_util.check_instance_table_conflicts:: ORA-00942:表或视图不存在/table or view doesnot exist解决思路
SDE环境:Oracle12C+ArcMap10.7+WinServer2012 出现问题情况: 1.SDE可以连接正常打开,但就是无法新建要素.导入要素等: 1)在根目录新建或导入要素,弹出提示: ...
- C++——运行时类型识别RTTI
1.实现方式 typeid运算符,返回表达式的类型 dynamic_cast运算符,基类的指针或引用安全地转换成派生类的指针或引用 2.适用于:使用基类的指针或引用执行派生类的操作,且该操作不是虚函数 ...
- 6. 使用cadvisor监控docker容器
Prometheus监控docker容器运行状态,我们用到cadvisor服务,cadvisor我们这里也采用docker方式直接运行.这里我们可以服务端和客户端都使用cadvisor 客户端 1.下 ...
- 点读系列《Jenkins用户文档》
Jenkins用户手册官网地址:点击打开 开源 CI&CD 软件 自动化各种任务, build test deploy 支持各种运行方式 Jenkins入门 入门指南 需要java和docke ...
- activiti7流程变量的测试(设置全局变量)
package com.zcc.activiti03; import org.activiti.engine.*;import org.activiti.engine.repository.Deplo ...
- 在apache hadoop2.6 上部署hive 并将hive数据源存储于Mysql
集成hive 的前提是apache hadoop 集群能够正常启动. hadoop 版本 apach2.6.0 hive 版本:1.2.1 1.安装mysql 并赋予权限: 1.1:创建hive 用 ...
- 什么是索引?Mysql目前主要的几种索引类型
一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...