RDay2-Problem 2 B
题目描述
小明家有n个信箱,前前后后来送信和取信的总次数为q,称为q次访问,其中这q次访问分成三种类型。
1:邮递员送来了一封信,放在了x号信箱。
2:小明取走了x号信箱的所有信(x信箱可能已经没有信了)。
3:小明取走了前t封送来的信(前t封表示从送来的第一封到送来的第t封,其中这t封信可能已经通过第二类或者之前的第三类访问取走了)
小明现在想要知道每一次访问之后,有多少封信时没有取走的,由于送来的信太多,小明想请学oi的你来解答。
输入
输入文件B.in
第一行两个整数n,q。
接下来q行,每行最开始一个整数type
若type=1紧接着一个整数x,表示第一类操作。
若type=2紧接着一个整数x,表示第二类操作。
若type=3紧接着一个整数t,表示第三类操作。
输出
输出文件B.out
对于每一次访问,输出访问结束时剩下多少信还没有被取走。
样例输入
3 4
1 3
1 1
1 2
2 3
样例输出
1
2
3
2
提示
【样例输入2】
4 6
1 2
1 4
1 2
3 3
1 3
1 3
【样例输出2】
1
2
3
0
1
2
【数据范围】
对于30%的数据,$ n,q \le 1000 \(
对于另外20%的数据,没有三操作。
对于100%的数据,\) n,q \le 300000 $
这个题说来我挺zz的,完全不知道自己在想什么.....
直接线段树 + 链表就能怼过去的事,结果我非要写线段树 + 时间戳 + 删除线段
这不是给自己找麻烦嘛....于是写了一会儿果断放弃(反正本来也不太会)
于是就开始 \(YY\) 怎么写链表了,写的时候被自己蠢到哭,单点修改非要写成左右端点相同的区间修改
觉得不用自上向下更新,就死活不想写 $ tag $ ,结果就是一直WA.....
改正之后好歹能过了~~
用线段树维护链表,其实是一堆链表...有点邻接表的意思
然后每次操作是对链表的,注意 $ 2 $ 操作别忘了把一整个链表删干净...最后别忘了重置链表
$ 3 $ 操作直接在线段树上区间修改就行了,也不难
$ 1 $ 操作先在插入链表再更新至线段树,注意线段树维护的是链表就行了
代码如下:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#define Noip2018RpINF return 0
#define ls ( rt << 1 )
#define rs ( rt << 1 | 1 )
#define mid ( ( l + r ) >> 1 )
#define pushup(rt) t[rt].data = t[ls].data + t[rs].data
#define LL long long
const LL N = 3e5 + 5 ;
struct seg{
LL left , right ;
LL data , tag;
}t[ ( N << 2 ) ];
LL n , m , x , tot ;
LL opt , pre[N] , nxt[N] ;
inline void build ( LL rt , LL l , LL r ){
t[rt].left = l ; t[rt].right = r ;
if ( l == r ) return ;
build ( ls , l , mid ) ; build ( rs , mid + 1 , r ) ;
pushup(rt) ; return ;
}
inline void insert (LL rt , LL pos){
LL l = t[rt].left , r = t[rt].right ;
if ( l == r ) { t[rt].data = 1 ; return ; }
if ( pos <= mid ) insert ( ls , pos ) ;
else if ( pos > mid ) insert ( rs , pos );
pushup (rt) ; return ;
}
inline void delet (LL rt , LL ll , LL rr){
if (t[rt].tag) return ;//现在来看,不写tag真是太zz了
LL l = t[rt].left , r = t[rt].right ;
if ( ll <= l && r <= rr ) { t[rt].data = 0 ; t[rt].tag = 1 ; return ; }
if ( ll <= mid ) delet( ls , ll , rr );
if ( rr > mid ) delet( rs , ll , rr );
pushup(rt) ; return ;
}
inline void Rinsert (LL x){
nxt[++tot] = pre[x] ; pre[x] = tot ;
insert( 1 , tot ) ; return ; //把修改tot写成修改x真是太zz了
}
inline void Rdelete (LL x){
for (int i = pre[x] ; i ; i = nxt[i] )
delet( 1 , i , i );
pre[x] = 0 ;//忘了归零真是太zz了
return ;
}
int main(){
scanf ("%lld%lld" , & n , & m );
build( 1 , 1 , m ) ;//忘了建树真是太zz了(太真实了)
while ( m -- ){
scanf ("%lld%lld" , & opt , & x ) ;
if ( opt == 1 ) Rinsert ( x ) ;
if ( opt == 2 ) Rdelete ( x ) ;
if ( opt == 3 ) delet( 1 , 1 , x ) ;
printf ("%lld\n" , t[1].data ) ;
}
Noip2018RpINF ;
}
RDay2-Problem 2 B的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- 基于idea的springcloud的helloworld项目搭建过程整理
Springcloud的搭建主要包括三个部分:服务注册中心.服务提供者.服务消费者.每一个部分都是一个springboot项目,它们通过配置文件(application.properties或appl ...
- 妙解Servlet四大域对象
pageContext pageContext作用域为page(页面执行期). request request是表示一个请求,只要发出一个请求就会创建一个request,它的作用域仅在当前请求中有效. ...
- JS中的块级作用域,var、let、const三者的区别
1. 块作用域{ } <script type="text/javascript"> { var a = 1; console.log(a); // 1 } conso ...
- 前端jquery 获取select多选的值
当select设置属性multiple='multiple'时, option就可以多选了 那么我们如何获取所有被选中的option的值呢? 首先说明: $('select[name="m ...
- Codeforces Round #513 总结
首次正式的$Codeforces$比赛啊,虽然滚粗了,然而终于有$rating$了…… #A Phone Numbers 签到题,然而我第一次写挂了(因为把11看成8了……) 只需要判断一下有多少个 ...
- Django_ORM_字段属性
Django_ORM_字段属性 常用字段 AutoField int自增列,必填参 primary_key=True 默认会自动创建一个列名为id的列 IntegerField 一个整数类型,范围在 ...
- l2tp pptp相关的一些记录
添加用户名和密码 echo "user l2tpd password *">>/etc/ppp/chap-secrets /etc/ipsec.conf dpddela ...
- Python3 与 C# 并发编程之~ 协程篇
3.协程篇¶ 去年微信公众号就陆陆续续发布了,我一直以为博客也汇总同步了,这几天有朋友说一直没找到,遂发现,的确是漏了,所以补上一篇 在线预览:https://github.lesschina.c ...
- 去掉 Chrome(V66) 新标签页的8个缩略图
1.Chrome程序资源文件路径: C:\Program Files (x86)\Google\Chrome\Application\66.0.3359.181\resources.pak 2.下载C ...
- freemarker和thymeleaf的使用样例
最近需要对公司项目首页使用Java模板重做,以提高首屏加载速度和优化SEO. 在选择模板时发现freemarker和thymeleaf最为常用. 两者最大的区别在于语法,对性能方面未作测试,具体性能测 ...