hdu 1166线段树

线段树的一般模板,1.结构体数组tree来存储  2.线段树的构建函数buildTree  3.改变元素值函数update   4.查询区间内总和的函数query
全部使用递归来实现

#####################################################################
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>

using namespace std;

const int maxn = 50050;
int ans;

struct node{
	int left, right, sum;
	int mid(){
		return (left+right)/2;
	}
}tree[maxn*4];

void buildTree(int left, int right, int rt)
{
	tree[rt].left = left;
	tree[rt].right = right;
	if(left == right){
		cin >> tree[rt].sum;
		return ;
	}
	int mid = tree[rt].mid();
	buildTree(left, mid, rt*2);
	buildTree(mid+1, right, rt*2+1);
	tree[rt].sum = tree[rt*2].sum + tree[rt*2+1].sum;
}

void query(int left, int right, int rt, int L, int R)
{
	if(L <= left && R >= right){
		ans += tree[rt].sum;
		return ;
	}
	int mid = tree[rt].mid();
	if(R <= mid)
		query(left, mid, rt*2, L, R);
	else if(L > mid)
		query(mid+1, right, rt*2+1, L, R);
	else
	{
		query(left, mid, rt*2, L, R);
		query(mid+1, right, rt*2+1, L, R);
	}
}

void update(int left, int right, int rt, int pos, int add)
{
	if(left == right)
	{
		tree[rt].sum += add;
		return ;
	}
	int mid = tree[rt].mid();
	if(pos <= mid)
		update(left, mid, rt*2, pos, add);
	else
		update(mid+1, right, rt*2+1, pos, add);
	tree[rt].sum = tree[rt*2].sum + tree[rt*2+1].sum;
}

int main()
{
	int T, N, temp;
	int a, b, Case = 0;
	string str;
	scanf("%d",&T);
	while(T --)
	{
		cin >> N;
		buildTree(1, N, 1);
		Case ++;
		printf("Case %d:\n",Case);
		while( cin >> str )
		{
			if(str == "End") break;
			scanf("%d%d", &a, &b);
			if(str == "Add"){
				update(1, N, 1, a, b);
			}
			else if(str == "Sub"){
				update(1, N, 1, a, -b);
			}
			else {
				ans = 0;
				query(1, N, 1, a, b);
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}

时间: 2024-10-12 19:12:07

hdu 1166线段树的相关文章

HDU(1166),线段树模板,单点更新,区间总和

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 第一次做线段树,帆哥的一句话,我记下来了,其实,线段树就是一种处理数据查询和更新的手段. 然后,我的代码风格,是网上的大牛们的辛苦总结,我就套用了.这里,我还是简单说一下线段树,说的不好,主要方便自己复习. 线段树,3个步骤,建树,查询,更新, 建树:到底部就是a[]数组的值,建立左右子树后,向上推根,根为左右子树的值 更新:类似建树,二分,找到单点所在区间,更新该区间,记得上一个区间也要变化

hdu 1166 线段树(单点增减 区间求和)

Sample Input1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End Sample OutputCase 1:63359 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath>

hdu 1166 线段树单点更新

等线段树复习完再做个总结 1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End Case 1:63359 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue>

hdu 1166 线段树

思路:标准的线段树 代码: #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #include <stdlib.h> #

HDU 1166 线段树模板题

坐了3天的火车,终于到外婆家了(┬_┬).这次北京之旅颇有感触,虽然学到的东西不是很多(主要是自己的原因,没有认真学,并不是老师讲的不好),不过出去见见世面也是好的.最后一场比赛印象颇深,被虐的有点惨....记得当时有道题感觉自己能过,想用数组模拟链表水过,可是无论怎么优化一直超时@[email protected]后面比完后听朋友说那题应该用线段树做,顿时恍然大悟,然并卵,线段树的模板早忘了.....今天做道线段树的模板题先复习一下,过会再想想那道题. 敌兵布阵 Time Limit: 200

hdu 1166 线段树 区间求和 +单点更新 CD模板

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

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

HDU 4902 线段树(区间更新)

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 353    Accepted Submission(s): 169 Problem Description There is an old country and the king fell in love with a devil. The devil alw

hdu 4893 线段树 --- 也是两个变 类似双标记

http://acm.hdu.edu.cn/showproblem.php?pid=4893 开始的时候,我按双标记,WA了一下午,搞不定,我是用的两个标记add--表示当前结点中有值发生变化,flag,斐波那契的懒惰标记,但是估计是我自己处理的有问题,一直不对 参考了别人的代码,写法还是很不错的,Add变量维护的是,完全变成Fibonacci的时候的和,---回头我再重新写一遍 #include <cstdio> #include <cstring> #include <a