QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#345289 | #6675. DS Team Selection 2 | nguyentunglam | WA | 9ms | 41212kb | C++17 | 5.7kb | 2024-03-06 19:07:10 | 2024-03-06 19:07:10 |
Judging History
answer
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 1e5 + 10;
int n, q;
int last[N], a[N], L[N], R[N], t[N], x[N], y[N], idx[N];
vector<int> query[N], event[N];
struct IT {
long long T[N << 2], _T[N << 2], cnt[N << 2], cntw[N << 2];
long long lazy[2][N << 2];
pair<long long, long long> mx[N << 2];
void push(int s, int l, int r) {
if (lazy[0][s] != -1) {
T[s] = lazy[0][s] * cnt[s];
mx[s].first = lazy[0][s];
if (l != r) {
lazy[0][s << 1] = lazy[0][s << 1 | 1] = lazy[0][s];
lazy[1][s << 1] = lazy[1][s << 1 | 1] = 0;
}
}
if (lazy[1][s]) {
T[s] += lazy[1][s] * cntw[s];
mx[s].first += mx[s].second * lazy[1][s];
if (l != r) {
lazy[1][s << 1] += lazy[1][s];
lazy[1][s << 1 | 1] += lazy[1][s];
}
}
lazy[0][s] = -1;
lazy[1][s] = 0;
}
void Set (int s, int l, int r, int pos, long long val) {
push(s, l, r);
if (l == r) {
if (val < 0) {
T[s] = cnt[s] = cntw[s] = 0;
mx[s] = make_pair(0, 0);
}
else {
T[s] = val;
cnt[s] = 1;
cntw[s] = pos;
mx[s] = make_pair(val, pos);
}
return;
}
int mid = l + r >> 1;
if (pos <= mid) Set(s << 1, l, mid, pos, val);
else Set(s << 1 | 1, mid + 1, r, pos, val);
push(s << 1, l, mid);
push(s << 1 | 1, mid + 1, r);
T[s] = T[s << 1] + T[s << 1 | 1];
cnt[s] = cnt[s << 1] + cnt[s << 1 | 1];
cntw[s] = cntw[s << 1] + cntw[s << 1 | 1];
mx[s] = max(mx[s << 1], mx[s << 1 | 1]);
}
void up(int s, int l, int r, int from, int to, bool t, long long val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[t][s] = val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, t, val);
up(s << 1 | 1, mid + 1, r, from, to, t, val);
T[s] = T[s << 1] + T[s << 1 | 1];
mx[s] = max(mx[s << 1], mx[s << 1 | 1]);
}
long long get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return 0;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return get(s << 1, l, mid, from, to) + get(s << 1 | 1, mid + 1, r, from, to);
}
int Find (long long val) {
int s = 1, l = 1, r = n;
while (l != r) {
push(s, l, r);
int mid = l + r >> 1;
push(s << 1, l, mid);
// if (val == 7) cout << l << " " << r << " " << T[s << 1] << endl;
if (mx[s << 1].first <= val) {
s = s << 1 | 1;
l = mid + 1;
}
else {
s = s << 1;
r = mid;
}
}
push(s, l, r);
return mx[s].first <= val ? l + 1 : l;
}
} it1, it2;
struct line {
long long m, c;
long long eval (long long x) {
return m * x + c;
}
float cut (line l) {
return (float) (c - l.c) / (l.m - m);
}
};
struct CHT {
vector<line> st;
void add (line cur) {
while (st.size() >= 2 && st.back().cut(cur) >= st.back().cut(st.end()[-2])) st.pop_back();
st.push_back(cur);
}
long long query(long long x) {
int l = 0, r = st.size() - 1;
while (l < r) {
int mid = l + r >> 1;
if (st[mid].eval(x) <= st[mid + 1].eval(x)) r = mid;
else l = mid + 1;
}
return st[l].eval(x);
}
} cht;
int32_t main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
if (fopen(task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
int c = 0;
vector<line> arr;
for(int i = 1; i <= q; i++) {
cin >> t[i];
if (t[i] == 1) cin >> x[i];
if (t[i] == 3) cin >> x[i] >> y[i];
if (t[i] == 2) c++;
if (t[i] == 1) {
idx[arr.size()] = i;
arr.push_back({c, x[i]});
}
}
for(int i = 1; i <= n; i++) {
L[i] = 0, R[i] = arr.size() - 1;
// cout << last[i] << " "; cout << endl;
last[i] = -1;
}
for(int loop = 1; loop <= 20; loop++) {
bool stop = 1;
for(int i = 1; i <= n; i++) if (L[i] <= R[i]) {
stop = 0;
query[L[i] + R[i] >> 1].push_back(i);
}
if (stop) break;
for(int i = 0; i < arr.size(); i++) {
cht.add(arr[i]);
for(auto &j : query[i]) {
long long val = cht.query(-j);
if (val < a[j]) {
last[j] = i;
R[j] = i - 1;
} else L[j] = i + 1;
} query[i].clear();
}
cht.st.clear();
}
for(int i = 1; i <= n; i++) {
// cout << last[i] << " ";
if (last[i] >= 0) event[idx[last[i]]].push_back(i);
}
// cout << endl;
for(int i = 1; i <= 4 * n; i++) {
it1.lazy[0][i] = it2.lazy[0][i] = -1;
}
for(int i = 1; i <= n; i++) it1.Set(1, 1, n, i, a[i]);
for(int i = 1; i <= q; i++) {
if (t[i] == 1) {
for(auto &j : event[i]) {
it1.Set(1, 1, n, j, -1);
it2.Set(1, 1, n, j, x[i]);
// cout << j << "##\n";
}
int start = it2.Find(x[i]);
// cout << start << "@\n";
it2.up(1, 1, n, start, n, 0, x[i]);
}
if (t[i] == 2) {
it1.up(1, 1, n, 1, n, 1, 1LL);
it2.up(1, 1, n, 1, n, 1, 1LL);
// cout << it1.get(1, 1, n, 6, 9) << "@@\n";
}
if (t[i] == 3) {
long long sum1 = it1.get(1, 1, n, x[i], y[i]);
long long sum2 = it2.get(1, 1, n, x[i], y[i]);
// cout << it1.get(1, 1, n, 6, 8) << endl;
// cout << sum1 << " " << sum2 << endl;
cout << sum1 + sum2 << endl;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 38648kb
input:
13 11 6 14 14 6 3 6 4 13 10 3 12 5 11 1 2 2 2 2 1 11 3 4 6 2 1 6 2 1 9 3 2 13
output:
33 107
result:
ok 2 number(s): "33 107"
Test #2:
score: -100
Wrong Answer
time: 9ms
memory: 41212kb
input:
5000 5000 29940 259997 53132 912489 608312 594283 432259 344137 889466 383028 320097 337418 571199 372832 563110 542407 133378 998389 238387 120880 477310 634888 191990 133585 935315 558139 141724 893331 190118 991968 843042 384930 935256 891482 123419 91431 955722 376987 197566 106433 234494 645967...
output:
512185934 455189773 121665669 408693244 345526480 53168744 287474075 355917030 258017802 47619939 395552726 404618192 215108915 3639429 279866050 221914913 21616647 113757623 22838450 44241592 68851111 113835794 44867948 103210090 50380019 59408992 1636374 91010136 209884841 25419603 133466610 26192...
result:
wrong answer 5th numbers differ - expected: '291779262', found: '345526480'