Segments

Time Limit: 2000ms
Memory Limit: 10000KB

This problem will be judged on ACdream. Original ID: 1157
64-bit integer IO format: %lld      Java class name: (No Java Yet)

 

由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 8

Sample Output

2
1

Hint

注意,删除第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

 

Source

 
解题:CDQ分治
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct QU{
int l,r,id,v;
bool operator<(const QU &t) const{
if(r != t.r) return r > t.r;
if(l != t.l) return l < t.l;
return id < t.id;
}
bool operator>(const QU &t) const{
return id < t.id;
}
}Q[maxn];
int bit[maxn],Li[maxn],ans[maxn],tot,n;
void update(int i,int val){
for(; i <= tot; i += i&(-i))
bit[i] += val;
}
int sum(int i,int ret = ){
for(; i > ; i -= i&(-i))
ret += bit[i];
return ret;
}
int d[maxn],cnt;
void cdq(int L,int R){
if(R <= L) return;
int mid = (L + R)>>;
cdq(L,mid);
cdq(mid+,R);
sort(Q+L,Q+R+);
for(int i = L; i <= R; ++i){
if(Q[i].id <= mid && Q[i].v) update(Q[i].l,Q[i].v);
if(Q[i].id > mid && !Q[i].v) ans[Q[i].id] += sum(Q[i].l);
}
for(int i = L; i <= R; ++i)
if(Q[i].id <= mid && Q[i].v) update(Q[i].l,-Q[i].v);
}
int main(){
char op[];
while(~scanf("%d",&n)){
memset(bit,,sizeof bit);
memset(ans,,sizeof ans);
for(int i = cnt = tot = ; i < n; ++i){
scanf("%s",op);
Q[i].id = i;
if(op[] == 'D'){
scanf("%d%d",&Q[i].l,&Q[i].r);
d[cnt++] = i;
Q[i].v = ;
Li[tot++] = Q[i].l;
Li[tot++] = Q[i].r;
}else if(op[] == 'Q'){
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].v = ;
Li[tot++] = Q[i].l;
Li[tot++] = Q[i].r;
}else{
scanf("%d",&Q[i].l);
Q[i].r = Q[d[Q[i].l-]].r;
Q[i].l = Q[d[Q[i].l-]].l;
Q[i].v = -;
}
}
sort(Li,Li + tot);
tot = unique(Li, Li + tot) - Li;
for(int i = ; i < n; ++i){
Q[i].l = lower_bound(Li, Li + tot,Q[i].l) - Li + ;
Q[i].r = lower_bound(Li, Li + tot,Q[i].r) - Li + ;
}
cdq(,n-);
sort(Q,Q+n,greater<QU>());
for(int i = ; i < n; ++i)
if(!Q[i].v) printf("%d\n",ans[Q[i].id]);
}
return ;
}

ACdream 1157 Segments的更多相关文章

  1. ACdream 1157 Segments(CDQ分治)

    题目链接:http://acdream.info/problem?pid=1157 Problem Description 由3钟类型操作:1)D L R(1 <= L <= R < ...

  2. ACdream 1157 Segments CDQ分治

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

  3. 【ACdream】1157 Segments cdq分治

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

  4. ACdream 1157 (cdq分治)

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

  5. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  6. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

  7. Application package 'AndroidManifest.xml' must have a minimum of 2 segments.

    看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...

  8. segments&cache

    Segments 执行效果 命令  在 sense 里边执行  GET /abcd/_segments  前边的是索引名称,后边是请求 段信息 说明  索引是面向分片的,是由于索引是由一个或多个分片( ...

  9. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

随机推荐

  1. asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值

    asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...

  2. 《大话操作系统——做坚实的project实践派》(2)

      <大话操作系统--做坚实的project实践派>(2) 

  3. 【opencv】opencv在vs下的配置(持续更新)

    经常使用配置记录,会更新下去. 1.去掉ipch及.sdf文件 opencv在vs编译会得到很多文件.当中.dsf和ipch文件就有几十M.总是非常占空间,而这都是用来保存C++预编译的头文件和Int ...

  4. 在nodejs使用Redis缓存和查询数据及Session持久化(Express)

    在nodejs使用Redis缓存和查询数据及Session持久化(Express) https://segmentfault.com/a/1190000002488971

  5. 捣鼓TinyMCE 粘贴图片并上传+Django后台

    前面一篇写了上传到Flask后台,但是我不熟悉Flask,原先想学习一下,据说是轻量级. 但是我发现,学习会浪费我大量的时间,因为我并不是以这个为生的,我的目标只是要完成功能,让我自己能尽早使用起来, ...

  6. Excel里的多列求和(相邻或相隔皆适用)

    最近,需要这个知识点,看到网上各种繁多复杂的资料,自己梳理个思路. 不多说,直接上干货! 简述:将L列.M列和N列,相加放到O列.(当然这里是相邻的列).同时,也可以求相隔几列的某些列相加.

  7. C# MVC登录判断状态

    public class AuthenAdminAttribute:FilterAttribute,IAuthorizationFilter { public void OnAuthenticatio ...

  8. DB2load遇到SQL3508N错误

    SQL3508N装入或装入查询期间,当存取类型为 "<文件类型>" 的文件或路径时出错.原因码:"<原因码>".路径:"< ...

  9. 手机号获取省份,城市api的使用

    function get_mobile_area($mobile){ header('Content-Type:text/html;charset=gbk'); $url = 'http://life ...

  10. Data type-数据类型

    操作方式.含义.存储方式. In computer science and computer programming, a data type or simply type is a classifi ...