#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int MAXN = 200200;
const int MAXM = 200200;
int N, M, M0, A[MAXN], B[MAXN], P[MAXM];
vector<int> laser;
set<pair<int, int> > diffs;
set<pair<int, int> > gaps;
vector<pair<int, int> > qs;
ll sol[MAXN];
ll gap_sum;
void rm(int loc)
{
auto df = *(diffs.lower_bound(make_pair(loc, -1001001001)));
gap_sum -= abs(df.second);
gaps.erase(make_pair(abs(df.second), df.first));
diffs.erase(df);
}
void ins(int loc, int gap)
{
gap_sum += abs(gap);
diffs.insert(make_pair(loc, gap));
gaps.insert(make_pair(abs(gap), loc));
}
int mov(int &left, int &right, int target)
{
if (left <= target && target <= right) return 0;
if (right < target) {
int d = target - right;
left += d;
right += d;
return d;
}
int d = left - target;
left -= d;
right -= d;
return d;
}
int main()
{
scanf("%d%d", &N, &M0);
for (int i = 0; i < N; ++i) scanf("%d%d", &(A[i]), &(B[i]));
int M = 0;
for (int i = 0; i < M0; ++i) {
int Ptmp;
scanf("%d", &Ptmp);
if (M == 0 || P[M - 1] != Ptmp) P[M++] = Ptmp;
}
laser.push_back(P[0]);
for (int i = 1; i < M - 1; ++i) {
if (P[i] == P[i - 1]) continue;
if ((P[i - 1] >= P[i] && P[i] <= P[i + 1]) || (P[i - 1] <= P[i] && P[i] >= P[i + 1])) laser.push_back(P[i]);
}
laser.push_back(P[M - 1]);
int orig = laser[0];
gap_sum = 0;
for (int i = 1; i < laser.size(); ++i) ins(i, laser[i] - laser[i - 1]);
for (int i = 0; i < N; ++i) qs.push_back(make_pair(B[i] - A[i], i));
sort(qs.begin(), qs.end());
for (int i = 0; i < N; ++i) {
int qi = qs[i].second;
while (1 < gaps.size() && gaps.begin()->first <= qs[i].first) {
auto beg = *(gaps.begin());
if (diffs.begin()->first == beg.second) {
orig += diffs.begin()->second;
rm(beg.second);
} else if (diffs.rbegin()->first == beg.second) {
rm(beg.second);
} else {
auto df = diffs.lower_bound(make_pair(beg.second, -1001001001));
auto prev = df; --prev;
auto next = df; ++next;
int gap_new = prev->second + df->second + next->second;
rm(prev->first);
rm(df->first);
rm(next->first);
ins(beg.second, gap_new);
}
}
if (gaps.size() >= 2) {
ll tmp = gap_sum - gaps.size() * (ll)(B[qi] - A[qi]);
int gfirst = diffs.begin()->second;
if (gfirst > 0) {
tmp += abs(A[qi] - orig);
} else {
tmp += abs(B[qi] - orig);
}
sol[qi] = tmp;
} else {
ll tmp = 0;
int left = A[qi], right = B[qi];
tmp += mov(left, right, orig);
if (gaps.size() == 1) tmp += mov(left, right, orig + diffs.begin()->second);
sol[qi] = tmp;
}
}
for (int i = 0; i < N; ++i) printf("%lld\n", sol[i]);
return 0;
}
Compilation message
walls.cpp: In function 'int main()':
walls.cpp:73:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < laser.size(); ++i) ins(i, laser[i] - laser[i - 1]);
~~^~~~~~~~~~~~~~
walls.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M0);
~~~~~^~~~~~~~~~~~~~~~~
walls.cpp:54:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 0; i < N; ++i) scanf("%d%d", &(A[i]), &(B[i]));
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
walls.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Ptmp);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1016 KB |
Output is correct |
2 |
Correct |
376 ms |
14284 KB |
Output is correct |
3 |
Correct |
687 ms |
20776 KB |
Output is correct |
4 |
Correct |
697 ms |
20904 KB |
Output is correct |
5 |
Correct |
736 ms |
20904 KB |
Output is correct |
6 |
Correct |
361 ms |
20904 KB |
Output is correct |
7 |
Correct |
39 ms |
20904 KB |
Output is correct |
8 |
Correct |
41 ms |
20904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
20904 KB |
Output is correct |
2 |
Correct |
616 ms |
20904 KB |
Output is correct |
3 |
Correct |
168 ms |
20904 KB |
Output is correct |
4 |
Correct |
896 ms |
28804 KB |
Output is correct |
5 |
Correct |
631 ms |
28804 KB |
Output is correct |
6 |
Correct |
1167 ms |
28804 KB |
Output is correct |
7 |
Correct |
625 ms |
28804 KB |
Output is correct |
8 |
Correct |
1068 ms |
28804 KB |
Output is correct |
9 |
Correct |
125 ms |
28804 KB |
Output is correct |
10 |
Correct |
430 ms |
28804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1016 KB |
Output is correct |
2 |
Correct |
376 ms |
14284 KB |
Output is correct |
3 |
Correct |
687 ms |
20776 KB |
Output is correct |
4 |
Correct |
697 ms |
20904 KB |
Output is correct |
5 |
Correct |
736 ms |
20904 KB |
Output is correct |
6 |
Correct |
361 ms |
20904 KB |
Output is correct |
7 |
Correct |
39 ms |
20904 KB |
Output is correct |
8 |
Correct |
41 ms |
20904 KB |
Output is correct |
9 |
Correct |
42 ms |
20904 KB |
Output is correct |
10 |
Correct |
616 ms |
20904 KB |
Output is correct |
11 |
Correct |
168 ms |
20904 KB |
Output is correct |
12 |
Correct |
896 ms |
28804 KB |
Output is correct |
13 |
Correct |
631 ms |
28804 KB |
Output is correct |
14 |
Correct |
1167 ms |
28804 KB |
Output is correct |
15 |
Correct |
625 ms |
28804 KB |
Output is correct |
16 |
Correct |
1068 ms |
28804 KB |
Output is correct |
17 |
Correct |
125 ms |
28804 KB |
Output is correct |
18 |
Correct |
430 ms |
28804 KB |
Output is correct |
19 |
Correct |
685 ms |
28804 KB |
Output is correct |
20 |
Correct |
955 ms |
28828 KB |
Output is correct |
21 |
Correct |
599 ms |
28828 KB |
Output is correct |
22 |
Correct |
803 ms |
28828 KB |
Output is correct |
23 |
Correct |
708 ms |
28828 KB |
Output is correct |
24 |
Correct |
1133 ms |
28828 KB |
Output is correct |
25 |
Correct |
169 ms |
28828 KB |
Output is correct |
26 |
Correct |
263 ms |
28828 KB |
Output is correct |
27 |
Correct |
515 ms |
28828 KB |
Output is correct |