欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

cf1136E. Nastya Hasn't Written a Legend(二分 线段树)

程序员文章站 2022-04-08 19:25:21
题意 "题目链接" Sol yy出了一个暴躁线段树的做法。 因为题目保证了 $a_i + k_i define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second define int long lon ......

题意

sol

yy出了一个暴躁线段树的做法。

因为题目保证了

\(a_i + k_i <= a_{i+1}\)

那么我们每次修改时只需要考虑取max就行了。

显然从一个位置开始能影响到的位置是单调的,而且这些位置的每个改变量都是\((a_i + x) + \sum_{t=i}^{j-1} k_t\)

那么可以建两棵线段树分别维护这两部分的值

每次修改的时候二分出要修改的位置。

打cf一定要记得开数据结构题啊qwq

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
template <typename a, typename b> inline ll fp(a a, b p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, a[maxn], k[maxn], s[maxn], val[maxn];
//第一棵线段树支持区间赋值 / 区间求和
//第二棵线段树支持区间赋值 / 区间求和 .....
struct seg {
#define ls k << 1
#define rs k << 1 | 1
    int sum[maxn], tag[maxn], siz[maxn];
    void update(int k) {
        sum[k] = sum[ls] + sum[rs];
    }
    void ps(int k, int f) {
        sum[k] = siz[k] * f;
        tag[k] = f;
    }
    void pushdown(int k) {
        if(tag[k] == -inf) return ;
        ps(ls, tag[k]); ps(rs, tag[k]);
        tag[k] = -inf;
    }
    void build(int k, int l, int r) {
        siz[k] = r - l + 1; tag[k] = -inf;
        if(l == r) {sum[k] = val[l]; return ;}
        int mid = l + r >> 1;
        build(ls, l, mid); build(rs, mid + 1, r);
        update(k);
    }
    void intmem(int k, int l, int r, int ql, int qr, int v) {
        if(ql <= l && r <= qr) {ps(k, v); return ;}
        int mid = l + r >> 1;
        pushdown(k);
        if(ql <= mid) intmem(ls, l, mid, ql, qr, v);
        if(qr  > mid) intmem(rs, mid + 1, r, ql, qr, v);
        update(k);
    }
    int intquery(int k, int l, int r, int ql, int qr) {
        if(ql <= l && r <= qr) return sum[k];
        int mid = l + r >> 1;
        pushdown(k);
        if(ql > mid) return intquery(rs, mid + 1, r, ql, qr);
        else if(qr <= mid) return intquery(ls, l, mid, ql, qr);
        else return intquery(ls, l, mid, ql, qr) + intquery(rs, mid + 1, r, ql, qr);
    }
}t[2];
int geta(int x) {
    return t[0].intquery(1, 1, n, x, x) + k[x] - t[1].intquery(1, 1, n, x, x);
}
void modify(int x, int v) {
    int l = x, r = n, ans = l, tmp = geta(x);
    while(l <= r) {
        int mid = l + r >> 1;
        if(tmp + v + k[mid] - k[x] >= geta(mid)) l = mid + 1, ans = mid;
        else r = mid - 1;   
    }
    t[0].intmem(1, 1, n, x, ans, tmp + v);
    t[1].intmem(1, 1, n, x, ans, k[x]);
}
signed main() {
    n = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    for(int i = 2; i <= n; i++) 
        k[i] = read() + k[i - 1];
    memcpy(val, a, sizeof(a));
    t[0].build(1, 1, n); 
    memcpy(val, k, sizeof(k));
    t[1].build(1, 1, n);
    for(int i = 1; i <= n; i++) s[i] = s[i - 1] + k[i];
    int q = read();
    while(q--) {
        char c = 'g';
        while(c != 's' && c != '+') c = getchar();
        if(c == '+') {
            int x = read(), v = read(); 
            modify(x, v);
        }
        else {
            int l = read(), r = read();
            int pre = t[0].intquery(1, 1, n, l, r);
            int nxt = s[r] - s[l - 1] - t[1].intquery(1, 1, n, l, r);
            cout << pre + nxt << '\n'; 
        }
        c = 'g';
    }
    return 0;
}