QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#308004 | #8016. 不休陀螺 | jrjyy | 0 | 0ms | 0kb | C++20 | 3.0kb | 2024-01-19 13:05:15 | 2024-01-19 13:05:16 |
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int S = 2.1e7;
struct Node {
int l, r, v;
} seg[S];
int tot;
void modify(int &u, int l, int r, int p) {
seg[++tot] = seg[u];
u = tot;
seg[u].v += 1;
if (r - l == 1) {
return;
}
int m = (l + r) / 2;
if (p < m) {
modify(seg[u].l, l, m, p);
} else {
modify(seg[u].r, m, r, p);
}
}
int query(int u, int l, int r, int ql, int qr) {
if (!u || r <= ql || qr <= l) {
return 0;
}
if (ql <= l && r <= qr) {
return seg[u].v;
}
int m = (l + r) / 2;
return query(seg[u].l, l, m, ql, qr) + query(seg[u].r, m, r, ql, qr);
}
int main() {
freopen("top.in", "r", stdin);
freopen("top.out", "w", stdout);
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, E;
std::cin >> n >> E;
std::vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
for (int i = 0; i < n; ++i) {
std::cin >> b[i];
}
std::vector<int> c(n);
std::vector<i64> f(n + 1), sum(n + 1);
for (int i = 0; i < n; ++i) {
c[i] = std::min(a[i], b[i]);
f[i + 1] = f[i] + -std::min(b[i] - a[i], 0);
sum[i + 1] = sum[i] + b[i] - a[i];
}
auto v = sum;
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
std::vector<int> s(n + 1);
std::vector<int> root(n + 2);
for (int i = 0; i <= n; ++i) {
s[i] = std::lower_bound(v.begin(), v.end(), sum[i]) - v.begin();
root[i + 1] = root[i];
modify(root[i + 1], 0, int(v.size()), s[i]);
}
std::vector<int> lmin(n), rmin(n, n), stk{-1};
for (int i = 0; i < n; ++i) {
while (stk.back() != -1 && c[stk.back()] <= c[i]) {
rmin[stk.back()] = i;
stk.pop_back();
}
lmin[i] = stk.back();
stk.push_back(i);
}
i64 ans = 0;
for (int x = 0; x < n; ++x) {
if (x - lmin[x] < rmin[x] - x) {
for (int i = lmin[x] + 1; i <= x; ++i) {
int j = std::upper_bound(f.begin(), f.end(), f[i] + E - c[x]) - f.begin();
j = std::min(j, rmin[x] + 1);
if (x + 1 < j) {
ans += query(root[j], 0, int(v.size()), s[i], int(v.size()));
ans -= query(root[x + 1], 0, int(v.size()), s[i], int(v.size()));
}
}
} else {
for (int i = x; i < rmin[x]; ++i) {
int j = std::lower_bound(f.begin(), f.end(), f[i + 1] + c[x] - E) - f.begin();
j = std::max(j, lmin[x] + 1);
if (j <= x) {
ans += query(root[x + 1], 0, int(v.size()), 0, s[i + 1] + 1);
ans -= query(root[j], 0, int(v.size()), 0, s[i + 1] + 1);
}
}
}
}
std::cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
5000 939255322 47952340 92329911 61615795 40122788 47258178 29326499 9822850 42767362 86610596 60318756 52429688 87502511 50194916 96377063 74322128 19511341 28794957 53813791 79075058 35555414 5249682 45174421 101856091 25257909 94697470 45853817 82945426 108415825 41731145 87133877 75167193 598696...
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #5:
score: 0
Dangerous Syscalls
input:
774484 763692678 47702350 34856775 28447988 4178162 45063720 8232662 36845607 27038945 44858289 5952529 39159657 21628528 60199611 5544054 59216841 39287087 43449994 20034684 56440004 11583811 44465341 32347476 49196492 22731571 9481143 11726859 35167370 23103544 23109378 38822668 29778048 58004104 ...
output:
result:
Subtask #3:
score: 0
Dangerous Syscalls
Test #10:
score: 0
Dangerous Syscalls
input:
1000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
result:
Subtask #4:
score: 0
Dangerous Syscalls
Test #14:
score: 0
Dangerous Syscalls
input:
174457 888 0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 1...
output:
result:
Subtask #5:
score: 0
Dangerous Syscalls
Test #18:
score: 0
Dangerous Syscalls
input:
343922 773619774 0 8292680 5684115 0 0 170056 5385926 0 0 1588575 0 0 10947891 170867 35145 0 0 103085 7231562 0 0 0 0 11128944 0 4872226 0 2879880 7565181 0 8631665 0 5162564 9511835 514165 0 9628987 14357934 174784 0 12400154 0 0 8198218 0 8496060 0 0 0 0 10376826 3523227 0 14548249 0 6840016 0 0 ...
output:
result:
Subtask #6:
score: 0
Dangerous Syscalls
Test #24:
score: 0
Dangerous Syscalls
input:
468676 582048177 6889433 7293342 20676061 15545414 4911497 12352219 8921719 1705801 19695926 25259227 2645394 17518171 19753552 9449377 982708 22479531 1267985 15594372 20685422 9627290 2017543 6459134 18614020 16206301 14962487 12932255 7101003 29140540 6479702 20607124 2540287 15565156 20274141 11...