[BZOJ4355] Play with sequence(線段樹)

題意

  • 給你一個序列,有三種操作, 11是區間賦值,22是區間加法後區間取對00maxmax,33是詢問區間內00的個數。

首先11操作可以轉化爲區間加上inf-inf後取maxmax,那麼我們就只要維護區間加和區間取maxmax操作了,區間取maxmax操作可以用吉司機線段樹來實現,這裏有個比較重要的點就是加的inf-inf不能影響到區間次小值,就是次小值要是一個更高階的infinf,不然會影響一些關係。

然後最主要的應該是pushdownpushdown,先下放加法標記再下放取maxmax標記,注意加法標記要對取maxmax造成影響,取maxmax標記修改的時候下放加法標記,然後就沒了,複雜度O(mlog2n)O(mlog^2n)

 
#include<cstdio>
#include<iostream>
#include<ctype.h>
#include<algorithm>
#include<vector>
 
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define go(x, i) for(register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define FOR(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define debug(x) cout << #x << " = " << x << endl
#define mem(a, b) memset(a, b, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(a))
#define min(a, b) (a < b ? a : b)
#define max(a, b) (b < a ? a : b)
#define inf (0x3f3f3f3f)
#define INF ((ll)1e12)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define y1 orzorz
 
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;
typedef std::pair<ll, int> PLI;
typedef std::pair<int, int> PII;
typedef long double ldb;
typedef double db;
 
namespace IO {
#define getc() ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_ ++)
#define putc(x) *nowps ++ = (x)
    const uint Buffsize = 1 << 15, Output = 1 << 23;
    static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_;
    static char Out[Output], *nowps = Out;
    inline void flush(){fwrite(Out, 1, nowps - Out, stdout); nowps = Out;}
    template<class T>inline void read(T &_) {
        _ = 0; static char __; T ___ = 1;
        for(__ = getc(); !isdigit(__); __ = getc()) if(__ == '-') ___ = -1;
        for(; isdigit(__); __ = getc()) _ = (_ << 3) + (_ << 1) + (__ ^ 48);
        _ *= ___;
    }
    template<class T>inline void write(T _, char __ = '\n') {
        if(!_) putc('0');
        if(_ < 0) putc('-'), _ = -_;
        static uint sta[111], tp;
        for(tp = 0; _; _ /= 10) sta[++ tp] = _ % 10;
        for(; tp; putc(sta[tp --] ^ 48)); putc(__);
    }
    template<class T>inline bool chkmax(T &_, T __) {return _ < __ ? _ = __, 1 : 0;}
    template<class T>inline bool chkmin(T &_, T __) {return _ > __ ? _ = __, 1 : 0;}
}
 
using namespace std;
using namespace IO;
 
const int N = 3e5 + 10;
 
int n, q;
 
int dbg;
 
struct Segment_Tree {
#define ls (bh << 1)
#define rs (ls | 1)
#define mid ((l + r) >> 1)
#define lson ls, l, mid
#define rson rs, mid + 1, r
    ll mn[N << 2], smn[N << 2], tagmax[N << 2], nmn[N << 2], add[N << 2];
 
	void pushup(int bh) {
		int lc = ls, rc = rs;
		if(mn[lc] == mn[rc]) {
			mn[bh] = mn[lc];
			nmn[bh] = nmn[lc] + nmn[rc];
			smn[bh] = min(smn[lc], smn[rc];
		}
		else {
			if(mn[lc] > mc[rc]) swap(lc, rc);
			mn[bh] = mn[lc], nmn[bh] = nmn[lc];
			smn[bh] = min(smn[lc], mn[rc]);
		}
	}

	void modifyadd(int bh, ll val) {
		if(tagmax[bh] ^ -INF) tagmax[bh] += val;
		add[bh] += val, mn[bh] += val, smn[bh] += val;
	}

	void modifymax(int bh, int l, int r, ll val) {
		if(mn[bh] >= val) return;
		if(l ^ r && add[bh]) modifyadd(ls, add[bh]), modifyadd(rs, add[bh]), add[bh] = 0;
		if(smn[bh] > val) mn[bh] = tagmax[bh] = val;
		else tagmax[bh] = -INF, modifymax(lson, val), modifymax(rson, val), pushup(bh);
	}

	void pushdown(int bh, int l, int r) {
		if(add[bh]) 
			modifyadd(ls, add[bh]), modifyadd(rs, add[bh]), add[bh] = 0;
		if(tagmax[bh] != -INF) 
			modifymax(lson, tagmax[bh]), modifymax(rson, tagmax[bh]), tagmax[bh] = -INF;
	}

	void build(int bh, int l, int r) {
		tagmax[bh] = -INF, add[bh] = 0;
		if(l == r) scanf("%lld", &mn[bh]), smn[bh] = 1e18, nmn[bh] = 1;
		else build(lson), build(rson), pushup(bh);
	}

	void updateadd(int bh, int l, int r, int x, int y, ll z) {
		if(x <= l && r <= y) modifyadd(bh, z);
		else {
			pushdown(bh, l, r);
			if(x <= mid) updateadd(lson, x, y, z);
			if(y > mid) updateadd(rson, x, y, z);
			pushup(bh);
		}
	}

	void updatemax(int bh, int l, int r, int x, int y, ll z) {
		if(x <= l && r <= y) modifymax(bh, l, r, z);
		else {
			pushdown(bh, l, r);
			if(x <= mid) updatemax(lson, x, y, z);
			if(y > mid) updatemax(rson, x, y, z);
			pushup(bh);
		}
	}

	int query(int bh, int l, int r, int x, int y) {
		if(x > r || y < l || mn[bh] != 0) return 0;
		if(x <= l && r <= y) return nmn[bh];
		pushdown(bh, l, r);
		return query(lson, x, y) + query(rson, x, y);
	}

}T;

int main() {
#ifdef ylsakioi
	file("4355");
#endif
	scanf("%d%d", &n, &q);
	T.build(1, 1, n);
	For(i, 1, q) {
		dbg = i;
		int opt, x, y, z;
		read(opt);
		if(opt == 1) {
			read(x), read(y), read(z);
			T.updateadd(1, 1, n, x, y, -INF);
			T.updatemax(1, 1, n, x, y, z);
		}
		if(opt == 2) {
			read(x), read(y), read(z);
			T.updateadd(1, 1, n, x, y, z);
			T.updatemax(1, 1, n, x, y, 0);
		}
		if(opt == 3) {
			read(x), read(y);
			write(T.query(1, 1, n, x, y));
		}
	}
	return flush(), 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章