#include<bits/stdc++.h>
using namespace std;
const int N = 50005, inf = 1e9+7;
map<int, int> fr, bk, s[2];
int getfr (int I) {
if(fr.find(I) == fr.end()) return I-1;
return fr[I];
}
int getbk (int I) {
if(bk.find(I) == bk.end()) return I+1;
return bk[I];
}
char ch;
int i,poz,n,q,CT=1;
int main()
{
#ifdef HOME
ifstream cin("queue.in");
ofstream cout("queue.out");
#endif // HOME
cin>>n;
while(n--) {
int A, B;
cin>>A>>B;
int AF = getfr(A), AB = getbk(A), BF = getfr(B);
if(A == B || AB == B) continue;
fr[AB] = AF;
bk[AF] = AB;
fr[A] = BF;
bk[BF] = A;
fr[B] = A;
bk[A] = B;
if(CT == A) CT = AB;
if(CT == B) CT = A;
}
bk[inf] = inf;
for(int i=CT,j=1;i<inf;) {
auto it = *bk.lower_bound(i);
int E = it.first;
j += E-i;
s[0][E] = j;
s[1][j] = E;
i = it.second;
j++;
}
cin>>q;
for (i=1;i<=q;i++)
{
cin>>ch>>poz;
if (ch=='L')
{
auto it = *s[1].lower_bound(poz);
cout<<it.second-it.first+poz<<'\n';
}
else
{
auto it = *s[0].lower_bound(poz);
cout<<it.second-it.first+poz<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
512 KB |
Output is correct |
5 |
Correct |
181 ms |
1400 KB |
Output is correct |
6 |
Correct |
199 ms |
2424 KB |
Output is correct |
7 |
Correct |
226 ms |
3576 KB |
Output is correct |
8 |
Correct |
247 ms |
5564 KB |
Output is correct |
9 |
Correct |
258 ms |
6396 KB |
Output is correct |
10 |
Correct |
255 ms |
7132 KB |
Output is correct |
11 |
Correct |
319 ms |
14952 KB |
Output is correct |
12 |
Correct |
244 ms |
11768 KB |
Output is correct |
13 |
Correct |
333 ms |
15228 KB |
Output is correct |
14 |
Correct |
375 ms |
15224 KB |
Output is correct |
15 |
Correct |
351 ms |
15352 KB |
Output is correct |
16 |
Correct |
333 ms |
15084 KB |
Output is correct |
17 |
Correct |
113 ms |
504 KB |
Output is correct |
18 |
Correct |
163 ms |
1608 KB |
Output is correct |
19 |
Correct |
238 ms |
2552 KB |
Output is correct |
20 |
Correct |
344 ms |
3860 KB |
Output is correct |
21 |
Correct |
309 ms |
14456 KB |
Output is correct |
22 |
Correct |
426 ms |
17400 KB |
Output is correct |
23 |
Correct |
582 ms |
21148 KB |
Output is correct |
24 |
Correct |
606 ms |
21112 KB |
Output is correct |
25 |
Correct |
531 ms |
16888 KB |
Output is correct |