题目链接: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. openCV中IplImage的使用

    http://blog.csdn.net/welcome_xu/article/details/7650680 IplImage结构详细分析   IplImage 结构解读: typedef stru ...

  2. javaScript没有块级作用域

    1.如下,变量i,j,k 的作用域是相同的. function test(obj){ var i= 0; if(typeof obj == "object"){ var j = 0 ...

  3. Qt通过QToolTip显示浮动信息

    QToolTip类的应用十分简单,其QToolTip类中全都是静态方法,如果要显示浮动信息的话使用该函数即可: void QToolTip::showText ( const QPoint & ...

  4. 软件工程概论---max单元测试

    题目:一个单元测试,查找list[]中的最大值 编写一个程序对Largest函数进行测试,列举所有测试用例. 思路:首先确保数组不为空,和数组长度不为0,否则输入错误.根据老师所给的函数写一个主函数, ...

  5. const 修饰函数

    At the very first ,I got a problem . Vector Vector::operator+(const Vector &v)const{ return Vect ...

  6. [LeetCode]题解(python):081 - Search in Rotated Sorted Array II

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...

  7. [LeetCode]题解(python):038-Count and Say

    题目来源 https://leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequence of inte ...

  8. Java日期时间处理常用方法

    虽然是老生常谈,但整理出来还是有点用. 1.由字符串时间得到Date类型时间 // 由字符串时间得到Date类型时间 public static Date getDateFrom(String str ...

  9. 怎么使用git来管理项目版本?

    怎么使用git来管理项目版本和存放代码? 作者:rongfangliu 转载请注明出处:http://www.cnblogs.com/rongfangliu/p/howuseGit.html 工具: ...

  10. JavaScript:JavaScript中常见获取对象元素的方法

    介绍: javascript中常见的3种获取元素的方法,分别是通过元素ID.通过标签名字和通过类名字来获取 操作如下: 1.getElementById DOM提供了一个名为getElementByI ...