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 ...
随机推荐
- docker(七) 使用dockerfile-maven-plugin插件构建docker镜像
在dockerfile-maven-plugin插件出现之前,还有一个maven插件是docker-maven-plugin,是由同一个作者创造,作者明确表示推荐使用dockerfile-maven- ...
- java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Mon Oct 01 16:32:37 CS
使用idea工具更改项目包名时报 :java.lang.ClassNotFoundException 解决方案: 1.删除项目的target目录,这个目录存放的是编译后的文件 2.清除缓存 3.重新编 ...
- python之数据类型补充、集合、深浅copy
一.内容回顾 代码块: 一个函数,一个模块,一个类,一个文件,交互模式下,每一行就是一个代码块. is == id id()查询对象的内存地址 == 比较的是两边的数值. is 比较的是两边的内存地址 ...
- 转:ls用法详解
在Linux中显示文件大小的时候,通常的做法是使用“ls -l”,显示的大小是文件的字节大小. 但是,如果文件比较大的话,显示起来不是特别易读,这个时候,可以使用“ls -lh”,就可以使用比较接近文 ...
- 清北澡堂 Day2 下午 一些比较重要的数论知识整理
1.欧拉定理 设x1,x2,.....,xk,k=φ(n)为1~n中k个与n互质的数 结论一:axi与axj不同余 结论二:gcd(axi,n)=1 结论三:x1,x2,...,xk和ax1,ax2, ...
- 10.3 Vue 路由系统
Vue 路由系统 简单示例 main.js import Vue from 'vue' import App from './App.vue' //https://router.vuejs.or ...
- HTTP 404 Not Found Error with .woff or .woff2 Font Files
如果是 ERR_ABORTED 404 (Not Found) WOFF2,则有可能是文件不存在
- 【转】Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。
@2019-02-28 [小记] Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印.
- 初识 go 语言:方法,接口及并发
目录 方法,接口及并发 方法 接口 并发 信道 结束语 前言: go语言的第四篇文章,主要讲述go语言中的方法,包括指针,结构体,数组,切片,映射,函数闭包等,每个都提供了示例,可直接运行. 方法,接 ...
- 和CISSP并肩的信息安全认证国际注册信息安全经理CISM
众所周知,信息安全认证界有一个扛把子的证书叫CISSP(国际信息安全专家认证),一般拥有CISSP证书的小哥哥还会选择考取另一个认证,这就是今天给大家介绍的CISM(国际注册信息安全经理).CISM是 ...