QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109366 | #70. Bitaro, who Leaps through Time | bashkort# | 0 | 0ms | 3532kb | C++20 | 5.7kb | 2023-05-28 18:39:38 | 2024-05-31 13:46:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int inf = 1e9 + 7;
void massert(bool f) {
while (!f) {
cout << "mda" << endl;
}
}
struct SegmentTree {
vector<pair<int, int>> t;
vector<ll> ansL;
int sz = 1, N;
int cost(int x, int d, ll &ans) {
massert(x < sz * 2);
if (t[x].first == -inf) {
return d;
}
if (t[x].first <= t[x].second) {
ans += max(0, d - t[x].second);
return min(t[x].second, max(d, t[x].first));
} else if (t[x << 1].second == -inf) {
ans += ansL[x];
cost(x << 1, d, ans);
return t[x].first;
} else {
ans += max(0, d - t[x << 1].second);
return cost(x << 1 | 1, min(t[x << 1].second, max(d, t[x << 1].first)), ans);
}
}
void pull(int x) {
massert(x < sz * 2);
ansL[x] = 0;
if (t[x << 1 | 1].first == -inf) {
t[x] = t[x << 1];
return;
}
if (t[x << 1].second == -inf) {
t[x].first = cost(x << 1 | 1, t[x << 1].first, ansL[x]);
t[x].second = -inf;
} else if (t[x << 1 | 1].second == -inf) {
t[x] = t[x << 1 | 1];
} else {
assert(t[x << 1].first <= t[x << 1].second);
assert(t[x << 1 | 1].first <= t[x << 1 | 1].second);
if (t[x << 1].first > t[x << 1 | 1].second) {
t[x].first = t[x << 1 | 1].second;
t[x].second = -inf;
} else if (t[x << 1].second < t[x << 1 | 1].first) {
t[x].first = t[x << 1].first;
t[x].second = -inf;
} else {
t[x].first = max(t[x << 1].first, t[x << 1 | 1].first);
t[x].second = min(t[x << 1].second, t[x << 1 | 1].second);
massert(t[x].first <= t[x].second);
}
}
}
void init(int n, vector<int> L, vector<int> R) {
sz = 1 << __lg(n) + !!(n & (n - 1));
N = n;
t.assign(sz << 1, {-inf, -inf}), ansL.resize(sz << 1);
for (int i = 0; i < n; ++i) {
t[i + sz].first = L[i], t[i + sz].second = R[i];
}
for (int i = sz - 1; i > 0; --i) {
pull(i);
}
}
void rangeQuery(int l, int r, int &d, ll &ans, int x, int lx, int rx) {
if (l >= rx || lx >= r) {
return;
}
if (l <= lx && rx <= r) {
d = cost(x, d, ans);
return;
}
int mid = lx + rx >> 1;
rangeQuery(l, r, d, ans, x << 1, lx, mid);
rangeQuery(l, r, d, ans, x << 1 | 1, mid, rx);
}
ll rangeQuery(int l, int r, int &d) {
assert(l >= 0 && l < N && l < r && r <= N);
ll ans = 0;
rangeQuery(l, r, d, ans, 1, 0, sz);
return ans;
}
void modify(int x, int L, int R) {
x += sz;
t[x].first = L, t[x].second = R;
while (x >>= 1) {
pull(x);
}
}
};
//don't forget to add diff between d and needed time
bool stress = true;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> L(n), R(n), AL(n), AR(n), BL(n), BR(n);
for (int i = 0; i < n - 1; ++i) {
cin >> L[i] >> R[i];
AL[i] = L[i] - i;
AR[i] = R[i] - i - 1;
BL[n - i - 2] = L[i] - (n - i - 2);
BR[n - i - 2] = R[i] - (n - i - 2) - 1;
}
SegmentTree A, B;
if (n > 1) {
A.init(n - 1, AL, AR);
B.init(n - 1, BL, BR);
}
auto modify = [&](int i, int lx, int rx) {
L[i] = lx, R[i] = rx;
AL[i] = L[i] - i;
AR[i] = R[i] - i - 1;
BL[n - i - 2] = L[i] - (n - i - 2);
BR[n - i - 2] = R[i] - (n - i - 2) - 1;
A.modify(i, AL[i], AR[i]);
B.modify(n - i - 2, BL[n - i - 2], BR[n - i - 2]);
};
auto stupid = [&n](int x, int y, int a, int b, vector<int> &L, vector<int> &R) {
ll ans = 0;
for (int i = x; i < y; ++i) {
ans += max(0, a - R[i]);
a = min(R[i], max(a, L[i]));
}
ans += max(0, a - b);
return ans;
};
auto query = [&](int x, int a, int y, int b) -> ll {
if (x == y) {
return max(0, a - b);
}
ll ans = 0;
ll stup = 0;
bool mda = false;
if (x <= y) {
a -= x;
b -= y;
stress = false;
mda = true;
if (stress) {
stup = stupid(x, y, a, b, AL, AR);
}
ans = A.rangeQuery(x, y, a);
} else {
a -= n - x - 1;
b -= n - y - 1;
if (stress) {
stup = stupid(n - x - 1, n - y - 1, a, b, BL, BR);
}
ans = B.rangeQuery(n - x - 1, n - y - 1, a);
}
ans += max(0, a - b);
if (stress) {
if (stup != ans) {
cout << "should have: " << stup << endl;
cout << "instead you got: " << ans << endl;
return 0;
}
}
if (mda) {
stress = true;
}
return ans;
};
while (q--) {
int t;
cin >> t;
if (t == 1) {
int p, s, e;
cin >> p >> s >> e;
modify(p - 1, s, e);
} else {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << query(a - 1, b, c - 1, d) << '\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3532kb
input:
20 18 115497790 671208773 326245299 528973482 100582193 437996818 89058008 771100620 768935396 842907844 187943946 997369106 455078418 542835554 536691525 970171971 564540350 570234421 657178750 753833933 386375484 979995375 389681484 772601117 634873482 897954663 87193815 139420775 259946990 394597...
output:
1133140545 286505553 517757980 236944355 561949186 106582836 0 304461403 191096499
result:
wrong answer 1st lines differ - expected: '1155445816', found: '1133140545'
Subtask #2:
score: 0
Time Limit Exceeded
Test #42:
score: 0
Time Limit Exceeded
input:
274318 300000 489215489 676617321 780126019 788585486 556851007 580284394 233372413 595198772 519202713 898223077 502895565 696411826 206200999 769856900 270143414 346344669 729812429 901771242 663771137 938786194 472985796 990513077 846601694 992055636 178982840 919444964 27052680 316046043 8183731...
output:
2332017664654 3146923337014 should have: 4095601609850 instead you got: 3348886982922 0 should have: 9126786831492 instead you got: 7434538419582 0 should have: 441666160010 instead you got: 352734662211 0 3640537765532 should have: 6742720280622 instead you got: 5483561680832 0 should have: 1076662...
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #56:
score: 0
Time Limit Exceeded
input:
270695 300000 513123795 772355425 210106247 394028231 276162603 911454418 105669187 977348162 173662950 272706156 152814457 669922258 344843731 523572913 316675910 752220119 109044474 322732409 555169512 652867118 622530606 779759913 153668285 339269709 150911093 937002300 186921016 855255616 118867...
output:
should have: 6546523326977 instead you got: 5354767852101 0 should have: 1147915030312 instead you got: 934309781772 0 8032746571463 should have: 1119704562225 instead you got: 926086642162 0 2959978892329 2745984542702 should have: 8703715133673 instead you got: 7131233264443 0 should have: 1212731...