LA 4108 (线段树)

时间:2022-08-21 10:49:36

区间更新 + 统计更新长度

稍稍不注意就T了

#include<bits/stdc++.h>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
const int maxn = 100000 + 131;
int Ma[maxn<<2], Mi[maxn<<2], Lazy[maxn<<2];
int Cnt; void PushUp(int rt)
{
Ma[rt] = max(Ma[rt<<1], Ma[rt<<1|1]);
Mi[rt] = min(Mi[rt<<1], Mi[rt<<1|1]);
} void PushDown(int rt)
{
if(Lazy[rt] != -1)
{
Lazy[rt<<1] = Lazy[rt<<1|1] = Lazy[rt];
Ma[rt<<1] = Ma[rt<<1|1] = Lazy[rt];
Mi[rt<<1] = Mi[rt<<1|1] = Lazy[rt];
Lazy[rt] = -1;
}
} void Build(int l, int r, int rt)
{
Ma[rt] = Mi[rt] = 0;
Lazy[rt] = -1;
if(l == r) return ;
int m = (l + r) >> 1;
Build(lson), Build(rson);
} void Query(int L, int R, int val, int l,int r,int rt)
{
////////////////////////////////////
if(L <= l && r <= R)
{
if(val < Mi[rt]) return ;
if(val >= Ma[rt])
{
Ma[rt] = Mi[rt] = val;
Lazy[rt] = val;
Cnt += r - l + 1;
return ;
}
if(l == r) return ;
}
///////////////////////////////////////
PushDown(rt);
int m = (l + r) >> 1;
if(L <= m) Query(L, R, val, lson);
if(R > m) Query(L, R, val, rson);
PushUp(rt);
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
Cnt = 0;
Build(1,100000,1);
int l, r, val;
for(int i = 0; i < n; ++ i)
{
scanf("%d%d%d",&l,&r,&val);
r--;
Query(l,r,val,1,100000,1);
}
printf("%d\n",Cnt);
}
return 0;
}