zoukankan      html  css  js  c++  java
  • cf1136E. Nastya Hasn't Written a Legend(二分 线段树)

    题意

    题目链接

    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 << '
    ';}
    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 << '
    '; 
    		}
    		c = 'g';
    	}
        return 0;
    }
    
  • 相关阅读:
    小学教师资格考试——综合素质——材料分析
    小学教师资格考试——综合素质——教师基本能力
    牛客网暑假训练第一场——J Different Integers(莫队算法 & 树状数组)
    牛客网暑假训练第二场——A run(递推&前缀和)
    CSU
    ZOJ
    CSU
    CSU
    HDU
    Hrbust-2090 背包(最小不可构造数)
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10549196.html
Copyright © 2011-2022 走看看