题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1698
题解:
题目大意:给一个含有n个整数的数组,在给出m个操作,每操作有a,b,c三个步骤,表示将[a,b]该区间的值变为c,问最后的总和是多少。
线段树区间更新模版题。
代码:
#include <cmath>
#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
typedef long long ll;
const ll maxn =1e5+10;
#define lchild rt << 1, l, m
#define rchild rt << 1 | 1, m + 1, r
int tree[maxn<<2];
int lazy[maxn<<2];
void push_up(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void push_down(int rt,int len)
{
if(lazy[rt])
{
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
tree[rt<<1]=lazy[rt]*(len-(len>>1));
tree[rt<<1|1]=lazy[rt]*(len>>1);
lazy[rt]=0;
}
}
void build(int rt,int l,int r)
{
tree[rt]=1;
lazy[rt]=0;
if(l==r)
return;
int m=(l+r)>>1;
build(lchild);
build(rchild);
push_up(rt);
}
void updata(int L,int R,int delta,int rt,int l,int r)
{
if(L<=l&&R>=r)
{
tree[rt]=delta*(r-l+1);
lazy[rt]=delta;
return;
}
if(lazy[rt])
push_down(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
updata(L,R,delta,lchild);
if(R>m)
updata(L,R,delta,rchild);
push_up(rt);
}
/*int query(int L,int R,int rt,int l,int r)
{
if(L<=l&&R>=r)
return tree[rt];
if(lazy[rt])
push_down(rt,r-l+1);
int m=(l+r)>>1,ret=0;
if(L<=m)
ret+=query(L,R,lchild);
if(R>m)
ret+=query(L,R,rchild);
return ret;
}*/
int main()
{
int t;
scanf("%d",&t);
int id=1;
while(t--)
{
int n,q;
scanf("%d%d",&n,&q);
build(1,1,n);
while(q--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
updata(a,b,c,1,1,n);
}
printf("Case %d: The total value of the hook is %d.\n",id++, tree[1]);
}
}