敌兵布阵---hud1166(线段树或者树状数组模板)
2024-08-24 19:12:55
题目链接: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 ;
}
树状数组
最新文章
- [Maven]Maven 那点事儿
- 教你搭建SpringMVC框架( 更新中、附源码)
- BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
- Python读取中文txt文件错误:UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character
- vc2005 编译ACE-6.2.0
- 十六款值得关注的NoSQL与NewSQL数据库--转载
- wince下sources\sources.cmn\Makefile.def的相关作用
- Even Tree
- FTP文件操作之创建目录
- ubuntu server 12.04 源
- FusionCharts中图的属性的总结归纳
- MySQL常用的查询命令
- JS-5-循环
- 基于HTML5 Canvas WebGL制作分离摩托车
- Django 日志配置按日期滚动
- js新打开页面
- Nop 4.1版本已经迁移到.net core2.1版本
- Asp.Net Mvc3.0(MEF依赖注入实例)
- Selenium 切换 Frame
- 【Unity】1.3 Unity3D游戏开发学习路线
热门文章
- Yarn执行流程
- sql 替换字符串
- 使用 urllib 发送请求
- C语言中一个字符数组里面的所有元素变成一个字符串
- PowerShell的初步学习
- codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)
- php文件的处理和操作
- WP8.1学习系列(第二十六章)——控件模板
- Mysql错误:Duplicate entry &#39;127&#39; for key &#39;PRIMARY&#39;的解决方法
- 使用 webpack 优化资源