您的位置:首页 > 产品设计 > UI/UE

CSU 1532: JuQueen(线段树)

2015-06-05 00:04 453 查看

1532: JuQueen

Time Limit: 5 Sec Memory Limit:
512 MB

Submit: 363 Solved: 110

[Submit][Status][Web
Board]

Description




Input




Output




Sample Input

10 10 5
state 0
groupchange 2 9 7
state 9
groupchange 0 2 10
change 0 -5

Sample Output

0
7
7
3
-3

HINT

题意: 输入 :c n q ->给你初始[0,c)去为0的区间,q个操作,n是区间数的上限,即不能超过n

q次操作: state id 输出下标为id 的数

groupchange l r val 区间【l,r】上的每个数:val>0,加1 val次,如果有一个数==n,停止操作;

val<0,减1 val次,如果有一个数==0,停止操作;输出实际加或减的次数。

change l val 同上,改为单点操作

题解:线段树维护区间最大值计最小值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 4587530
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 100010;

using namespace std;
struct node {
    int Max;
    int Min;
    int se;
} tree[N<<2];

int n,c,q;
int Max,Min;

void push_up(int idx) {
    tree[idx].Max=max(tree[lc].Max,tree[rc].Max);
    tree[idx].Min=min(tree[lc].Min,tree[rc].Min);
}

void build(int l,int r,int idx) {
    tree[idx].se=0;
    if(l==r) {
        tree[idx].Max=0;
        tree[idx].Min=0;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(idx);
}

void push_down(int idx) {
    if(tree[idx].se) {
        tree[lc].se+=tree[idx].se;
        tree[rc].se+=tree[idx].se;
        tree[lc].Max+=tree[idx].se;
        tree[lc].Min+=tree[idx].se;
        tree[rc].Max+=tree[idx].se;
        tree[rc].Min+=tree[idx].se;
        tree[idx].se=0;
    }
}

void updata(int l,int r,int idx,int x,int y,int val) {
    if(l>=x&&r<=y) {
        tree[idx].Max+=val;
        tree[idx].Min+=val;
        tree[idx].se+=val;
        return;
    }
    push_down(idx);
    int mid=(l+r)>>1;
    if(x<=mid)updata(lson,x,y,val);
    if(y>mid)updata(rson,x,y,val);
    push_up(idx);
}

int query(int l,int r,int idx,int x,int y) {
    if(l>=x&&r<=y) {
        Min=min(tree[idx].Min,Min);
        return  tree[idx].Max;
    }
    push_down(idx);
    int mid=(l+r)>>1;
    int res=0;
    if(x<=mid)res=max(res,query(lson,x,y));
    if(y>mid)res=max(res,query(rson,x,y));
    return res;
}

int main() {
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&c,&n,&q)) {
        char s[20];
        int l,r,val;
        build(1,c,1);
        while(q--) {
            scanf("%s",s);
            if(s[0]=='s') {
                scanf("%d",&l);
                printf("%d\n",query(1,c,1,l+1,l+1));
            } else if(s[0]=='g') {
                scanf("%d%d%d",&l,&r,&val);
                Min=INF;
                if(val==0) {
                    printf("0\n");
                    continue;
                }
                int x=query(1,c,1,l+1,r+1);
               // printf("x=%d\n",x);
                if(val>0) {
                    if(n-x>=val) {
                        printf("%d\n",val);
                        updata(1,c,1,l+1,r+1,val);
                    } else {
                        printf("%d\n",n-x);
                        updata(1,c,1,l+1,r+1,n-x);
                    }
                } else {
                    int v=-val;
                    if(Min>=v) {
                        printf("%d\n",val);
                        updata(1,c,1,l+1,r+1,val);
                    } else {
                        printf("%d\n",-1*Min);
                        updata(1,c,1,l+1,r+1,-1*Min);
                    }
                }
            } else {
                scanf("%d%d",&l,&val);
                if(val==0) {
                    printf("0\n");
                    continue;
                }
                Min=INF;
                int x=query(1,c,1,l+1,l+1);
                if(val>0) {
                    if(n-x>=val) {
                        printf("%d\n",val);
                        updata(1,c,1,l+1,l+1,val);
                    } else {
                        printf("%d\n",n-x);
                        updata(1,c,1,l+1,l+1,n-x);
                    }
                } else {
                    int v=-val;
                    if(Min>=v) {
                        printf("%d\n",val);
                        updata(1,c,1,l+1,l+1,val);
                    } else {
                        printf("%d\n",-1*Min);
                        updata(1,c,1,l+1,l+1,-1*Min);
                    }
                }
            }

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