题目:

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

ACDream - Crayon的更多相关文章

  1. ACdream 1214---矩阵连乘

    ACdream 1214---矩阵连乘 Problem Description You might have noticed that there is the new fashion among r ...

  2. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  3. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  4. acdream.A Very Easy Triangle Counting Game(数学推导)

    A - A Very Easy Triangle Counting Game Time Limit:1000MS     Memory Limit:64000KB     64bit IO Forma ...

  5. acdream.Bet(数学推导)

    Bet Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Status Pra ...

  6. acdream.郭式树(数学推导)

    郭式树 Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  7. ACdream 1188 Read Phone Number (字符串大模拟)

    Read Phone Number Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Sub ...

  8. ACdream 1195 Sudoku Checker (数独)

    Sudoku Checker Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  9. ACdream 1112 Alice and Bob(素筛+博弈SG函数)

    Alice and Bob Time Limit:3000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. oc28--Property增强

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject /* { @public int ...

  2. 2017 Multi-University Training Contest - Team 1 1002&&hdu 6034

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. 自顶向下(递归)的归并排序和自底向上(循环)的归并排序——java实现

    归并排序有两种实现方式,自顶向下和自底向上.前者的思想是分治法,现将数组逐级二分再二分,分到最小的两个元素后,逐级往上归并,故其核心在于归并.后者的思想相反,采用循环的方式将小问题不断的壮大,最后变成 ...

  4. RabbitMQ 官方NET教程(四)【路由选择】

    在上一个教程中,我们构建了一个简单的日志记录系统. 我们能够广播日志消息给所有你的接收者. 在本教程中,我们将为其添加一个功能 - 我们将让日志接收者可以仅订阅一部分消息. 例如,我们将能够仅将关键的 ...

  5. System.net.mail发送电子邮件

    之前做的实现发送邮件的功能,基于System.net.mail,在本地测试是可以发送邮件的,发布到服务器上发送不了邮件,后来发现STMP默认使用25端口收发邮件,服务器封掉25了端口,导致发送邮件失败 ...

  6. max-age 和 Expires

    网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.   Ex ...

  7. 非内置浏览器WebView 调起H5支付,提示商家参数格式有误

    微信H5 支付开发官方文档参考资料: https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4 0. 场景描述:在APP 中使用webVie ...

  8. 书不在多,精读则灵 - Oracle入门书籍推荐

      作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.com/archives/2006/08/ora ...

  9. php数据库增删改查

    首先建立一个数据库db_0808,将db_0808中表格student导入网页. CURD.php <!DOCTYPE html> <html lang="en" ...

  10. hibernate_05_单表操作_对象类型

    本篇使用hibernate输出一个对象(图片) 先写一个java类 package com.imooc.hibernate; import java.sql.Blob; import java.uti ...