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 ...
随机推荐
- iOS 简易型标签的实现(UICollectionView)
https://blog.csdn.net/sinat_39362502/article/details/80900984 2018年07月03日 16:49:05 Recorder_MZou 阅读数 ...
- git中如何撤销部分修改?
以提问中修改了两个文件a.b为例,假设需要撤销文件a的修改,则修改后的两个文件: 1.如果没有被git add到索引区 git checkout a 便可撤销对文件a的修改 2.如果被git add到 ...
- Linux 系统巡检常用命令
Linux系统巡检常用命令 # uname -a # 查看内核/操作系统# cat /etc/centos-release # 查看centos操作系统版本# cat /proc/cpuinfo ...
- linux 运维工程师发展路线
linux运维发展常见的就是下面两条路线:第一条:运维应用-->系统架构-->运维开发-->系统开发第二条:运维应用-->应用dba-->架构dba-->开发DBA ...
- 其它综合-VMware虚拟机安装Ubuntu 19.04 版本
Ubuntu 19.04 版本安装过程 1. 环境: 使用的虚拟机软件是VMware,版本为 12 .(网上一搜一大推,在此不再演示.) 使用的 ISO镜像为Ubuntu 19.04.(自己也可以在网 ...
- 配置启动MySQL的Docker容器
docker run -d -p : --name mysql -e MYSQL_ROOT_PASSWORD= mysql:
- Mdoelsim10.4怎么脚本单独仿真ISE14.7 IP核
软件版本: Modelsim10.4SE ISE14.7 仿真IP:时钟管理IP(clock wizard) 流程: 1.对于Modelsim10.4SE,并不自带Xilinx家的仿真库,因此首先 ...
- Linux(Ubuntu)换apt-get源
在虚拟机安装完Ubuntu后,因为apt-get命令默认的服务器在国外会很慢,换成国内的会快很多 选一个国内镜像源,以清华大学开源镜像为例,要选对应的Ubuntu版本 网站链接https://mirr ...
- Python【第一篇】python安装、pip基本用法、变量、输入输出、流程控制、循环
一.python安装 Ubuntu下 系统版本已经同时安装了python2和python3 如果没有python3,可以参考这个貌似是印度阿三的安装视频:http://v.youku.com/v_sh ...
- C++:位操作基础篇之位操作全面总结
位操作篇共分为基础篇和提高篇,基础篇主要对位操作进行全面总结,帮助大家梳理知识.提高篇则针对各大IT公司如微软.腾讯.百度.360等公司的笔试面试题作详细的解答,使大家能熟练应对在笔试面试中位操作题目 ...