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 ...
 
随机推荐
- 17.kubernete的dashboard
			
部署dashboard kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/depl ...
 - hdu-5786(补图最短路)
			
题意:给你n个点,m条无向边,问你这n个点构成的完全图,不用那m条边,由一个s出现的单源最短路 解题思路:首先,暴力建图不行,点太多,那么我们就按照它的规则来,把m条边建好,但是建的这个图表示不走的方 ...
 - 腾讯笔试---小Q的歌单
			
链接:https://www.nowcoder.com/questionTerminal/f3ab6fe72af34b71a2fd1d83304cbbb3 来源:牛客网 小Q有X首长度为A的不同的歌和 ...
 - 内网ntp时间同步配置
			
选择局域网中的一台机器作为ntp服务器,在ntp server上安装并启动ntpd客户端上要关闭ntpd,安装ntpdateCentOS7上这两个软件都是自带的,只需根据需要打开或者关闭.注意客户端机 ...
 - Day038--Python--Gevent , IO多路复用
			
1. 协程: gevent (遇到IO自动切换) import gevent import time from gevent import monkey; monkey.patch_all() # ...
 - 如何查看C++ dll位数
			
使用VS自带工具 dumpbin dumpbin /headers xxx.dll
 - np.array.all()和np.array.any()函数
			
np.array.all()是对np.array中所有元素进行与操作,然后结果返回True或False np.array.any()是对np.array中所有元素进行或操作,然后结果返回True或Fa ...
 - SQL随记(三)
			
1.关于package: 包的作用:可以将任何出现在块声明的语句(过程,函数,游标,游标,类型,变量)放入包中,相当于一个容器. 包的好处:在包中的(过程,函数,游标,游标,类型,变量)相当于sql/ ...
 - 《JavaScript.DOM》读书笔记
 - 第十二节:Lambda、linq、SQL的相爱相杀(1)
			
一. 谈情怀 Lambda.Linq.SQL伴随着我的开发一年又一年,但它们三者并没有此消彼长,各自占有这一定的比重,起着不可替代的作用. 相信我们最先接触的应该就是SQL了,凡是科班出身的人,大学 ...