#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
map<int, int> bf, af, s[2];
map<int, pii> pos;
const int N = 5e4+1;
const int INF = 1e9+7;
int prv(int x) {
if (bf.find(x) != bf.end()) return bf[x];
return x+1;
}
int nxt(int x) {
if (af.find(x) != af.end()) return af[x];
return x-1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, x=1; cin>>n;
for (int i=1; i<=n; ++i) {
int a, b; cin>>a>>b;
int afr=nxt(a), abk=prv(a), bfr=nxt(b);
if (a == b || abk == b) continue;
af[abk]=afr, bf[afr]=abk;
af[a]=bfr, bf[bfr]=a;
af[b]=a, bf[a]=b;
if (x == a) x=abk;
if (x == b) x=a;
} bf[INF]=INF;
for (int i=x, j=1; i<INF;) {
auto it=bf.lower_bound(i);
int e=(*it).first; j+=e-i;
s[0][e]=j, s[1][j]=e;
i=(*it).second; ++j;
}
int q; cin>>q;
while (q--) {
char c; cin>>c;
int a; cin>>a;
auto p=s[c == 'L'].lower_bound(a);
cout<<(p->second-p->first+a)<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
19 ms |
2004 KB |
Output is correct |
6 |
Correct |
24 ms |
3044 KB |
Output is correct |
7 |
Correct |
41 ms |
4120 KB |
Output is correct |
8 |
Correct |
50 ms |
6264 KB |
Output is correct |
9 |
Correct |
54 ms |
6700 KB |
Output is correct |
10 |
Correct |
58 ms |
7408 KB |
Output is correct |
11 |
Correct |
167 ms |
15352 KB |
Output is correct |
12 |
Correct |
146 ms |
12308 KB |
Output is correct |
13 |
Correct |
173 ms |
15436 KB |
Output is correct |
14 |
Correct |
171 ms |
15524 KB |
Output is correct |
15 |
Correct |
177 ms |
15504 KB |
Output is correct |
16 |
Correct |
177 ms |
15576 KB |
Output is correct |
17 |
Correct |
18 ms |
1108 KB |
Output is correct |
18 |
Correct |
38 ms |
2164 KB |
Output is correct |
19 |
Correct |
55 ms |
2764 KB |
Output is correct |
20 |
Correct |
96 ms |
4144 KB |
Output is correct |
21 |
Correct |
138 ms |
14848 KB |
Output is correct |
22 |
Correct |
181 ms |
17736 KB |
Output is correct |
23 |
Correct |
245 ms |
21556 KB |
Output is correct |
24 |
Correct |
231 ms |
21540 KB |
Output is correct |
25 |
Correct |
196 ms |
17424 KB |
Output is correct |