【CodeForces - 705C】Thor(模拟,STLset优化链表)
題干:
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are?n?applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q?events are about to happen (in chronological order). They are of three types:
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers?n?and?q?(1?≤?n,?q?≤?300?000)?— the number of applications and the number of events to happen.
The next?q?lines contain the events. The?i-th of these lines starts with an integer?typei?— type of the?i-th event. If?typei?=?1?or?typei?=?2?then it is followed by an integer?xi. Otherwise it is followed by an integer?ti?(1?≤?typei?≤?3,?1?≤?xi?≤?n,?1?≤?ti?≤?q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4 1 3 1 1 1 2 2 3Output
1 2 3 2Input
4 6 1 2 1 4 1 2 3 3 1 3 1 3Output
1 2 3 0 1 2Note
In the first sample:
In the second sample test:
題目大意:
第一行兩個整數n,m,代表n個軟件,m次操作。(1?≤?n,?q?≤?300?000)?
接下來m行:
1 x :軟件 x 產生了一條未讀信息。
2 x: 軟件 x 的所有信息都被讀了。
3? t:?前 t 條產生的信息都被讀了。注意是前t條,不是未讀的前t條,已讀的也算在t條中。
解題報告:
? 本來想用個雙向鏈表來啊維護出現過的所有信息,但是發現它可以支持快速的刪除一個元素,但是對于查找前t條信息比較雞肋,甚至還要拿個并查集去搞,比較麻煩,于是換了各項性能都還說的過去的set,直接二分找前t條信息就可以了,這樣最后的答案就是set的大小。
?注意不能讓標號直接等于i,因為第i個操作不一定是產生一天新的信息。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 5e5 + 5; int n,m,last,id; set<int> ss[MAX],q;//存鏈表,因為值直接就是索引就可以了,不需要單獨弄鏈表了int main() {cin>>n>>m;for(int op,x,i = 1; i<=m; i++) {scanf("%d%d",&op,&x);if(op == 1) {id++;ss[x].insert(id);q.insert(id);}if(op == 2) {for(auto idx : ss[x]) {if(q.find(idx) != q.end())q.erase(idx);}ss[x].clear();}if(op == 3) {auto upp = q.upper_bound(x),down = q.begin();q.erase(down,upp);last = max(last,x);}printf("%d\n",q.size());}return 0 ; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 705C】Thor(模拟,STLset优化链表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广东清远泄洪现场引围观:15孔闸口全开
- 下一篇: 汇添富杨瑨的投资策略有哪些?杨瑨的投资风