2020ICPC·小米 网络选拔赛第二场 - C Data Structure Problem(线段树+树状数组)
题面:
思路:
我们设:
(S(i)=sum_{j=1}^{i}b_j)
那么对于一个询问3,答案为:
[max(a_i+S(x)-S(i)),iin[0,x]
]
显然,对于每一个(iin[0,x]),(S(x))都相等。
那么可以将上式改为:
[S(x)+max(a_i-S(i)),iin[0,x]
]
则,上式的左侧(S(x))可以用单调修改,区间查询的树状数组来维护。
上式的右侧(a_i-S(i))可以用单调修改,区间修改,区间查询最大值的线段树来维护。
对于每一个操作1,
将线段树中(-a_x-S(x))改为(y-S(x)),只需要单点修改第(mathit x)个位置(y-a[x]),并更新(a[x])。
操作2:
将线段树区间([x,n])都加上(-y+b(x)),树状数组单点(mathit x) 增(y-b[x]),并更新(a[x])。
上面用到的加减法都是差分的思想。
操作3:
区间询问([1,x])中的(a_i-S(i))的最大值,并和( ext 0)取最大值,因为(mathit i) 可以取到0.
再加上(S(x))即是答案。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 200010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, m;
ll tree[maxn];
int lowbit(int x)
{
return (-x)& x;
}
void add(int pos, ll val)
{
while (pos <= n)
{
tree[pos] += val;
pos += lowbit(pos);
}
}
ll ask(int pos)
{
ll res = 0;
while (pos > 0)
{
res += tree[pos];
pos -= lowbit(pos);
}
return res;
}
ll a[maxn];
ll b[maxn];
struct node
{
int l, r;
ll val;
ll laze;
} segment_tree[maxn << 2];
ll sum[maxn];
void pushup(int rt)
{
segment_tree[rt].val = max(segment_tree[rt << 1].val, segment_tree[rt << 1 | 1].val);
}
void pushdown(int rt)
{
ll num = segment_tree[rt].laze;
segment_tree[rt].laze = 0ll;
segment_tree[rt << 1].laze += num;
segment_tree[rt << 1].val += num;
segment_tree[rt << 1 | 1].laze += num;
segment_tree[rt << 1 | 1].val += num;
}
void build(int rt, int l, int r)
{
segment_tree[rt].l = l;
segment_tree[rt].r = r;
segment_tree[rt].laze = 0;
if (l == r)
{
segment_tree[rt].val = a[l] - sum[l];
return;
}
int mid = (l + r) / 2;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void change1(int rt, int pos, ll num) {
if (segment_tree[rt].l == pos && segment_tree[rt].r == pos)
{
segment_tree[rt].val += num;
return;
}
pushdown(rt);
int mid = (segment_tree[rt].l + segment_tree[rt].r) / 2;
if (pos > mid)
{
change1(rt << 1 | 1, pos, num);
} else
{
change1(rt << 1, pos, num);
}
pushup(rt);
}
void change2(int rt, int l, int r, ll num)
{
if (l <= segment_tree[rt].l && segment_tree[rt].r <= r)
{
segment_tree[rt].val += num;
segment_tree[rt].laze += num;
return ;
}
pushdown(rt);
int mid = (segment_tree[rt].l + segment_tree[rt].r) / 2;
if (l <= mid)
{
change2(rt << 1, l, r, num);
}
if (r > mid)
{
change2(rt << 1 | 1, l, r, num);
}
pushup(rt);
}
ll ask(int rt, int l, int r)
{
if (l <= segment_tree[rt].l && segment_tree[rt].r <= r)
{
return segment_tree[rt].val;
}
pushdown(rt);
int mid = (segment_tree[rt].l + segment_tree[rt].r) / 2;
ll res = -1e18;
if (l <= mid)
{
res = max(res, ask(rt << 1, l, r));
}
if (r > mid)
{
res = max(res, ask(rt << 1 | 1, l, r));
}
return res;
}
int main()
{
#if DEBUG_Switch
freopen("C:\code\input.txt", "r", stdin);
#endif
//freopen("C:\code\output.txt","w",stdout);
while (~scanf("%d %d", &n, &m))
{
repd(i, 1, n)
{
a[i] = readint();
}
repd(i, 1, n)
{
b[i] = readint();
add(i, b[i]);
sum[i] = b[i] + sum[i - 1];// 建树时可以更快一点
}
build(1, 1, n);
while (m--)
{
int op = readint();
if (op == 1)
{
int x = readint();
ll y = readint();
change1(1, x, y - a[x]);
a[x] = y;
} else if (op == 2)
{
int x = readint();
ll y = readint();
change2(1, x, n, -y + b[x]);
add(x, y - b[x]);
b[x] = y;
} else
{
int x = readint();
ll ans = ask(1, 1, x);
ans = max(ans, 0ll);// S_x 也可以直接作为答案
ans += ask(x);
printf("%lld
", ans );
}
}
repd(i, 1, n)
{
tree[i] = 0ll;
}
}
return 0;
}