hdu 1698 Just a Hook 基本线段树

使用线段树更新每段区间的奖(1,2,3),最后在统计整段区间的数和,基本线段树,果断1A啊

#include<iostream>
#include<stdio.h>
using namespace std;
#define N 100000
struct node{
    int l,r,p;
}a[N*4];
int n;
void build(int left,int right,int i){
    a[i].l=left;
    a[i].r=right;
    a[i].p=1;
    if(a[i].l==a[i].r){
        return ;
    }
    int mid=(a[i].l+a[i].r)>>1;
    build(left,mid,i*2);
    build(mid+1,right,i*2+1);
//    a[i].p=a[i*2].p+a[i*2+1].p;
}
void updata(int left,int right,int i,int p){
    if(left==a[i].l&&a[i].r==right){
        a[i].p=p;
        return ;
    }
    if(a[i].p>=1){
        a[i*2].p=a[i].p;
        a[i*2+1].p=a[i].p;
        a[i].p=-1;
    }
    int mid=(a[i].l+a[i].r)>>1;
    if(mid>=right)
        updata(left,right,i*2,p);
    else if(mid<left)
        updata(left,right,i*2+1,p);
    else{
        updata(left,mid,i*2,p);
        updata(mid+1,right,i*2+1,p);
    }
 //   a[i].p=a[i*2].p+a[i*2+1].p;
}
int ans=0;
void sum(int i){
 //   cout<<a[i].l<<" "<<a[i].r<<" "<<a[i].p<<endl;
    if(a[i].p!=-1){
        ans+=a[i].p*(a[i].r-a[i].l+1);
        return ;
    }
    sum(i*2);
    sum(i*2+1);

}
int main(){
    int t,s,x,y,z;
    scanf("%d",&t);
    int cou=1;
    while(t--){
        scanf("%d %d",&n,&s);
        build(1,n,1);
        while(s--){
            scanf("%d%d%d",&x,&y,&z);
            updata(x,y,1,z);
        }
        ans=0;
        sum(1);
        printf("Case %d: The total value of the hook is %d.\n",cou++,ans);
    }
}

posted @ 2017-06-10 16:38  wzzkaifa  阅读(103)  评论(0编辑  收藏  举报