#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
priority_queue <pll> Q;
set <pll> S;
ll A[202020], B[202020], K[202020], T[202020];
ll P[202020], N[202020];
ll n, m, k, s, cnt;
int main()
{
ll i, t, a, f, p;
pll q;
scanf("%lld%lld", &n, &m);
for(i=1;i<=n;i++){
scanf("%lld%lld", A+i, B+i);
K[i] = i;
}
for(i=1;i<=m;i++){
scanf("%lld", &a);
if(k == 0) P[++k] = a;
else if(k == 1){
if(P[k] != a) P[++k] = a;
}
else{
if(P[k-1] < P[k] && P[k] <= a) P[k] = a;
else if(P[k-1] > P[k] && P[k] >= a) P[k] = a;
else P[++k] = a;
}
}
if(k == 1){
for(i=1;i<=n;i++){
if(A[i] <= a && a <= B[i]) printf("0\n");
else if(a < A[i]) printf("%lld\n", A[i] - a);
else printf("%lld\n", a - B[i]);
}
return 0;
}
for(i=1;i<k;i++){
S.insert(pll(i, i+1));
N[i] = i+1;
Q.push(pll(-abs(P[i] - P[i+1]), i));
s += abs(P[i] - P[i+1]);
cnt ++;
}
sort(K+1, K+n+1, [&](int ca, int cb){
return B[ca] - A[ca] < B[cb] - A[cb];
});
for(i=1;i<=n;i++){
t = K[i];
for(;S.size() > 1 && Q.size();){
if(-Q.top().first <= B[t] - A[t]){
f = -Q.top().first;
p = Q.top().second;
Q.pop();
if(N[p] == -1 || abs(P[p] - P[N[p]]) != f) continue;
auto it1 = S.lower_bound(pll(p, N[p]));
auto it2 = it1; it2 ++;
if(it2 == S.end()){
s -= f; cnt --;
N[p] = -1;
S.erase(it1);
}
else if(it1 == S.begin()){
s -= f; cnt --;
N[p] = -1;
S.erase(it1);
}
else{
s -= f*2; cnt -= 2;
auto it3 = it1; it3 --;
ll x1 = it3->first, x2 = it1->first, x3 = it2->first, x4 = it2->second;
N[x1] = x4;
N[x2] = -1;
N[x3] = -1;
Q.push(pll(-abs(P[x1] - P[x4]), x1));
it1 = S.lower_bound(pll(x1, x2)); S.erase(it1);
it1 = S.lower_bound(pll(x2, x3)); S.erase(it1);
it1 = S.lower_bound(pll(x3, x4)); S.erase(it1);
S.insert(pll(x1, x4));
}
}
else break;
}
q = *S.begin();
if(S.size() == 1){
ll a = P[q.first], b = P[q.second];
if(abs(a - b) <= B[t] - A[t]){
if(a > b) swap(a, b);
if(A[t] <= a && b <= B[t]) T[t] = 0;
else if(a <= A[t]) T[t] = A[t] - a;
else T[t] = b - B[t];
continue;
}
}
if(P[q.first] < P[q.second]) T[t] = abs(A[t] - P[q.first]);
else T[t] = abs(B[t] - P[q.first]);
T[t] += s - cnt * (B[t] - A[t]);
}
for(i=1;i<=n;i++){
printf("%lld\n", T[i]);
}
return 0;
}
Compilation message
walls.cpp: In function 'int main()':
walls.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~
walls.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", A+i, B+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
walls.cpp:27:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a);
~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1016 KB |
Output is correct |
2 |
Correct |
241 ms |
14780 KB |
Output is correct |
3 |
Correct |
408 ms |
19204 KB |
Output is correct |
4 |
Correct |
278 ms |
19272 KB |
Output is correct |
5 |
Correct |
368 ms |
19392 KB |
Output is correct |
6 |
Correct |
182 ms |
19568 KB |
Output is correct |
7 |
Correct |
40 ms |
19568 KB |
Output is correct |
8 |
Correct |
51 ms |
19568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
19568 KB |
Output is correct |
2 |
Correct |
365 ms |
19568 KB |
Output is correct |
3 |
Correct |
151 ms |
19568 KB |
Output is correct |
4 |
Correct |
738 ms |
28536 KB |
Output is correct |
5 |
Correct |
571 ms |
28536 KB |
Output is correct |
6 |
Correct |
636 ms |
28688 KB |
Output is correct |
7 |
Correct |
519 ms |
28688 KB |
Output is correct |
8 |
Correct |
774 ms |
28688 KB |
Output is correct |
9 |
Correct |
106 ms |
28688 KB |
Output is correct |
10 |
Correct |
378 ms |
28688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1016 KB |
Output is correct |
2 |
Correct |
241 ms |
14780 KB |
Output is correct |
3 |
Correct |
408 ms |
19204 KB |
Output is correct |
4 |
Correct |
278 ms |
19272 KB |
Output is correct |
5 |
Correct |
368 ms |
19392 KB |
Output is correct |
6 |
Correct |
182 ms |
19568 KB |
Output is correct |
7 |
Correct |
40 ms |
19568 KB |
Output is correct |
8 |
Correct |
51 ms |
19568 KB |
Output is correct |
9 |
Correct |
53 ms |
19568 KB |
Output is correct |
10 |
Correct |
365 ms |
19568 KB |
Output is correct |
11 |
Correct |
151 ms |
19568 KB |
Output is correct |
12 |
Correct |
738 ms |
28536 KB |
Output is correct |
13 |
Correct |
571 ms |
28536 KB |
Output is correct |
14 |
Correct |
636 ms |
28688 KB |
Output is correct |
15 |
Correct |
519 ms |
28688 KB |
Output is correct |
16 |
Correct |
774 ms |
28688 KB |
Output is correct |
17 |
Correct |
106 ms |
28688 KB |
Output is correct |
18 |
Correct |
378 ms |
28688 KB |
Output is correct |
19 |
Correct |
542 ms |
28688 KB |
Output is correct |
20 |
Correct |
683 ms |
33796 KB |
Output is correct |
21 |
Correct |
432 ms |
33796 KB |
Output is correct |
22 |
Correct |
549 ms |
35784 KB |
Output is correct |
23 |
Correct |
609 ms |
35784 KB |
Output is correct |
24 |
Correct |
765 ms |
38268 KB |
Output is correct |
25 |
Correct |
194 ms |
38268 KB |
Output is correct |
26 |
Correct |
239 ms |
38268 KB |
Output is correct |
27 |
Correct |
492 ms |
38268 KB |
Output is correct |