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 ...
随机推荐
- CodeForces--626C--Block Towers (二分)
Block Towers Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- codevs1358棋盘游戏(状压dp)
1358 棋盘游戏 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初 ...
- go之数据类型转换和类型断言
一.类型转换 1.1 简单类型转换 格式 valueOfTypeB = typeB(valueOfTypeA) int 转 float64 package main import "fmt& ...
- java 实现yaml 数据转json与map
首先引入snakeyaml-1.16.jar的包. 直接上代码: package com.ming.yaml; import java.util.Map; import org.yaml.snakey ...
- 6月7号shiro
Retains all Cache objects maintained by this cache manager :保留此缓存管理器维护的所有缓存对象 Destroyable可毁灭的 retain ...
- HTML学习笔记——DOCTYPE和DTD,标准模式和兼容模式
主要涉及知识点: HTML与XHTML HTML与XHTML的区别 DOCTYPE与DTD的概念 DTD的分类以及DOCTYPE的声明方式 标准模式(Standard Mode)和兼容模式(Quirc ...
- dell inspiron 15 3000 装XP win7 等GHOST系统方法
dell inspiron 装XP win7 等GHOST系统方法 . 开机按F2,进入BIOS .在 BIOS 的Boot菜单下,将Secure Boot 改为 Disabled . 将Boot L ...
- more-less-cat-tail-head 命令简单分析
区别:cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式 操作,适合察看内容短小.不超过一屏的文件:more比cat强大一点,支持分页显示,你可以ctrl+ ...
- ASP.NE 上传文件控件
protected void Button1_Click(object sender, EventArgs e) { //if (Request["id"]==null & ...
- shell脚本—基础知识,变量
shell脚本本质: 编译型语言 解释型语言 shell编程基本过程 1.建立shell文件 2.赋予shell文件执行权限,使用chmod命令修改权限 3.执行shell文件 shell变量: sh ...