ACDream - Crayon

题目:

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的左边,所以就会先算了一次,然后再被减回去。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #define lowbit(x) (x & (-x))
 7 #define MAX 100201
 8 #define LL long long
 9 using namespace std;
10
11 typedef struct{
12     LL l,r,k;
13     char ch;
14 }Seg;
15 Seg s[MAX];
16 LL l[MAX<<1],r[MAX<<1];
17 vector<LL> d;
18 LL tot,no,N[MAX];
19
20 void add(LL p[],LL x,LL e){
21     for(;x<=tot;x+=lowbit(x)) p[x]+=e;
22 }
23
24 LL query(LL p[],LL x){
25     LL ans=0;
26     for(;x>0;x-=lowbit(x)) ans+=p[x];
27     return ans;
28 }
29
30 int main()
31 {
32     LL n,loc,ans;
33     //freopen("data.txt","r",stdin);
34     ios::sync_with_stdio(false);
35     while(cin>>n){
36         d.clear();
37         d.push_back(-(1<<30));
38         memset(l,0,sizeof(l));
39         memset(r,0,sizeof(r));
40         no=0;
41         for(LL i=0;i<n;i++){
42             cin>>s[i].ch;
43             if(s[i].ch==‘C‘){
44                 cin>>s[i].k;
45             }else{
46                 cin>>s[i].l>>s[i].r;
47                 if(s[i].ch==‘D‘)    N[++no]=i;
48                 d.push_back(s[i].l);
49                 d.push_back(s[i].r);
50             }
51         }
52         sort(d.begin(),d.end());
53         d.erase(unique(d.begin(),d.end()),d.end());
54         tot = (LL)d.size()-1;
55         for(LL i=0;i<n;i++){
56             if(s[i].ch==‘D‘){
57                 loc = lower_bound(d.begin(),d.end(),s[i].l) - d.begin();
58                 add(l,loc,1);
59                 loc = lower_bound(d.begin(),d.end(),s[i].r) - d.begin();
60                 add(r,loc,1);
61             }else if(s[i].ch==‘C‘){
62                 LL& e = N[s[i].k];
63                 loc = lower_bound(d.begin(),d.end(),s[e].l) - d.begin();
64                 add(l,loc,-1);
65                 loc = lower_bound(d.begin(),d.end(),s[e].r) - d.begin();
66                 add(r,loc,-1);
67
68             }else{
69                 loc = lower_bound(d.begin(),d.end(),s[i].r) - d.begin();
70                 ans = query(l,loc);
71                 loc = lower_bound(d.begin(),d.end(),s[i].l) - d.begin();
72                 ans-= query(r,loc-1);
73                 cout<<ans<<endl;
74             }
75         }
76         //cout<<endl;
77     }
78     return 0;
79 }

Crayon

ACDream - Crayon,布布扣,bubuko.com

时间: 2024-10-19 13:24:54

ACDream - Crayon的相关文章

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

acdream 1738 世风日下的哗啦啦族I

原题链接:http://acdream.info/problem?pid=1738 树套树裸题,如下: 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #define lc root<<1 7 #define rc root<<1|1 8 using std::so

数学 ACdream 1196 KIDx&#39;s Triangle

题目传送门 1 /* 2 这道题花了好长时间AC,思路有,但是表达式少写了括号一直乱码,囧! 3 注意:a==0时要特判:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <cmath> 11 using namespace std; 1

ACdream 之ACfun 题解

A - ACfun Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 题目链接点击打开链接 As a former ACMer, "AC" is a special abbreviated word which can bring much pleasure to me. Sometimes it means

Ubuntu下快速搭建ACdream Online Judge v1.5.3

原文:https://github.com/KIDx/ACdream#%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96%E6%A8%A1%E5%9D%97 安装依赖 $ sudo apt-get update $ sudo apt-get install imagemagick $ sudo apt-get install python-software-properties python g++ make $ sudo apt-get install libcairo2

ACdream 1157 Segments(CDQ分治)

题目链接: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

ACDream - Chasing Girl

先上题目: Chasing Girl Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description YYS 是SCAU_ACM里相当有名气的高富帅, 又因为他曾代表SCAU参加 World Final 而被各路亲朋好友仰慕.但YYS 又有另外一个嗜好,就是泡妞.在大学的时候,他经常去找各个女友玩耍, 但在路上却又整天遇到膜拜他的渣渣

ACDream - Graphs

先上题目: Graphs Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N个点,M条边,问是否存在一个连通子图,子图由原图删掉一些点和边(不删亦可),且叶子数>=4(即度为1的点) Input 多组数据,每组数据N,M(0 <= N <= 10000,0 <= M <= 20000) 接下来M

ACdream 1099 瑶瑶的第K大

瑶瑶的第K大 Time Limit: 10000/5000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) SubmitStatisticNext Problem Problem Description 一天,萌萌的妹子--瑶瑶(tsyao)很无聊,就来找你玩.可是你们都不知道玩什么...尴尬了一阵子,机智的瑶瑶就提议:"这样吧,你说N个整数xi,然后在随意说一个数字k,我能够快速地说出这些数字里面第 k 大的数字."