HDU 1698 Just a Hook (선분 트 리, 세그먼트 업데이트)

2060 단어 HDU
링크:
http://acm.hdu.edu.cn/showproblem.php?pid=1698
분석 과 총 결:
내 첫 번 째 라인 트 리 업데이트 문제... 유 여가 대 백서 + 바보 새끼 코드 보고 배 웠 어 요.
코드:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((left+right)>>1)
#define lson rt<<1,left,mid
#define rson rt<<1|1,mid+1,right
#define int64 long long

using namespace std;

const int MAXN = 100005;
int n,m;
int sum[MAXN<<2],col[MAXN<<2];

inline void push_up(int rt){
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
inline void push_down(int rt,int m){
    if(col[rt]){
        col[rt<<1] = col[rt<<1|1] = col[rt];
        sum[rt<<1] = (m-(m>>1)) * col[rt];
        sum[rt<<1|1] = (m>>1) * col[rt];
        col[rt] = 0;
    }
}
void build(int rt,int left,int right){
    col[rt] = 0;
    sum[rt] = 1;
    if(left==right)return;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int rt,int left,int right,int l,int r,int data){
    if(l<=left && right<=r) {
        col[rt] = data;
        sum[rt] = data*(right-left+1);
        return;
    }
    push_down(rt, right-left+1);
    int m = mid;
    if(l <= m) update(lson,l,r,data);
    if(r > m) update(rson,l,r,data);
    push_up(rt);
}
int main(){
    int T,x,y,z,cas=1;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for(int i=0; i<m; ++i){
            scanf("%d%d%d",&x,&y,&z);
            update(1,1,n,x,y,z);
        }
        printf("Case %d: The total value of the hook is %d.
",cas++,sum[1]); } return 0; }

- 생명의 의 미 는 의 미 를 부여 하 는 데 있다.
오리지널 http://blog.csdn.net/shuangde800, By DDouble (전재 표시)

좋은 웹페이지 즐겨찾기