首页 > 代码库 > hdu1698(线段树)

hdu1698(线段树)

 

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息)

 

技术分享
#pragma comment(linker,"/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <iostream>#include <algorithm>#include <queue>#include <cstdlib>#include <stack>#include <vector>#include <set>#include <map>#define LL long long#define mod 1000000007#define inf 0x3f3f3f3f#define N 100010#define FILL(a,b) (memset(a,b,sizeof(a)))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int sum[N<<2],col[N<<2];void Pushup(int rt){    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void Pushdown(int rt,int len){    if(col[rt])    {        col[rt<<1]=col[rt<<1|1]=col[rt];        sum[rt<<1]=col[rt]*(len-(len>>1));        sum[rt<<1|1]=col[rt]*(len>>1);        col[rt]=0;    }}void build(int l,int r,int rt){    col[rt]=0;    if(l==r)    {        sum[rt]=1;return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    Pushup(rt);}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R)    {        sum[rt]=(r-l+1)*c;        col[rt]=c;        return;    }    Pushdown(rt,r-l+1);    int m=(l+r)>>1;    if(L<=m)update(L,R,c,lson);    if(m<R)update(L,R,c,rson);    Pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return sum[rt];    }    Pushdown(rt,r-l+1);    int m=(l+r)>>1;    int res=0;    if(L<=m)res+=query(L,R,lson);    if(m<R)res+=query(L,R,rson);    return res;}int main(){    int n,m,t,cas=1;    int a,b,c;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        build(1,n,1);        scanf("%d",&m);        while(m--)        {            scanf("%d%d%d",&a,&b,&c);            update(a,b,c,1,n,1);        }        printf("Case %d: ",cas++);        printf("The total value of the hook is %d.\n",query(1,n,1,n,1));    }}
View Code

 

hdu1698(线段树)