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 ...
随机推荐
- Android Back键和Home键的区别
back键 Android的程序无需刻意的去退出,当你一按下手机的back键的时候,系统会默认调用程序栈中最上层Activity的Destroy()方法来,销毁当前Activity.当此Activit ...
- How to use shared model by git in sql source control of red gate
1.clone the git repository for datbase 2.open sql source control window and select the target databa ...
- bzoj2120 数颜色——带修莫队
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 带修改的莫队: 用结构体存下修改和询问,排好序保证时间后就全局移动修改即可: 参考了T ...
- E20170907-ts
flash vt. 使闪光,使闪烁; 拍出,发出(电报等); 〈口〉炫耀; adj. 闪光的,闪耀的,一闪而过的; 浮华的; 庞大的; n. 闪光; 闪光灯下摄 ...
- web动画小结
前端写动画,无非两种方案,一种是通过css,另一种是js css的方案: 1.transform的单独使用 (IE9+) rotate(90deg) 2d旋转,也可以理解为沿着3D的Z轴旋转 rota ...
- Visual Studio q启动卡顿
在开发人员CMD下面执行 Devenv.exe /ResetSettings ,然后顺利打开 总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAs ...
- 【LuoguP5004】 专心OI - 跳房子
首先这是一道计数类DP,那我们得先推式子,经过瞎掰乱凑,经过认真分析,我们可以得到这样的方程 F(N)=F(0)+F(1)+....+F(N-M-1) 所有F初值为1,F(1)=2 ANS=F(N+M ...
- android指纹识别、拼图游戏、仿MIUI长截屏、bilibili最美创意等源码
Android精选源码 一个动画效果的播放控件,播放,暂停,停止之间的动画 用 RxJava 实现 Android 指纹识别代码 Android仿滴滴打车(滴滴UI)源码 Android高仿哔哩哔哩动 ...
- 设置浏览器让js报错
ie-工具---internet选项--高级--“禁用脚本提示”前面那个框的勾去掉---“显示每个脚本错误的通知”给该项打勾 注意:此时是静态页面很容易提示出错误的行号,但是当js是动态页面的时候,浏 ...
- SLAM: Structure From Motion-移动中三维场景重建
wiki链接:https://en.wikipedia.org/wiki/Structure_from_motion 三维重建: 三维物体建模总结 1. 视野内三维物体重建 : Kinect fusi ...