hdu 1166 敌兵布阵

第一个线段树,参考别人代码

#include<iostream>

#include<cctype>

#include<cstdlib>

#include<cstring>

#include<cstdio>

using namespace std;



const int MAXN = 55555;

int sum[MAXN << 2];



void PushUp(int rt)

{

    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];

}



void build(int l, int r, int rt)

{

    if(l == r)

    {

        scanf("%d",&sum[rt]);

        return;

    }

    int mid = (l + r) >> 1;

    build(l, mid, rt << 1);

    build(mid + 1, r, rt << 1 | 1);

    PushUp(rt);

}



void update(int p, int add, int l, int r, int rt)

{

    if(l == r)

    {

        sum[rt] += add;

        return;

    }

    int mid = (l + r) >> 1;

    if(p <= mid)

        update(p, add, l, mid, rt << 1);

    else

        update(p, add, mid + 1, r, rt << 1 | 1);

    PushUp(rt);

}



int query(int ll, int rr, int l, int r, int rt)

{

    if(ll <= l && rr >= r)

        return sum[rt];

    int mid = (l + r) >> 1;

    int ret = 0;

    if(ll <= mid)

        ret += query(ll, rr, l, mid, rt << 1);

    if(rr > mid)

        ret += query(ll, rr, mid +1, r, rt << 1 | 1);

    return ret;

}



int main()

{

    int t,c;

    char d[10];

    scanf("%d", &t);

    for(c = 1; c <= t; c++){

        printf("Case %d:\n", c);

        int n;

        scanf("%d", &n);

        build(1,n,1);

        while(scanf("%s",d) != EOF)

        {

            if(d[0] == 'E')

                break;

            int x,y;

            scanf("%d%d",&x,&y);

            if(d[0] == 'Q')

            {

                int ans = query(x, y, 1, n, 1);

                printf("%d\n",ans);

            }

            if(d[0] == 'S')

                update(x,-y,1,n,1);

            if(d[0] == 'A')

                update(x, y,1,n,1);

        }

    }

    return 0;

}

 

#include<iostream>

#include<cctype>

#include<cstdlib>

#include<cstring>

#include<cstdio>

using namespace std;



struct seg

{

    int l;

    int r;

    int n;

}T[150011];



void build(int l, int r, int k)

{

    int mid;

    if(l == r){

        T[k].l = l;

        T[k].r = r;

        T[k].n = 0;

        return ;

    }



    mid = (l + r)/2;

    T[k].l = l;

    T[k].r = r;

    T[k].n = 0;

    build(l,mid,2*k);

    build(mid+1,r,2*k+1);

}



void insert(int n,int d,int k)

{

    int mid;

    if(T[k].l == T[k].r && T[k].l == d)

    {

        T[k].n += n;

        return;

    }

    mid = (T[k].l + T[k].r)>>1;

    if(d <= mid) insert(n,d,2*k);

    else insert(n,d,2*k + 1);

    T[k].n = T[2*k].n + T[2*k + 1].n;

}



int ans;

void search(int l, int r, int k)

{

    int mid;

    if(T[k].l == l && T[k].r == r)

    {

        ans += T[k].n;

        return;

    }

    mid = (T[k].l + T[k].r) >> 1;

    if(r <= mid) search(l,r,2*k);

    else if(l > mid) search(l,r,2*k +1);

    else

    {

        search(l,mid,2*k);

        search(mid + 1,r,2*k+1);

    }

}



int main(){

    int Case,TT;

    int n;

    int i;

    int temp;

    char str[11];

    int a,b;

    scanf("%d",&TT);

    for(Case = 1;Case <= TT; Case++)

    {

        scanf("%d",&n);



        build(1,n,1);

        for(i = 1; i <= n; i++)

        {

            scanf("%d",&temp);

            insert(temp,i,1);

        }



        printf("Case %d:\n",Case);



        while(scanf("%s",str),strcmp(str,"End"))

        {

            scanf("%d%d",&a,&b);

            if(strcmp(str,"Add")==0)

                insert(b,a,1);

            else if(strcmp(str,"Sub")==0)

                insert(-b,a,1);

            else{

                ans = 0;

                search(a,b,1);

                printf("%d\n",ans);

            }

        }

    }



    return 0;

}

 

你可能感兴趣的:(HDU)