标签:clu 一个 问题 cst while char problem queue include
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define M 500010
#define ls now << 1
#define rs now << 1 | 1
#define lson l, mid, now << 1
#define rson mid + 1, r, now << 1 | 1
#define ll long long
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int n, q, x, y, z;
ll sum[M << 2], minn[M << 2], maxx[M << 2];
void pushup(int now) {
sum[now] = sum[ls] + sum[rs];
minn[now] = min(minn[ls], sum[ls] + minn[rs]);
maxx[now] = max(maxx[ls], sum[ls] + maxx[rs]);
}
void modify(int l, int r, int now, int pl, int v) {
if(l > pl || r < pl) return;
if(l == r) {
sum[now] = v;
minn[now] = min(v, 0);
maxx[now] = max(v, 0);
return;
}
int mid = (l + r) >> 1;
modify(lson, pl, v), modify(rson, pl, v);
pushup(now);
}
ll s;
ll merge(ll v, int now) {
if(v + maxx[now] > s) return s - maxx[now] + sum[now];
if(v + minn[now] < 0) return sum[now] - minn[now];
return v + sum[now];
}
ll query(int l, int r, int now, ll v) {
if(l == r) return max(min(s, sum[now] + v), 0ll);
int mid = (l + r) >> 1;
if(maxx[rs] - minn[rs] > s) return query(rson, 0);
else return merge(query(lson, v), rs);
}
int main() {
// freopen("stone1.in", "r", stdin);
n = read(), q = read();
x = read(), y = read();
for(int i = 1; i <= n; i++) {
z = read();
if(i % 2 == 0) z = -z;
modify(1, n, 1, i, z);
}
while(q--) {
int op = read();
if(op == 1) x = read();
else if(op == 2) y = read();
else {
int pl = read(), v = read();
if(pl % 2 == 0) v = -v;
modify(1, n, 1, pl, v);
}
s = x + y;
cout << query(1, n, 1, x) << "\n";
}
return 0;
}
标签:clu 一个 问题 cst while char problem queue include
原文地址:https://www.cnblogs.com/luoyibujue/p/10713334.html