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

线段树中对某一点的值进行改变;

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define N 50010
#define Lson r<<1
#define Rson r<<1|1 struct SegmentTree
{
int L, R, k, sum;
int Mid()
{
return (L+R)>>;
}
int len()
{
return R-L+;
}
}a[N<<]; void BuildTree(int r,int L,int R)
{
a[r].L = L; a[r].R = R; a[r].k = ;
if(L == R)
{
scanf("%d",&a[r].sum);
return ;
} BuildTree(Lson, L, a[r].Mid());
BuildTree(Rson, a[r].Mid()+, R); a[r].sum = a[Lson].sum + a[Rson].sum;
}
int Query(int r, int L, int R)
{
if(a[r].R == R && a[r].L == L)
return a[r].sum;
if(L>a[r].Mid())
return Query(Rson, L, R);
else if(R <= a[r].Mid())
return Query(Lson, L, R);
else
{
int sum1=Query(Lson, L, a[r].Mid());
int sum2=Query(Rson, a[r].Mid()+, R);
return sum1 + sum2;
}
}
void Update(int r, int p, int x)
{
if(a[r].L == p && a[r].R == p)
{
a[r].sum += x;
return ;
} if(p<=a[r].Mid())
Update(Lson, p, x);
else
Update(Rson, p, x); a[r].sum = a[Rson].sum + a[Lson].sum;
}
int main()
{
int t=, T, n, L, R, p, x;
char s[];
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
BuildTree(, , n);
printf("Case %d:\n", t++);
while(scanf("%s", s))
{
if(s[] == 'E')break;
else if(s[] == 'Q')
{
scanf("%d %d", &L, &R);
printf("%d\n", Query(, L, R));
}
else if(s[] == 'A')
{
scanf("%d%d", &p,&x);
Update(, p, x);
}
else if(s[] == 'S')
{
scanf("%d %d", &p, &x);
Update(, p, -x);//减去一个数可以看做加一个负数;
}
}
}
return ;
}

线段树

关于树状数组:参考链接

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream> using namespace std; #define N 50500
#define met(a, b) memset(a, b, sizeof(a))
int Tree[N], n; int lowbit(int x)
{
return x&(-x);
} void Update(int pos, int num)
{
while(pos<=n)
{
Tree[pos]+=num;
pos += lowbit(pos);
}
} int GetSum(int pos)
{
int s=;
while(pos)
{
s += Tree[pos];
pos -= lowbit(pos);
}
return s;
}
int main()
{
int T, t=;
scanf("%d", &T);
while(T--)
{
met(Tree, );
int num, pos, x, y;
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d", &num);
Update(i, num);
}
printf("Case %d:\n", t++);
char s[];
while(scanf("%s", s), s[]!='E')
{
if(s[]=='A')
{
scanf("%d %d", &pos, &num);
Update(pos, num);
}
if(s[]=='S')
{
scanf("%d %d", &pos, &num);
Update(pos, -num);
}
if(s[]=='Q')
{
scanf("%d %d", &x, &y);
int ans = GetSum(y)-GetSum(x-);
printf("%d\n", ans);
}
}
}
return ;
}

树状数组

敌兵布阵---hud1166(线段树或者树状数组模板)的更多相关文章

  1. HDU 1611 敌兵布阵 / HRBUST 1794 敌兵布阵(线段树)

    HDU 1611 敌兵布阵 / HRBUST 1794 敌兵布阵(线段树) Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A ...

  2. hdu acm 1166 敌兵布阵 (线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. HDU 1166 敌兵布阵(线段树/树状数组模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. HDUOJ----1166敌兵布阵(线段树单点更新)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. HDU1166 敌兵布阵_线段树

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. HDU1166:敌兵布阵(线段树模板)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. hdu 1166敌兵布阵(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. hdu 1166 敌兵布阵 (线段树单点更新)

    敌兵布阵                                                         Time Limit: 2000/1000 MS (Java/Others)  ...

  10. HUD 1166:敌兵布阵(线段树 or 树状数组)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

随机推荐

  1. 【代码审计】BootCMS v1.1.3 文件上传漏洞分析

      0x00 环境准备 BootCMS官网:http://www.kilofox.net 网站源码版本:BootCMS v1.1.3  发布日期:2016年10月17日 程序源码下载:http://w ...

  2. 【安全开发】Android安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...

  3. Linux CentOS6.5上搭建环境遇到的问题

    1.卸载CentOS自带的JDK 查看centos上 安装的jdk:rpm -qa|grep jdk 出现如下: java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86 ...

  4. Maven编译出现“[ERROR] java.lang.OutOfMemoryError: Java heap space”

    Windows下添加环境变量MAVEN_OPTS的value为-Xms1024m -Xmx1024m -Xss1m Linux下可修改.profile或者.bash_profile文件,并做如下设置: ...

  5. Dictionary的应用

    在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...

  6. mysql的存储过程与事务入门

    存储过程是:通过一系列的SQL语句, 根据传入的参数(也可以没有), 通过简单的调用, 完成比单个SQL语句更复杂的功能, 存储在数据库服务器端,只需要编译过一次之后再次使用都不需要再进行编译.主要对 ...

  7. Matlab练习——寻找完全数

    clc; clear; wq = []; : sum = ; k = ; : i / sum = sum + j; end end == i wq=[wq i]; end end disp(['2至1 ...

  8. 修改计算机名或IP后Oracle10g无法启动服务的解决办法

    修改计算机名或IP后Oracle10g无法启动服务的解决办法 遇到的问题,问题产生原因不详.症状为,windows服务中有一项oracle服务启动不了,报出如下错误. Windows 不能在 本地计算 ...

  9. Delphi应用程序的调试(二)使用断点

    Delphi应用程序的调试(二)使用断点 使用断点(Using Breakpoints) 当用户从Delphi IDE 运行程序时,程序全速运行,只会在设置了断点的地方停住. New Term 断点( ...

  10. Qt编写机房安全作业预警系统

    最近给一个朋友做了个项目,运行在一个日本大型企业中,大屏显示.机房安全作业预警系统工事主要是由监控系统和报警系统组成.其目的就是通过监控系统实时观察空压机房内的动态,通过报警系统确认空压机房在规定的时 ...