QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#343525 | #8286. Stacks | ucup-team173 | ML | 0ms | 0kb | C++17 | 6.5kb | 2024-03-02 18:09:15 | 2024-03-02 18:09:15 |
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back
#define SZ(x) (int((x).size()))
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
constexpr int B = 320;
void solve() {
int n, m;
cin >> n >> m;
vector op(m, array<ll, 5>());
for(int i = 0; i < m; i++) {
cin >> op[i][0];
if(op[i][0] == 1) {
cin >> op[i][1] >> op[i][2] >> op[i][3] >> op[i][4];
} else if(op[i][0] == 2) {
cin >> op[i][1] >> op[i][2] >> op[i][3];
} else {
cin >> op[i][1] >> op[i][2] >> op[i][3];
}
}
vector bel(n + 1, vi(B + 10));
vector pos(B + 10, vi());
vector lst(n + 1, vector<pii>(B + 10));
vector nxt(n + 1, vi(B + 10));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= B; j++) {
nxt[i][j] = j;
}
}
vector nodes(B + 10, vector(2 * B + 10, vector<pii>(B + 10)));
vector presum(B + 10, vector(2 * B + 10, vector<ll>(B + 10)));
vector top(B + 10, vector(2 * B + 10, 0));
vector del(B + 10, vector(2 * B + 10, 0ll));
vector cntsum(B + 10, vector(2 * B + 10, vector<ll>(B + 10)));
auto calc_cnt = [&](int t, int seg, pii lst) -> ll {
if(lst.fi == 0) return 0;
return cntsum[t][seg][lst.fi - 1] + lst.se;
};
auto calc_sum = [&](int t, int seg, pii lst) {
return presum[t][seg][lst.fi - 1] + 1ll * nodes[t][seg][lst.fi].se * lst.se;
};
function<int(vi&, int)> find = [&](vi &f, int x) {
return f[x] == x ? x : f[x] = find(f, f[x]);
};
auto delete_ = [&](int i, int j, ll d_) {
for(int t = i - 1; t && d_; t--) {
t = find(nxt[j], t);
if(t == 0) break;
int now_seg = bel[j][t];
ll now_lst = calc_cnt(t, now_seg, lst[j][t]);
if(d_ >= now_lst) {
d_ -= now_lst;
lst[j][t] = Mp(0, 0);
} else {
int lo = 0, hi = lst[j][t].fi, mid;
while(lo < hi) {
mid = (lo + hi + 1) / 2;
if(calc_cnt(t, now_seg, Mp(mid + 1, 0)) <= now_lst - d_)
lo = mid;
else hi = mid - 1;
}
lst[j][t] = Mp(lo, now_lst - d_ - calc_cnt(t, now_seg, Mp(lo, 0)));
}
if(lst[j][t] == Mp(0, 0)) nxt[j][t] = t - 1;
}
};
for(int i = 0; i * B < m; i++) {
vi deleted(n + 1);
for(int j = 0; j < B && i * B + j < m; j++) {
auto [ty, l, r, x, y] = op[i * B + j];
if(ty == 1 || ty == 2) {
pos[i].pb(l), pos[i].pb(r + 1);
}
}
pos[i].pb(n + 1), pos[i].pb(1);
sort(pos[i].begin(), pos[i].end());
pos[i].erase(unique(pos[i].begin(), pos[i].end()), pos[i].end());
for(int j = 1; j < SZ(pos[i]); j++) {
for(int k = pos[i][j - 1]; k < pos[i][j]; k++) {
bel[k][i] = j - 1;
}
}
for(int j = 0; j < B && i * B + j < m; j++) {
auto [ty, l, r, x, y] = op[i * B + j];
if(ty == 1) {
for(int k = bel[l][i]; pos[i][k] <= r; k++) {
int now = ++top[i][k];
nodes[i][k][now] = Mp(x, y);
cntsum[i][k][now] = cntsum[i][k][now - 1] + x;
presum[i][k][now] = presum[i][k][now - 1] + 1ll * x * y;
}
} else if(ty == 2) {
ll w = x;
for(int k = bel[l][i]; pos[i][k] <= r; k++) {
for(ll w_ = w; w_; ) {
if(top[i][k] == 0) {
del[i][k] += w_;
w_ = 0;
} else {
int now = top[i][k];
if(w_ >= nodes[i][k][now].fi) {
top[i][k]--;
w_ -= nodes[i][k][now].fi;
} else {
nodes[i][k][now].fi -= w_;
cntsum[i][k][now] -= w_;
presum[i][k][now] -= 1ll * w_ * nodes[i][k][now].se;
w_ = 0;
}
}
}
}
} else {
ll pos = l, p = r, q = x;
ll vis = 0, ans = 0;
delete_(i, pos, del[i][bel[pos][i]] - deleted[pos]);
deleted[pos] = del[i][bel[pos][i]];
for(int t = 0; t < i; t++) if(lst[pos][t].fi != 0) {
ll L = vis + 1, R = vis, seg = bel[pos][t];
ll now_lst = calc_cnt(t, seg, lst[pos][t]);
// ll now_lst = cntsum[t][seg][lst[pos][t].fi - 1] + lst[pos][t].se;
R += now_lst;
if(p <= L && R <= q) {
ans += calc_sum(t, seg, lst[pos][t]);
} else if(max(p, L) > min(q, R)) {
continue;
} else {
ll now_vis = vis;
for(int k = 1; k <= top[t][seg]; k++) {
ll now_L = now_vis + 1, now_R = now_vis + nodes[t][seg][k].fi;
ll len = min(q, now_R) - max(p, now_L) + 1;
len = max(len, 0ll);
ans += len * nodes[t][seg][k].se;
}
}
vis += now_lst;
}
ll seg = bel[pos][i];
for(int k = 1; k <= top[i][seg]; k++) {
ll now_L = vis + 1, now_R = vis + nodes[i][seg][k].fi;
ll len = min(q, now_R) - max(p, now_L) + 1;
len = max(0ll, len);
ans += len * nodes[i][seg][k].se;
}
cout << ans << '\n';
}
}
for(int j = 1; j <= n; j++) {
int seg = bel[j][i];
lst[j][i] = Mp(top[i][seg], nodes[i][seg][top[i][seg]].fi);
delete_(i, j, del[i][seg] - deleted[seg]);
}
}
}
signed main() {
ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
4907 4910 2 763 3330 1 3 307 1 1 1 2262 3430 22699 89397 1 1915 4000 51541 67587 2 212 2990 9763 2 1086 2162 1 2 1813 4496 16760 1 51 2796 68005 99390 1 1267 1519 74236 66178 3 1768 23808 54314 2 900 4122 27758 3 3287 17350 28989 2 3277 4024 3633 2 444 4866 1 2 353 4219 1061 1 987 3141 99906 17320 2...
output:
0 3032090730 478273950 859096795 200648623 98486697 691214382 123945 0 61782451 0 0 0 762429740 0 638060258 0 3504698464 0 0 6024704458 2625373140 961035066 0 3832874176 53899549 0 0 451585320 0 515151640 84280112 0 4707150236 1269900438 3724578987 0 0 4109884860 642842550 1027888122 113773506 59115...