zoukankan      html  css  js  c++  java
  • HDU 1166 敌兵布阵

    http://acm.hdu.edu.cn/showproblem.php?pid=1166

    线段树,单点增减,区间求和

    View Code
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    const int maxn=51000;
    int sum[maxn<<2];
    int nCase=1;
    int pushup(int rt)
    {
    return 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 m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
    }
    void update(int p,int add,int l,int r,int rt)
    {
    if(l==r)
    {
    sum[rt]+=add;
    return;
    }
    int m=(l+r)>>1;
    if(p<=m)update(p,add,lson);
    else update(p,add,rson);
    pushup(rt);
    }
    int query(int L,int R,int l,int r,int rt)
    {
    if(L<=l&&r<=R){
    return sum[rt];
    }
    int m=(l+r)>>1;
    int ans=0;
    if(L<=m)ans+=query(L,R,lson);
    if(R>m)ans+=query(L,R,rson);
    return ans;
    }
    int main()
    {
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
    scanf("%d",&n);
    build(1,n,1);
    char op[10];
    printf("Case %d:\n",nCase++);
    while(scanf("%s",op))
    {
    if(op[0]=='E')break;
    int a,b;
    scanf("%d%d",&a,&b);
    if(op[0]=='A')update(a,b,1,n,1);
    else if(op[0]=='S')update(a,-b,1,n,1);
    else printf("%d\n",query(a,b,1,n,1));
    }
    }
    return 0;
    }
  • 相关阅读:
    1108递归
    1108JS函数
    1107数组例子
    1106JS循环
    1106JS数组
    1104JS语法
    1104JS循环
    PDO概念 分析 练习
    Jquery网页元素里面的操作以及JSON
    Jquery事件
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/2436416.html
Copyright © 2011-2022 走看看