题目:

Description

There are only one case in each input file, the first line is a integer N (N ≤ 1,000,00) denoted the total operations executed by Mary.

Then following N lines, each line is one of the folling operations.

  • D L R : draw a segment [L, R], 1 ≤ L ≤  R ≤ 1,000,000,000.
  • C I : clear the ith added segment. It’s guaranteed that the every added segment will be cleared only once.
  • Q L R : query the number of segment sharing at least a common point with interval [L, R]. 1 ≤ L ≤ R ≤ 1,000,000,000.

Input

n

Then following n operations ...

Output

For each query, print the result on a single line ...

Sample Input

6
D 1 3
D 2 4
Q 2 3
D 2 4
C 2
Q 2 3

Sample Output

2
2   题意:给出三种操作:①画一条占据[L,R]区间的线段,②去掉前面画的第i条线段(保证合法),③查询一条占据区间[L,R]的线段与前面多少条线段至少有一个公共点。对于③操作,输出结果。
区间范围[1,1000000000],操作最多100000次。
  区间很大,但是操作相对比较少,所以可以先对坐标离散化,然后缩小了范围以后再对数据操作。
  怎样去统计有多少条线段覆盖了某一段?这里用的方法是统计线段的两个端点,用树状数组来记录。这里需要开两个树状数组,一个用来记录左端点,一个用来记录右端点,然后每一次求某一段[L,R]有多少条线段经过的话,我们可以先求出有多少条线段的左端点在R的左边,这里只要求前段和就可以了,然后我们再求一下有多少线段的右端点在L的左边,同样求一次和,然后相减就可以得到结果了。
  为什么可以这样做?首先是因为线段有两个端点,然后两次求和都直接排除了R右边的无关线段,而在L左边的线段的两个端点都在L的左边,所以就会先算了一次,然后再被减回去。 代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define lowbit(x) (x & (-x))
#define MAX 100201
#define LL long long
using namespace std; typedef struct{
LL l,r,k;
char ch;
}Seg;
Seg s[MAX];
LL l[MAX<<],r[MAX<<];
vector<LL> d;
LL tot,no,N[MAX]; void add(LL p[],LL x,LL e){
for(;x<=tot;x+=lowbit(x)) p[x]+=e;
} LL query(LL p[],LL x){
LL ans=;
for(;x>;x-=lowbit(x)) ans+=p[x];
return ans;
} int main()
{
LL n,loc,ans;
//freopen("data.txt","r",stdin);
ios::sync_with_stdio(false);
while(cin>>n){
d.clear();
d.push_back(-(<<));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
no=;
for(LL i=;i<n;i++){
cin>>s[i].ch;
if(s[i].ch=='C'){
cin>>s[i].k;
}else{
cin>>s[i].l>>s[i].r;
if(s[i].ch=='D') N[++no]=i;
d.push_back(s[i].l);
d.push_back(s[i].r);
}
}
sort(d.begin(),d.end());
d.erase(unique(d.begin(),d.end()),d.end());
tot = (LL)d.size()-;
for(LL i=;i<n;i++){
if(s[i].ch=='D'){
loc = lower_bound(d.begin(),d.end(),s[i].l) - d.begin();
add(l,loc,);
loc = lower_bound(d.begin(),d.end(),s[i].r) - d.begin();
add(r,loc,);
}else if(s[i].ch=='C'){
LL& e = N[s[i].k];
loc = lower_bound(d.begin(),d.end(),s[e].l) - d.begin();
add(l,loc,-);
loc = lower_bound(d.begin(),d.end(),s[e].r) - d.begin();
add(r,loc,-); }else{
loc = lower_bound(d.begin(),d.end(),s[i].r) - d.begin();
ans = query(l,loc);
loc = lower_bound(d.begin(),d.end(),s[i].l) - d.begin();
ans-= query(r,loc-);
cout<<ans<<endl;
}
}
//cout<<endl;
}
return ;
}

Crayon

最新文章

  1. GPS部标平台的架构设计(四)-百度地图设计
  2. C程序编译过程浅析
  3. 你真的了解UIApplication吗?
  4. iOS钥匙串
  5. 一个PHP数组能占多大内存
  6. J2EE事务
  7. 【HDOJ】1648 Keywords
  8. 教你50招提升ASP.NET性能(七):总是在服务器端执行验证
  9. Ollydbg 中断方法浅探
  10. VC++6.0打开文件出错的解决办法
  11. 修复intellij idea 2017.2中文输入法无候选框,亲测可以用
  12. js+jq实现图片预览,支持到ie9+ff+chrome
  13. webpack下css/js/html引用图片的正确方式
  14. php 多维数组转二维数组
  15. $.ajax({})方法中的回调函数beforeSend,success,complete,error使用示例
  16. day13 生成器 三元运算 列表解析
  17. CF321E Ciel and Gondolas
  18. 纯 Java 开发 WebService 调用测试工具(wsCaller.jar)
  19. 如何从Windows中删除Node.js
  20. Django介绍(3)

热门文章

  1. Karma和Jasmine自动化单元测试——本质上还是在要开一个浏览器来做测试
  2. Alignment(dp)
  3. java 中接口的概念
  4. .Net Core(二) 下
  5. android 国际化 横屏(land) 竖屏(port)margin外边距和padding内边距
  6. js实现“级联菜单”
  7. dubbo之结果缓存
  8. Python 遍历目录
  9. JavaScript中原生事件
  10. 上传菜品数据&amp;生成点餐二维码