首页 > 代码库 > hdu1698(线段树的区间替换)

hdu1698(线段树的区间替换)

HDU1698

 

#include <bits/stdc++.h>using namespace std;#define Maxn 1001000*4struct Node{    int lt,rt,val;}A[Maxn];int j = 1;void Build(int i,int lt,int rt){    A[i].lt = lt;    A[i].rt = rt;    A[i].val = 1;    if( lt == rt ){        return ;    }    int mid = (lt+rt) >> 1;    Build(i<<1,lt,mid);    Build(i<<1|1,mid+1,rt);}void update(int i,int lt,int rt,int val){    if( A[i].val == val ){        return ;    }    if( A[i].lt == lt && A[i].rt == rt ){        A[i].val = val;        return ;    }    if( A[i].val != -1 ){        A[i<<1].val = A[i<<1|1].val = A[i].val;        A[i].val = -1;    }    if( rt <= A[i<<1].rt ){        update(i<<1,lt,rt,val);    }else if( lt >= A[i<<1|1].lt ){        update(i<<1|1,lt,rt,val);    }else{        update(i<<1,lt,A[i<<1].rt,val);        update(i<<1|1,A[i<<1|1].lt,rt,val);    }}int find(int i){    if( A[i].val != -1 ){        return ( (A[i].rt-A[i].lt+1)*A[i].val);    }else{        return ( find(i<<1) + find(i<<1|1) );    }}int main(){    int T,N,a,b,c;    cin >> T;    while(T--){        scanf("%d",&N);        Build(1,1,N);        int n;        scanf("%d",&n);        while(n--){            scanf("%d%d%d",&a,&b,&c);            update(1,a,b,c);        }        printf("Case %d: The total value of the hook is %d.\n",j++,find(1));    }}

  

hdu1698(线段树的区间替换)