This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
using ll = long long;
ll ans[200009];
int N, M, A[200009], B[200009], L[200009], R[200009], V[200009];
bool C[200009];
struct fenwick {
ll F[200009];
ll get(int x) {
++x;
ll s = 0;
for(int i=x; i>=1; i-=(i&-i)) s += F[i];
return s;
}
void upd(int x, int y) {
++x;
for(int i=x; i<=M; i+=(i&-i)) F[i] += y;
}
} F1, F2;
int dst(int l, int r, int x) {
if(x < l) return l-x;
if(l <= x && x <= r) return 0;
if(r < x) return x-r;
}
int main() {
vector<int> W;
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) {
scanf("%d%d", &A[i], &B[i]);
W.push_back(i);
}
sort(W.begin(), W.end(), [&](int p, int q) { return B[p] - A[p] < B[q] - A[q]; });
vector<int> P;
while(M--) {
int x; scanf("%d", &x);
int K = P.size();
if(K <= 1) P.push_back(x);
else {
if(P[K-2] < P[K-1] && P[K-1] <= x) P.pop_back();
else if(P[K-2] > P[K-1] && P[K-1] >= x) P.pop_back();
P.push_back(x);
}
}
M = P.size();
set<int> S;
priority_queue<tiii> pq;
L[0] = R[0] = P[0]; C[0] = 1;
for(int i=0; i<M; i++) {
S.insert(i);
if(i) {
V[i] = max(P[i] - P[i-1], P[i-1] - P[i]);
F1.upd(i, V[i]);
F2.upd(i, +1);
L[i] = min(L[i-1], P[i]);
R[i] = max(R[i-1], P[i]);
pq.push({-V[i], i-1, i});
}
}
for(auto& it: W) {
while(pq.size() && S.size() > 3) {
int g, a, b; tie(g, a, b) = pq.top(); g = -g;
if(C[a] || C[b]) {
pq.pop();
continue;
}
if(g > B[it] - A[it]) break;
pq.pop();
C[b] = 1; F1.upd(b, -V[b]); F2.upd(b, -1); S.erase(b);
auto nt = S.upper_bound(b);
if(nt != S.end()) {
C[a] = 1; F1.upd(a, -V[a]); F2.upd(a, -1); S.erase(a);
nt = S.upper_bound(b);
auto pt = prev(nt);
F1.upd(*nt, V[a] - V[b]); V[*nt] += V[a] - V[b];
pq.push({-V[*nt], *pt, *nt});
}
}
int l = 0, r = M-1, f = -1;
while(l <= r) {
int m = l+r >> 1;
if(L[m] < A[it] || B[it] < R[m]) r = m-1, f = m;
else l = m+1;
}
if(f == -1) continue;
int x = *S.begin();
while(1) {
ans[it] += dst(A[it], B[it], P[x]);
int d = 0;
if(P[x] < A[it]) d = P[x] - A[it];
if(B[it] < P[x]) d = P[x] - B[it];
A[it] += d; B[it] += d;
auto nt = S.upper_bound(x);
if(nt == S.end()) break;
x = *nt;
}
/*
if(nt != S.end()) {
int d = 0;
if(P[f] < A[it]) d = P[f] - A[it];
if(B[it] < P[f]) d = P[f] - B[it];
ans[it] += dst(A[it] + d, B[it] + d, P[*nt]);
ans[it] += F1.get(M-1) - F1.get(*nt) - (F2.get(M-1) - F2.get(*nt)) * (B[it] - A[it]);
}
*/
}
for(int i=1; i<=N; i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message (stderr)
walls.cpp: In function 'int main()':
walls.cpp:87:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int m = l+r >> 1;
| ~^~
walls.cpp: In function 'int dst(int, int, int)':
walls.cpp:29:1: warning: control reaches end of non-void function [-Wreturn-type]
29 | }
| ^
walls.cpp: In function 'int main()':
walls.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
walls.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d%d", &A[i], &B[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
walls.cpp:41:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | int x; scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |