学了一下午,终于学会了成段更新,以及lazy标记
#include<cstdio>
#include<iostream>
using namespace std;
#define lson id<<1
#define rson id<<1|1
const int Max = 500005;
struct line
{
int left,right,mark;
int sum;
int mid()
{
return (left + right) / 2;
}
}node[Max];
int a[Max];
void PushUp(int id)
{
node[id].sum = node[lson].sum + node[rson].sum;
}
void Pushdown(int id,int c)
{
if(node[id].mark>0)
{
node[lson].mark = node[rson].mark = node[id].mark;
node[lson].sum = (node[lson].right-node[lson].left + 1) * node[lson].mark;
node[rson].sum = (node[rson].right-node[rson].left + 1) * node[rson].mark;
node[id].mark = 0;
}
}
void _Build_Tree(int id,int l,int r)
{
node[id].left = l,node[id].right = r;
node[id].sum = node[id].mark = 1;
if(l==r)return;
int mid = node[id].mid();
_Build_Tree(lson,l,mid);
_Build_Tree(rson,mid+1,r);
PushUp(id);
}
int n;
void upside(int l,int r,int c,int id)
{
if(node[id].mark == c)return;
if(node[id].left == l && node[id].right ==r)
{
node[id].mark = c;
node[id].sum = (r - l + 1) * c;
return ;
}
int mid = node[id].mid();
Pushdown(id,c);
if(l > mid)upside(l,r,c,rson);
else if(r<=mid)upside(l,r,c,lson);
else {
upside(l,mid,c,lson);
upside(mid+1,r,c,rson);
}
PushUp(id);
}
int main()
{
int n,m;
int T,l,r,c,cn = 0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
_Build_Tree(1,1,n);
while(m--)
{
scanf("%d %d %d",&l,&r,&c);
upside(l,r,c,1);
}
printf("Case %d: The total value of the hook is %d.\n",++cn,node[1].sum);
}
}