# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
677434 |
2023-01-03T10:49:32 Z |
onjo0127 |
None (JOI15_walls) |
C++11 |
|
203 ms |
18532 KB |
#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()) {
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({min(P[*nt] - P[*pt], P[*pt] - P[*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;
auto nt = S.upper_bound(f);
ans[it] = dst(A[it], B[it], P[f]);
if(nt != S.end()) {
int d;
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
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);
| ~~~~~^~~~~~~~~~
walls.cpp:98:18: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | ans[it] += dst(A[it] + d, B[it] + d, P[*nt]);
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
980 KB |
Output is correct |
2 |
Correct |
132 ms |
13620 KB |
Output is correct |
3 |
Correct |
203 ms |
18532 KB |
Output is correct |
4 |
Correct |
153 ms |
18420 KB |
Output is correct |
5 |
Correct |
186 ms |
18468 KB |
Output is correct |
6 |
Incorrect |
111 ms |
18412 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
2132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
980 KB |
Output is correct |
2 |
Correct |
132 ms |
13620 KB |
Output is correct |
3 |
Correct |
203 ms |
18532 KB |
Output is correct |
4 |
Correct |
153 ms |
18420 KB |
Output is correct |
5 |
Correct |
186 ms |
18468 KB |
Output is correct |
6 |
Incorrect |
111 ms |
18412 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |