-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(unfinished)916D.Jamie and To-do List.cpp
162 lines (141 loc) · 3.85 KB
/
(unfinished)916D.Jamie and To-do List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <unordered_map>
#define cind(x) scanf("%d", &x)
#define cindln(x) scanf("%d\n", &x)
#define cinc(x) scanf("%c", &x)
using namespace std;
const int MAXQ = 1e5 + 7;
// without undo operations
class Query {
public:
char t;
string a;
int x;
Query(){}
Query(char c, int p):
t(c), x(p) {}
Query(char c, string name):
t(c), a(name) {}
Query(char c, string name, int p):
t(c), a(name), x(p) {}
};
class Node {
public:
int cnt, l, r;
int s[2];
Node() {
cnt = s[0] = s[1] = 0;
}
};
int Q;
vector<Query> query(1);
vector<int> xs(1);
Node t[MAXQ<<2];
unordered_map<string, int> task;
int link[MAXQ];
inline int get_mid(int l,int r) { return (l+r)>>1; }
void refresh(int node) {
t[node].cnt = t[t[node].s[0]].cnt + t[t[node].s[1]].cnt;
}
void build(int node, int l, int r) {
t[node].l = l, t[node].r = r;
if (l!=r) {
t[node].s[0] = node << 1;
t[node].s[1] = node << 1 | 1;
build(t[node].s[0], l, (l+r)>>1);
build(t[node].s[1], ((l+r)>>1) + 1, r);
}
}
void read_in() {
char c;
for (int x,q=1; q<=Q; ++q) {
cinc(c);
for (char t='1'; t!=' '; cinc(t));
if (c=='u') { //undo
cind(x);
query.emplace_back('u', x);
} else {
string name;
char t;
for (cinc(t); isalpha(t); cinc(t))
name.push_back(t);
if (c=='q' || c=='r')
query.emplace_back(c, name);
else { // set
cindln(x);
query.emplace_back(c, name, x);
xs.push_back(x);
}
}
}
sort(xs.begin()+1, xs.end());
xs.resize(unique(xs.begin()+1, xs.end()) - xs.begin()); // distinct
if (!xs.empty()) build(1, 1, xs.size());
}
void update(int node, int pos, int delta) {
Node& n = t[node];
if (n.l==n.r)
n.cnt += delta;
else {
update(t[node].s[pos>get_mid(n.l, n.r)], pos, delta);
refresh(node);
}
}
int dfs(int node, int p) {
Node& n = t[node];
// printf("%d %d %d %d %d\n", node, n.l, n.r, n.cnt, p);
if (n.l>=p) return 0;
if (n.r<p) return n.cnt;
return dfs(n.s[0], p) + dfs(n.s[1], p);
}
int main() {
cindln(Q);
read_in();
for (int q=1; q<=Q; ++q) {
Query& qr = query[q];
// printf("%c\n", qr.t);
// cout << qr.a << endl;
if (qr.t=='s') {
int pos = lower_bound(xs.begin()+1, xs.end(), qr.x) - xs.begin();
auto it = task.find(qr.a);
if (it==task.end()) {
task[qr.a] = qr.x;
update(1, pos, 1);
} else {
int prepos = lower_bound(xs.begin()+1, xs.end(), it->second)
- xs.begin();
it->second = qr.x;
update(1, prepos, -1);
update(1, pos, 1);
}
} else if (qr.t=='r') {
auto it = task.find(qr.a);
if (it!=task.end()) {
int pos = lower_bound(xs.begin()+1, xs.end(), it->second)
- xs.begin();
task.erase(it);
update(1, pos, -1);
}
} else if (qr.t=='q') {
auto it = task.find(qr.a);
if (it==task.end()) {
printf("%d\n", -1);
} else {
int pos = lower_bound(xs.begin()+1, xs.end(), it->second)
- xs.begin();
printf("%d\n", dfs(1, pos));
}
fflush(stdout);
} else {
// int now = q-1, pre = now - qr.x - 1;
// while (now!=pre)
// if (query[now].t=='s')
}
}
return 0;
}