题目链接:http://acdream.info/problem?pid=1157

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操作,输出一行,答案
 
题目大意:略。
思路:传说这个就叫做CDQ分治,我也不确定是不是。对每一段[l..r],分治处理[l, mid]和[mid + 1..r]。对于[l..r],处理[l..mid]中的线段对[mid + 1..r]的影响,用树状数组处理一下就可以了。
 
代码(1608MS):
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std; const int MAXN = ; int hashmap[MAXN], hcnt;
int tree[MAXN]; int hash(int x) {
return lower_bound(hashmap, hashmap + hcnt, x) - hashmap + ;
} inline int lowbit(int x) {
return x & -x;
} void modify(int k, int val) {
while(k <= hcnt) {
tree[k] += val;
k += lowbit(k);
}
} int get_sum(int k) {
int res = ;
while(k) {
res += tree[k];
k -= lowbit(k);
}
return res;
} struct Node {
int v, l, r, id;
Node() {}
Node(int v, int l, int r, int id): v(v), l(l), r(r), id(id) {}
bool operator < (const Node &rhs) const {
if(r != rhs.r) return r > rhs.r;
if(l != rhs.l) return l < rhs.l;
return (v == ) < (rhs.v == );
}
}; bool cmp_id(const Node &a, const Node &b) {
return a.id < b.id;
} Node p[MAXN];
int ans[MAXN], tl[MAXN], tr[MAXN];
int n; void solve(int l, int r) {
if(l == r) return ;
int mid = (l + r) >> ;
solve(l, mid);
solve(mid + , r);
sort(p + l, p + r + );
for(int i = l; i <= r; ++i) {
if(p[i].id <= mid) {
if(p[i].v) modify(p[i].l, p[i].v);
} else {
if(!p[i].v) ans[p[i].id] += get_sum(p[i].l);
}
}
for(int i = l; i <= r; ++i) {
if(p[i].id <= mid && p[i].v) modify(p[i].l, -p[i].v);
}
sort(p + l, p + r + , cmp_id);
} int main() {
while(scanf("%d", &n) != EOF) {
int tcnt = ; hcnt = ;
char c;
for(int i = , t, u; i <= n; ++i) {
scanf(" %c", &c);
if(c == 'D') {
scanf("%d%d", &t, &u);
p[i] = Node(, t, u, i);
hashmap[hcnt++] = t; hashmap[hcnt++] = u;
tl[++tcnt] = t; tr[tcnt] = u;
}
if(c == 'C') {
scanf("%d", &t);
p[i] = Node(-, tl[t], tr[t], i);
}
if(c == 'Q') {
scanf("%d%d", &t, &u);
p[i] = Node(, t, u, i);
hashmap[hcnt++] = t; hashmap[hcnt++] = u;
}
}
sort(hashmap, hashmap + hcnt);
hcnt = unique(hashmap, hashmap + hcnt) - hashmap;
for(int i = ; i <= n; ++i) {
p[i].l = hash(p[i].l); p[i].r = hash(p[i].r);
}
memset(ans + , , n * sizeof(int));
solve(, n);
for(int i = ; i <= n; ++i)
if(p[i].v == ) printf("%d\n", ans[i]);
}
}

ACdream 1157 Segments(CDQ分治)的更多相关文章

  1. ACdream 1157 Segments CDQ分治

    题目链接:https://vjudge.net/problem/ACdream-1157 题意: Problem Description 由3钟类型操作: 1)D L R(1 <= L < ...

  2. 【ACdream】1157 Segments cdq分治

    Segments   Problem Description 由3钟类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i ...

  3. ACdream 1157 (cdq分治)

    题目链接 Segments Time Limit: 4000/2000MS (Java/Others)Memory Limit: 20000/10000KB (Java/Others) Problem ...

  4. ACdream 1157 Segments

    Segments Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on ACdream. Original I ...

  5. ACdream1157 Segments(CDQ分治 + 线段树)

    题目这么说的: 进行如下3种类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i (1-base) 删除第i条增加的线段, ...

  6. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  7. BZOJ 2683 简单题 ——CDQ分治

    [题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...

  8. HDU5618 & CDQ分治

    Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...

  9. 初识CDQ分治

    [BZOJ 1176:单点修改,查询子矩阵和]: 1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 200 ...

随机推荐

  1. [daily][device] linux挂载iphone

    头几个月去旅游,亲戚的iphone照了好多照片,空间不足.就备份在了我的电脑上. 那么问题就是如何在linux系统里挂载iphone? 我找到了这篇文档,然而我没看. https://wiki.arc ...

  2. 怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图?

      分类: OpenCV [Q1]怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图? 进行灰度化,IplImage* pImg = cvLoadImage( "C:\\1.b ...

  3. 关于Memo或者Edit之类控件, 直接设置Text无法撤销的解决方案

    昨天看到群里有人问使用Memo1.Text := '11111';来设置内容的代码无法使用Memo1.Undo的方式来撤销 测试了一下果然如此, 跟踪了VCL代码, 发现Text := '11111' ...

  4. 国外it网站收集

    1.http://news.com.com/2.http://www.zdnet.com/3.http://www.salon.com/tech/index.html4.http://www.brin ...

  5. Socket通信原理探讨(C++为例) good

    http://www.cnblogs.com/xufeiyang/articles/4878096.html http://www.cnblogs.com/xufeiyang/articles/453 ...

  6. Using Change Management and Change Control Within a Project

    In any project, change is inevitable whether it comes from within the project or from external sourc ...

  7. c#网页爬虫初探

    一个简单的网页爬虫例子! html代码: <head runat="server"> <title>c#爬网</title> </head ...

  8. shell中引用其他脚本的方法

    在Shell中引用其他脚本的方法是source   filename.sh 或    .    filename.sh 注意:      .   和   filename.sh之间必须有空格

  9. python判断key是否在字典用in不用has_key

    小测试 in del.py import datetime cur = datetime.datetime.now() num = 1 a_list = {"a":1, " ...

  10. mySQL 教程 第7章 存储过程和函数

    存储过程和存储函数 MySQL的存储过程(stored procedure)和函数(stored function)统称为stored routines. 1. MySQL存储过程和函数的区别 函数只 ...