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的左边,所以就会先算了一次,然后再被减回去。 代码:
#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的更多相关文章
- ACdream 1214---矩阵连乘
ACdream 1214---矩阵连乘 Problem Description You might have noticed that there is the new fashion among r ...
- acdream.LCM Challenge(数学推导)
LCM Challenge Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
- acdream.Triangles(数学推导)
Triangles Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Stat ...
- acdream.A Very Easy Triangle Counting Game(数学推导)
A - A Very Easy Triangle Counting Game Time Limit:1000MS Memory Limit:64000KB 64bit IO Forma ...
- acdream.Bet(数学推导)
Bet Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- acdream.郭式树(数学推导)
郭式树 Time Limit:2000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit Status Pr ...
- ACdream 1188 Read Phone Number (字符串大模拟)
Read Phone Number Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Sub ...
- ACdream 1195 Sudoku Checker (数独)
Sudoku Checker Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
- ACdream 1112 Alice and Bob(素筛+博弈SG函数)
Alice and Bob Time Limit:3000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- pat1013:数素数
https://www.patest.cn/contests/pat-b-practise/1013 #include "stdio.h" #include "math. ...
- oc59--匿名分类
// // main.m // 匿名分类(延展) // 可以为某个类扩展私有的成员变量和方法,写在.m文件中, // 分类不可以扩展属性,分类有名字,匿名分类没有名字. #import <Fou ...
- linux驱动由浅入深系列:tinyalsa(tinymix/tinycap/tinyplay/tinypcminfo)音频子系统之一【转】
本文转载自:http://blog.csdn.net/radianceblau/article/details/64125411 目前linux中主流的音频体系结构是ALSA(Advanced Lin ...
- 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分
题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...
- E20170911-hm
specification n. 规格; 说明书; 详述;
- 数据通讯与网络 第五版第24章 传输层协议-UDP协议部分要点
24.1 介绍 本章节主要集中于传输层协议的解读,图24.1展示TCP.UDP.SCTP在TCP\IP协议栈的位置 24.1.1 服务(Service) 每个协议都提供不同的服务,所以应该合理正确的使 ...
- BZOJ 2101 DP+优化
思路: http://www.cnblogs.com/exponent/archive/2011/08/14/2137849.html f[i,i+len]=sum[i,i+len]-min(f[i+ ...
- 【Codeforces】Codeforces Round #373 (Div. 2)
B. Anatoly and Cockroaches Anatoly lives in the university dorm as many other students do. As you kn ...
- 快速录入快递地址API接口实现
电商.ERP等行业下单环节极其重要,如何提高下单的效率已经成为首要问题.快速下单对于客户来说,为提前发货争取了时间:对于卖家来说,提高了库存周转率及利用率.快速下单的接口实现,需要解决如下几个问题:1 ...
- php判断方法及区别
php判断方法 ‘is_类型名称’ php判断方法 $x="1"; echo gettype(is_string($x)); isset 是否存在 empty 是否 ...