hdu1166(线段树)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

线段树功能:update:单点增减 query:区间求和

#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 50010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;

int sum[N<<2];
int a[N];
void Pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=a[l];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    Pushup(rt);
}
void update(int pos,int num,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]+=num;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m)update(pos,num,lson);
    else update(pos,num,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 res=0;
    if(L<=m)res+=query(L,R,lson);
    if(m<R)res+=query(L,R,rson);
    return res;
}
int main()
{
    int t,n,cas=1;
    char op[10];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        build(1,n,1);//printf("%d ",sum[20]);
        printf("Case %d:\n",cas++);
        while(scanf("%s",op)>0)
        {
            if(op[0]==‘E‘)break;
            int a,b;
            scanf("%d%d",&a,&b);
            if(op[0]==‘Q‘)
                printf("%d\n",query(a,b,1,n,1));
            else if(op[0]==‘A‘)
                update(a,b,1,n,1);
            else update(a,-b,1,n,1);
        }
    }
}

时间: 2024-07-29 16:27:08

hdu1166(线段树)的相关文章

HDU1166 线段树(最基础题)

1.写法一: 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 5 using namespace std; 6 7 int numv[50005<<2]; 8 int A[50005]; 9 10 void builtTree(int o,int l,int r){ 11 if(l==r) { 12 numv[o]=A[l]; 13 return ; 14 } 15 int

hdu1166 线段树单点修改与区间查询

基本上就是个简单的线段树的单点修改(update)与区间查询(query) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1166 连Lazy标记都不用 附上代码 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=50000+1

HDU1166线段树(单点更新,区间求和)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 78691    Accepted Submission(s): 33255 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营 地,Derek和Tidy的任务

hdu1166 线段树

Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视.中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人

HDU1166 线段树——单点更新

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1166 根据网上大神的简短代码模板自己重新打了一遍,也加深理解 #include<cstdio> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn = 55555; int sum[maxn << 2]; void pushup(int rt) { sum[rt] = sum[rt <

HDU1166 线段树裸题 区间求和

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 113159    Accepted Submission(s): 47413 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

Hdu1166单点更新线段树

入门线段树,单点更新.写了几遍,都是学着notonlysuccess写的. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list>

【线段树】hdu1166敌兵布阵

/* 水水的线段树点修改: ---------------------------------------------------------------- void build(int l,int r,int o)建树 { int mid = (l + r) / 2; a[o].left = l; a[o].right = r; a[o].num = 0; if(a[o].left == a[o].right)到达叶子节点 return ; build(l, mid, lc);向左走 buil

hdu1166敌兵布阵&amp;&amp;hdu1754I Hate It(线段树入门)

单点更新是最最基础的线段树,只更新叶子节点,然后把信息用pushup这个函数更新上来. http://acm.hdu.edu.cn/showproblem.php?pid=1166 update单点更新,query区域求和. #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 200001 using namespace std; s

HDU1166(线段树单点更新区间查询)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 62483    Accepted Submission(s): 26386 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就