1
+ #include < cstdio>
2
+ #include < cstring>
3
+ #include < cmath>
4
+ #include < ctime>
5
+ #include < vector>
6
+ #include < string>
7
+ #include < algorithm>
8
+ #include < map>
9
+ #include < set>
10
+ #include < unordered_map>
11
+ #include < unordered_set>
12
+ #include < queue>
13
+ #include < deque>
14
+ #include < stack>
15
+ #include < numeric>
16
+ #include < memory>
17
+ using namespace std ;
18
+
19
+ #define PB push_back
20
+ #define F first
21
+ #define S second
22
+
23
+ #define REP (i,from,to ) for (auto i=(from); i<=(to); ++i)
24
+ #define PER (i,from,to ) for (auto i=(from); i>=(to); --i)
25
+ #define REP_IF (i,from,to,assert ) for (auto i=(from); i<=(to); ++i) if (assert)
26
+
27
+ #define FOR (i,less_than ) for (auto i=0 ; i<(less_than); ++i)
28
+ #define FORI (i, container ) for (auto i=0 ; i<(container).size(); ++i)
29
+ #define FORI_IF (i, container, assert ) for (auto i=0 ; i<(container).size(); ++i) if (assert)
30
+ #define ROFI (i, container ) for (auto i=(container).size()-1 ; i>=0 ; --i)
31
+
32
+ #define FOREACH (elem, container ) for (auto elem : (container))
33
+ #define FILL (container, value ) memset(container, value, sizeof (container))
34
+ #define ALL (container ) (container).begin(), (container).end()
35
+ #define SZ (container ) (int )((container).size())
36
+
37
+ #define BACK (set_map ) *prev ((set_map).end(), 1)
38
+ #define FRONT (set_map ) *(set_map).begin()
39
+
40
+ inline void _RD (int &x) { scanf (" %d" , &x); }
41
+ inline void _RD (long long &x) { scanf (" %lld" , &x); }
42
+ inline void _RD (double &x) { scanf (" %lf" , &x); }
43
+ inline void _RD (long double &x) { scanf (" %Lf" , &x); }
44
+ inline void _RD (char &x) { scanf (" %c" , &x); }
45
+ inline void RD () {}
46
+ template <class T , class ... U>
47
+ inline void RD (T &head, U &... tail) { _RD (head); RD (tail...); }
48
+
49
+ using PII = pair<int ,int >;
50
+ using LL = long long ;
51
+ using VI = vector<int >;
52
+ using VLL = vector<LL>;
53
+ using VVI = vector<VI>;
54
+
55
+ int main () {
56
+ int n, m;
57
+ scanf (" %d %d\n " , &n, &m);
58
+
59
+ VI s (n+1 , 0 ), c (n+1 , 0 ); // s[i]:initial state of i:1 in, -1 not in, 0 NA
60
+ // c[i]:if i could be a leader: 1 yes, 0 no
61
+ vector<PII> ops;
62
+
63
+ char op; int x, cnt=0 ;
64
+ int first_in = 0 ;
65
+
66
+ unordered_set<int > in, out; // set of ids in/out of the room
67
+ // all are candidates
68
+
69
+ REP (i, 1 , m) {
70
+ scanf (" %c %d\n " , &op, &x);
71
+ if (op==' +' ) {
72
+ if (s[x]==0 ) s[x] = -1 ; // x initially not in room
73
+ if (!first_in) first_in = x;
74
+ } else {
75
+ if (s[x]==0 ) { // x initially in room
76
+ s[x] = c[x] = 1 ; // x is potentially a leader
77
+ in.insert (x);
78
+ ++cnt;
79
+ }
80
+ }
81
+ ops.PB ({op==' +' ?1 :-1 , x});
82
+ }
83
+
84
+ VI ans;
85
+
86
+ // all those not shown in the sequence could be a leader
87
+ REP_IF (i, 1 , n, s[i]==0 ) c[i] = 1 ;
88
+
89
+ // not one initially in the room, so the first added one could be a leader
90
+ if (cnt==0 && first_in) {
91
+ c[first_in] = s[first_in] = 1 ;
92
+ in.insert (first_in);
93
+ cnt = 1 ;
94
+ ops.erase (ops.begin ());
95
+ }
96
+
97
+ FOREACH (&o, ops) {
98
+ int op = o.F , x = o.S ;
99
+ if (op==1 ) {
100
+ ++cnt;
101
+ if (c[x]==1 ) {
102
+ out.erase (x);
103
+ in.insert (x);
104
+ } else {
105
+ // cnt>0, so those who curntly not in the room could not be the leaders
106
+ FOREACH (i, out) c[i] = 0 ;
107
+ out.clear ();
108
+ }
109
+ } else {
110
+ --cnt;
111
+ if (c[x]==1 ) {
112
+ // if after erasing x, cnt>0, x could not be a leader
113
+ if (cnt>0 ) c[x] = 0 ;
114
+ else {
115
+ in.erase (x);
116
+ out.insert (x);
117
+ }
118
+ }
119
+ }
120
+ }
121
+ // last state
122
+ if (cnt>0 )
123
+ FOREACH (i, out) c[i] = 0 ;
124
+
125
+
126
+ REP_IF (i, 1 , n, c[i]==1 ) ans.PB (i);
127
+ printf (" %d\n " , SZ (ans));
128
+ FOREACH (x, ans) printf (" %d " , x); printf (" \n " );
129
+
130
+
131
+ return 0 ;
132
+ }
0 commit comments