您的位置:首页 > 其它

XTU1238Segment Tree(线段树)解题思想必较经典

2015-06-14 08:12 190 查看

Segment Tree

Accepted : 9Submit : 123
Time Limit : 9000 MSMemory Limit : 65536 KB

Segment Tree

Problem Description:

A contest is not integrity without problems about data structure.

There is an array a[1],a[2],…,a
. And q questions of the following 4 types:

1 l r c - Update a[k] with a[k]+c for all l≤k≤r
2 l r c - Update a[k] with min{a[k],c} for all l≤k≤r;
3 l r c - Update a[k] with max{a[k],c} for all l≤k≤r;
4 l r - Ask for min{a[k]:l≤k≤r} and max{a[k]:l≤k≤r}.

Input

The first line contains a integer T(no more than 5) which represents the number of test cases.

For each test case, the first line contains 2 integers n,q (1≤n,q≤200000).

The second line contains n integers a1,a2,…,an which indicates the initial values of the array (|ai|≤10^9).

Each of the following q lines contains an integer t which denotes the type of i-th question. If t=1,2,3, 3 integers l,r,c follows. If t=4, 2 integers l,r follows. (1≤ti≤4,1≤li≤ri≤n)

If t=1, |ci|≤2000;

If t=2,3, |ci|≤10^9.

Output

For each question of type 4, output two integers denote the minimum and the maximum.

Sample Input

1

1 1

1

4 1 1

Sample Output

1 1

Source

XTU OnlineJudge

在输入时要注意:用%I64d AC,用%lld WA

解题:op=2 [l , r] c 时则区间[l , r]内的数小于等于 c 则 不用变,大于c 则都变成 c . op=3同理。所以我们只需要记录区间内的最大值和最小值,还有每个数都要加多少add.具体代码中体现。

#include<stdio.h>
#define LL long long
const int N = 200100;
const LL INF = 9999999999999;

struct Tree{
    LL maxt,mint,add;
}node[N*3];

LL max(LL a,LL b){return a>b?a:b;}
LL min(LL a,LL b){return a>b?b:a;}
void pushUP(int k){
    node[k].maxt=max(node[k<<1].maxt,node[k<<1|1].maxt);
    node[k].mint=min(node[k<<1].mint,node[k<<1|1].mint);
}
void pushDown(int k){
    if(node[k].add){
        node[k<<1].add+=node[k].add;
        node[k<<1].maxt+=node[k].add;
        node[k<<1].mint+=node[k].add;

        node[k<<1|1].add+=node[k].add;
        node[k<<1|1].maxt+=node[k].add;
        node[k<<1|1].mint+=node[k].add;

        node[k].add=0;
    }
    node[k<<1].maxt=min(node[k<<1].maxt,node[k].maxt);
    node[k<<1].maxt=max(node[k<<1].maxt,node[k].mint);
    node[k<<1].mint=max(node[k<<1].mint,node[k].mint);
    node[k<<1].mint=min(node[k<<1].mint,node[k].maxt);

    node[k<<1|1].maxt=min(node[k<<1|1].maxt,node[k].maxt);
    node[k<<1|1].maxt=max(node[k<<1|1].maxt,node[k].mint);
    node[k<<1|1].mint=max(node[k<<1|1].mint,node[k].mint);
    node[k<<1|1].mint=min(node[k<<1|1].mint,node[k].maxt);
}
void builde(int l,int r,int k){
    node[k].add=0;
    if(l==r){
        scanf("%I64d",&node[k].maxt);
        node[k].mint=node[k].maxt;
        return ;
    }
    int m=(l+r)>>1;
    builde(l,m,k<<1);
    builde(m+1,r,k<<1|1);
    pushUP(k);
}

int L,R;
LL C;

void updata1(int l,int r,int k){
    if(L<=l&&r<=R){
        node[k].add+=C;
        node[k].maxt+=C;
        node[k].mint+=C;
        return ;
    }
    int m=(l+r)>>1;
    pushDown(k);

    if(L<=m)
     updata1(l,m,k<<1);
    if(m<R)
     updata1(m+1,r,k<<1|1);
     pushUP(k);
}
void updata2(int l,int r,int k){

    if(L<=l&&r<=R){
        node[k].maxt=min(node[k].maxt,C);
        node[k].mint=min(node[k].mint,C);
        return ;
    }
    int m=(l+r)>>1;
    pushDown(k);

    if(L<=m)
     updata2(l,m,k<<1);
    if(m<R)
     updata2(m+1,r,k<<1|1);
     pushUP(k);
}
void updata3(int l,int r,int k){
    if(L<=l&&r<=R){
        node[k].maxt=max(node[k].maxt,C);
        node[k].mint=max(node[k].mint,C);
        return ;
    }
    int m=(l+r)>>1;
    pushDown(k);

    if(L<=m)
     updata3(l,m,k<<1);
    if(m<R)
     updata3(m+1,r,k<<1|1);
     pushUP(k);
}
LL maxans,minans;
void query(int l,int r,int k){
    if(L<=l&&r<=R){
        maxans=max(maxans,node[k].maxt);
        minans=min(minans,node[k].mint);
        return ;
    }
    int m=(l+r)>>1;
    pushDown(k);

    if(L<=m)
     query(l,m,k<<1);
    if(m<R)
     query(m+1,r,k<<1|1);
     pushUP(k);
}

int main(){
    int T,n,q,op;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&q);
        builde(1,n,1);
        while(q--){
            scanf("%d%d%d",&op,&L,&R);
            if(op!=4){
                scanf("%I64d",&C);
                if(op==1)updata1(1,n,1);
                else if(op==2)updata2(1,n,1);
                else if(op==3)updata3(1,n,1);
            }
            else{
                minans=INF; maxans=-INF;
                query(1,n,1);
                printf("%I64d %I64d\n",minans,maxans);
            }
        }
    }
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: