DAY5 - T1
T1:
题目
题目描述
VFK家有n个信箱,送信和取信的总次数为q,称为q次访问。
其中这q次访问分成三种类型。
1:使者送来了一封信,放在了x号信箱。
2:VFK取走了x号信箱的所有信(x信箱可能已经没有信了)。
3:VFK取走了前t封送来的信(其中这t封信可能已经通过第二类访问取走了)
VFK现在想要知道每一次访问之后,有多少封信时没有取走的,由于送来的信太多,VFK想请学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
输入
4 6
1 2
1 4
1 2
3 3
1 3
1 3
输出
1
2
3
0
1
2
数据
对于30%的数据,n,q<=1000。
对于另外30%的数据,没有三操作。
对于100%的数据,n,q<=300000。
题解
30%
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int n, q, type, tot, a[300010], num;
int main(){
// freopen("B.in", "r", stdin);
// freopen("B.out", "w", stdout);
scanf("%d%d", &n, &q);
while(q--){
scanf("%d", &type);
int x;
if(type == 1){
scanf("%d", &x);
tot++;
a[tot] = x;
num++;
}
if(type == 2){
scanf("%d", &x);
for(int i = 1; i <= tot; i++){
if(a[i] == x){
num--;
a[i] = 0;
}
}
}
if(type == 3){
scanf("%d", &x);
for(int i = 1; i <= x; i++){
if(a[i]){
num--;
a[i] = 0;
}
}
}
printf("%d\n", num);
}
return 0;
}
100%
#include <bits/stdc++.h>
using namespace std;
int n, q, x, y;
bool bo[300001];
queue<int> a[300001];
int main(){
scanf("%d%d", &n, &q);
int cnt = 0, ans = 0, nows = 1;
for(int i = 1; i <= q; i++){
scanf("%d%d", &x, &y);
if(x == 1){
cnt++;
a[y].push(cnt);
ans++;
}
if(x == 2){
while(!a[y].empty()){
int k = a[y].front();
if(!bo[k]){
ans--;
}
bo[k] = 1;
a[y].pop();
}
}
if(x == 3){
if(y >= nows){
for(int i = nows; i <= y; i++){
if(!bo[i]){
ans--;
}
bo[i] = 1;
}
}
nows = max(nows, y+1);
}
printf("%d\n", ans);
}
return 0;
}
没有未来的未来不是我想要的未来

浙公网安备 33010602011771号