#include <bits/stdc++.h>
/// author: LilPluton auuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
using namespace std;
#define ll long long
#define ld long double
#define ar array
#define int ll
#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define endll '\n'
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
struct BIT
{
long long n;
vector<long long> ft;
BIT(long long N)
{
n = N;
ft.assign(n + 5, 0);
}
void upd(long long pos, long long val)
{
while(pos <= n)
{
ft[pos] += val;
pos += (pos & (-pos));
}
}
long long sum(long long l, long long r)
{
if(l != 1) return sum(1, r) - sum(1, l - 1);
long long res = 0;
while(r >= 1)
{
res += ft[r];
r += (r | (-r));
}
return res;
}
};
struct DSU
{
vector<int>par, size;
int n;
DSU(int N)
{
n = N + 5;
par.resize(n + 1, 0);
size.assign(n + 1, 1);
for(int i = 0; i <= n; ++i)
par[i] = i;
}
int _find(int v)
{
if(par[v] == v)
return v;
return par[v] = _find(par[v]);
}
bool unite(int a, int b)
{
a = _find(a);
b = _find(b);
if(a != b)
{
if(size[a] < size[b])
swap(a, b);
size[a] += size[b];
par[b] = a;
return 1;
}
return 0;
}
};
const long long INF = 1e18;
const int N = 2e5 + 5;
const int L = 20;
int n;
int calc(int a,int b)
{
return min({abs(a - b), 2 * n - abs(a - b)});
}
int dist(int a,int b, ar<int,2> c)
{
return 1 + min(calc(a, c[0]) + calc(b, c[1]), calc(a, c[1]) + calc(b, c[0]));
}
void solve(int test_case) {
int m, q;
cin >> n >> m >> q;
vector<int>v;
FOR(m)
{
int x;
cin >> x;
v.push_back(x);
v.push_back(n + x);
}
sort(all(v));
FOR(q)
{
int l, r;
cin >> l >> r;
int ans = calc(l, r);
vector<ar<int,2>> s;
auto it = lower_bound(all(v), l);
if(it != v.end())
{
s.push_back({*it, (*it + n) % (n * 2)});
}
if(it != v.begin())
{
--it;
s.push_back({*it, (*it + n) % (n * 2)});
}
it = lower_bound(all(v), r);
if(it != v.end())
{
s.push_back({*it, (*it + n) % (n * 2)});
}
if(it != v.begin())
{
--it;
s.push_back({*it, (*it + n) % (n * 2)});
}
for(auto f : s) ans = min({ans, dist(l, r, f)});
cout << ans << endll;
}
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
for(int i = 1; i <= t; ++i)
solve(i);
return 0;
}
Compilation message
Main.cpp:152:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
152 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
740 KB |
Output is correct |
6 |
Correct |
4 ms |
836 KB |
Output is correct |
7 |
Correct |
5 ms |
732 KB |
Output is correct |
8 |
Correct |
5 ms |
856 KB |
Output is correct |
9 |
Correct |
5 ms |
820 KB |
Output is correct |
10 |
Correct |
5 ms |
1016 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
352 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
472 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
59 ms |
12896 KB |
Output is correct |
4 |
Correct |
16 ms |
3288 KB |
Output is correct |
5 |
Correct |
4 ms |
676 KB |
Output is correct |
6 |
Correct |
4 ms |
860 KB |
Output is correct |
7 |
Correct |
4 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
728 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
604 KB |
Output is correct |
14 |
Correct |
9 ms |
860 KB |
Output is correct |
15 |
Correct |
33 ms |
7568 KB |
Output is correct |
16 |
Correct |
32 ms |
7380 KB |
Output is correct |
17 |
Correct |
65 ms |
12728 KB |
Output is correct |
18 |
Correct |
66 ms |
12740 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
740 KB |
Output is correct |
6 |
Correct |
4 ms |
836 KB |
Output is correct |
7 |
Correct |
5 ms |
732 KB |
Output is correct |
8 |
Correct |
5 ms |
856 KB |
Output is correct |
9 |
Correct |
5 ms |
820 KB |
Output is correct |
10 |
Correct |
5 ms |
1016 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
464 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
460 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
0 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
352 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
472 KB |
Output is correct |
53 |
Correct |
1 ms |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
5 ms |
860 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
59 ms |
12896 KB |
Output is correct |
68 |
Correct |
16 ms |
3288 KB |
Output is correct |
69 |
Correct |
4 ms |
676 KB |
Output is correct |
70 |
Correct |
4 ms |
860 KB |
Output is correct |
71 |
Correct |
4 ms |
604 KB |
Output is correct |
72 |
Correct |
4 ms |
604 KB |
Output is correct |
73 |
Correct |
4 ms |
728 KB |
Output is correct |
74 |
Correct |
4 ms |
604 KB |
Output is correct |
75 |
Correct |
6 ms |
620 KB |
Output is correct |
76 |
Correct |
5 ms |
860 KB |
Output is correct |
77 |
Correct |
6 ms |
604 KB |
Output is correct |
78 |
Correct |
9 ms |
860 KB |
Output is correct |
79 |
Correct |
33 ms |
7568 KB |
Output is correct |
80 |
Correct |
32 ms |
7380 KB |
Output is correct |
81 |
Correct |
65 ms |
12728 KB |
Output is correct |
82 |
Correct |
66 ms |
12740 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
4 ms |
344 KB |
Output is correct |
85 |
Correct |
4 ms |
344 KB |
Output is correct |
86 |
Correct |
4 ms |
604 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
4 ms |
604 KB |
Output is correct |
89 |
Correct |
5 ms |
600 KB |
Output is correct |
90 |
Correct |
5 ms |
860 KB |
Output is correct |
91 |
Correct |
5 ms |
928 KB |
Output is correct |
92 |
Correct |
5 ms |
856 KB |
Output is correct |
93 |
Correct |
5 ms |
856 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
0 ms |
348 KB |
Output is correct |
98 |
Correct |
0 ms |
348 KB |
Output is correct |
99 |
Correct |
0 ms |
348 KB |
Output is correct |
100 |
Correct |
0 ms |
348 KB |
Output is correct |
101 |
Correct |
0 ms |
348 KB |
Output is correct |
102 |
Correct |
1 ms |
352 KB |
Output is correct |
103 |
Correct |
1 ms |
348 KB |
Output is correct |
104 |
Correct |
0 ms |
348 KB |
Output is correct |
105 |
Correct |
0 ms |
348 KB |
Output is correct |
106 |
Correct |
0 ms |
348 KB |
Output is correct |
107 |
Correct |
0 ms |
348 KB |
Output is correct |
108 |
Correct |
0 ms |
348 KB |
Output is correct |
109 |
Correct |
0 ms |
348 KB |
Output is correct |
110 |
Correct |
1 ms |
348 KB |
Output is correct |
111 |
Correct |
0 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
348 KB |
Output is correct |
113 |
Correct |
0 ms |
348 KB |
Output is correct |
114 |
Correct |
0 ms |
348 KB |
Output is correct |
115 |
Correct |
0 ms |
348 KB |
Output is correct |
116 |
Correct |
0 ms |
348 KB |
Output is correct |
117 |
Correct |
1 ms |
348 KB |
Output is correct |
118 |
Correct |
1 ms |
348 KB |
Output is correct |
119 |
Correct |
0 ms |
348 KB |
Output is correct |
120 |
Correct |
0 ms |
348 KB |
Output is correct |
121 |
Correct |
59 ms |
11392 KB |
Output is correct |
122 |
Correct |
21 ms |
3028 KB |
Output is correct |
123 |
Correct |
4 ms |
604 KB |
Output is correct |
124 |
Correct |
4 ms |
604 KB |
Output is correct |
125 |
Correct |
4 ms |
816 KB |
Output is correct |
126 |
Correct |
4 ms |
604 KB |
Output is correct |
127 |
Correct |
4 ms |
608 KB |
Output is correct |
128 |
Correct |
5 ms |
748 KB |
Output is correct |
129 |
Correct |
4 ms |
856 KB |
Output is correct |
130 |
Correct |
6 ms |
860 KB |
Output is correct |
131 |
Correct |
5 ms |
852 KB |
Output is correct |
132 |
Correct |
6 ms |
920 KB |
Output is correct |
133 |
Correct |
35 ms |
6612 KB |
Output is correct |
134 |
Correct |
33 ms |
7124 KB |
Output is correct |
135 |
Correct |
65 ms |
12980 KB |
Output is correct |
136 |
Correct |
68 ms |
13240 KB |
Output is correct |
137 |
Correct |
67 ms |
13468 KB |
Output is correct |
138 |
Correct |
68 ms |
12720 KB |
Output is correct |
139 |
Correct |
8 ms |
3032 KB |
Output is correct |
140 |
Correct |
3 ms |
1248 KB |
Output is correct |
141 |
Correct |
31 ms |
6100 KB |
Output is correct |
142 |
Correct |
39 ms |
6608 KB |
Output is correct |
143 |
Correct |
58 ms |
11204 KB |
Output is correct |
144 |
Correct |
5 ms |
856 KB |
Output is correct |
145 |
Correct |
5 ms |
860 KB |
Output is correct |
146 |
Correct |
5 ms |
884 KB |
Output is correct |
147 |
Correct |
7 ms |
856 KB |
Output is correct |
148 |
Correct |
7 ms |
860 KB |
Output is correct |
149 |
Correct |
6 ms |
860 KB |
Output is correct |
150 |
Correct |
8 ms |
1116 KB |
Output is correct |
151 |
Correct |
0 ms |
348 KB |
Output is correct |
152 |
Correct |
0 ms |
348 KB |
Output is correct |
153 |
Correct |
0 ms |
344 KB |
Output is correct |
154 |
Correct |
0 ms |
348 KB |
Output is correct |
155 |
Correct |
0 ms |
348 KB |
Output is correct |