QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#107580 | #3558. Constellation 3 | bashkort | 0 | 2ms | 3384kb | C++20 | 2.6kb | 2023-05-22 03:10:18 | 2023-05-22 03:10:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T>
struct Fenwick {
int n;
vector<T> a;
Fenwick() = default;
explicit Fenwick(int n) : n(n), a(n + 1) {}
void modify(int x, T v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i] += v;
}
}
void modify(int l, int r, T v) {
if (l >= r) return;
modify(l, v), modify(r, -v);
}
T sum(int x) {
T ans = 0;
for (int i = x + 1; i > 0; i -= i & -i) {
ans += a[i];
}
return ans;
}
T rangeSum(int l, int r) { //[l, r)
if (l >= r) return 0;
return sum(r - 1) - sum(l - 1);
}
int kth(T k) {
int x = 0;
for (int i = 1 << __lg(n); i; i >>= 1) {
if (x + i <= n && k > a[x + i]) {
x += i;
k -= a[x];
}
}
return x;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), L(n), R(n);
vector<pair<int, int>> stk;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int m;
cin >> m;
vector<int> x(m), y(m), c(m);
vector<vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
cin >> x[i] >> y[i] >> c[i];
adj[--x[i]].push_back(i);
}
stk = {{1e9 + 7, -1}};
for (int i = 0; i < n; ++i) {
while (!stk.empty() && stk.back().first <= a[i]) {
stk.pop_back();
}
stk.emplace_back(a[i], i);
for (int j : adj[i]) {
L[j] = lower_bound(stk.rbegin(), stk.rend(), pair{y[j], -1})->second + 1;
}
}
stk = {{1e9 + 7, n}};
for (int i = n - 1; i >= 0; --i) {
while (!stk.empty() && stk.back().first <= a[i]) {
stk.pop_back();
}
stk.emplace_back(a[i], i);
for (int j : adj[i]) {
R[j] = lower_bound(stk.rbegin(), stk.rend(), pair{y[j], -1})->second - 1;
}
}
vector<int> ord(m);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return array{R[i] - L[i], L[i], i} < array{R[j] - L[j], L[j], j};
});
Fenwick<ll> fn(n);
ll ans = accumulate(c.begin(), c.end(), 0LL);
for (int i : ord) {
ll diff = c[i] - fn.sum(x[i]);
if (diff > 0) {
ans -= diff;
fn.modify(L[i], R[i] + 1, diff);
}
}
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: 14
Accepted
time: 2ms
memory: 3384kb
input:
297 296 275 40 154 200 77 201 106 133 163 127 268 225 154 27 202 272 176 133 116 151 11 262 281 188 152 249 53 133 106 160 62 104 210 54 201 184 110 199 217 155 193 168 239 277 22 230 187 201 14 85 100 159 129 69 241 150 10 20 263 285 76 219 52 40 241 126 182 23 55 243 145 203 163 251 243 13 292 249...
output:
4354552
result:
ok single line: '4354552'
Test #2:
score: -14
Dangerous Syscalls
input:
285 243 196 230 42 278 255 93 14 131 30 113 79 6 15 231 98 99 171 144 65 218 73 128 214 120 53 3 272 52 279 243 171 74 179 62 130 22 69 226 283 276 82 79 50 228 114 72 150 266 200 150 118 8 181 182 211 211 199 100 96 149 35 47 93 226 257 25 230 170 110 265 19 191 241 181 120 87 132 76 5 16 175 235 2...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%