线段树区间染色

题目要求最大的连续段的左端点,我们在查询的时候返回最左端即可,注意查找顺序,应该从左到右!!

另外这类染色的push_down其实比较简单,直接染成上一层的标记即可

push_up和连续子段和有点像,需要维护前缀和后缀

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C yql){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % yql)if(p & 1)ans = 1LL * x * ans % yql;
return ans;
}
const int N = 50005;
int n, m;
int prefix[N<<2], suffix[N<<2], tree[N<<2], lazy[N<<2]; void push_up(int rt, int l, int r){
int lson = rt << 1, rson = rt << 1 | 1;
int mid = (l + r) >> 1;
tree[rt] = max(tree[lson], tree[rson], suffix[lson] + prefix[rson]);
prefix[rt] = prefix[lson], suffix[rt] = suffix[rson];
if(tree[lson] == mid - l + 1) prefix[rt] += prefix[rson];
if(tree[rson] == r - mid) suffix[rt] += suffix[lson];
} void push_down(int rt, int l, int r){
if(lazy[rt] != -1){
int lson = rt << 1, rson = rt << 1 | 1;
int mid = (l + r) >> 1;
lazy[lson] = lazy[rson] = lazy[rt];
prefix[lson] = suffix[lson] = tree[lson] = lazy[rt] * (mid - l + 1);
prefix[rson] = suffix[rson] = tree[rson] = lazy[rt] * (r - mid);
lazy[rt] = -1;
}
} void buildTree(int rt, int l, int r){
if(l == r){
prefix[rt] = suffix[rt] = tree[rt] = 1;
lazy[rt] = -1;
return;
}
lazy[rt] = -1;
int mid = (l + r) >> 1;
buildTree(rt << 1, l, mid);
buildTree(rt << 1 | 1, mid + 1, r);
push_up(rt, l, r);
} void modify(int rt, int l, int r, int modifyL, int modifyR, int status){
if(l == modifyL && r == modifyR){
tree[rt] = prefix[rt] = suffix[rt] = (r - l + 1) * status;
lazy[rt] = status;
return;
}
push_down(rt, l, r);
int mid = (l + r) >> 1;
if(modifyL > mid) modify(rt << 1 | 1, mid + 1, r, modifyL, modifyR, status);
else if(modifyR <= mid) modify(rt << 1, l, mid, modifyL, modifyR, status);
else{
modify(rt << 1, l, mid, modifyL, mid, status);
modify(rt << 1 | 1, mid + 1, r, mid + 1, modifyR, status);
}
push_up(rt, l, r);
} int query(int rt, int l, int r, int len){
if(prefix[rt] >= len) return l;
push_down(rt, l, r);
int mid = (l + r) >> 1, lson = rt << 1, rson = rt << 1 | 1;
if(tree[lson] >= len) return query(lson, l, mid, len);
//else if(tree[rson] >= len) return query(rson, mid + 1, r, len);
else if(suffix[lson] + prefix[rson] >= len) return mid - suffix[lson] + 1;
else if(tree[rson] >= len) return query(rson, mid + 1, r, len);
else return 0;
} int main(){ n = read(), m = read();
buildTree(1, 1, n);
while(m --){
int opt = read();
if(opt == 1){
int len = read();
int pos = query(1, 1, n, len);
if(pos != 0 ) modify(1, 1, n, pos, pos + len - 1, 0);
printf("%d\n", pos);
}
else if(opt == 2){
int x = read(), y = read();
modify(1, 1, n, x, x + y - 1, 1);
}
}
return 0;
}
05-08 15:51