线段树需要的空间。

区间为1-->n

假设是一棵完全二叉树,且树高为i。

完全二叉树性质:第i层最多有2^(i-1)个结点。

那么 2^(i-1) = n;     i = log2(n)  + 1;

共有   2^i - 1 个结点, 即     2^(log2(n) + 1) - 1个结点

即2 * 2^log2(n)  - 1 =   2 * n - 1

但这是建立树是完全二叉树的情况下。

但是如图所示,树可能不是完全二叉树,最下面一层的结点个数>n,   我们以2n来来计算,那么就需要4*n-1的空间。

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

单点更新

 #include <stdio.h>
#include <string.h>
const int N = ;
int a[N*],ans;
void swap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
void build(int rt, int l, int r)
{
if(l==r)
{
scanf("%d",&a[rt]);
return;
}
int mid = (l + r) >> ;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
a[rt] = a[rt<<] + a[rt<<|];
} void query(int rt, int L, int R, int l, int r)
{
if(L==l && R==r)
{
ans += a[rt];
return;
}
int mid = (l + r) >> ;
if(R<=mid)
query(rt<<,L,R,l,mid);
else if(L>mid)
query(rt<<|,L,R,mid+,r);
else
{
query(rt<<,L,mid,l,mid);
query(rt<<|,mid+,R,mid+,r);
}
}
void update(int rt, int val, int pos, int l, int r)
{
if(l == r)
{
a[rt] += val;
return ;
}
int mid = (l + r) >> ;
if(pos <= mid)
update(rt<<,val,pos,l,mid);
else
update(rt<<|,val,pos,mid+,r);
a[rt] = a[rt<<] + a[rt<<|];
}
int main()
{
int t,n,i,j,tCase;
char str[];
scanf("%d",&t);
for(tCase=; tCase<=t; ++tCase)
{
memset(a,,sizeof(a));
scanf("%d",&n);
build(,,n);
printf("Case %d:\n",tCase);
while(true)
{
scanf("%s",str);
if(str[]=='E')
break;
scanf("%d%d",&i,&j);
if(str[]=='Q')
{
ans = ;
if(i > j)
swap(i,j);
query(,i,j,,n);
printf("%d\n",ans);
}
else if(str[]=='A')
{
update(,j,i,,n);
}
else
update(,-j,i,,n);
}
}
return ;
}

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

成段更新, 要用到懒惰标记,每次更改区间时,不会更新到叶子结点,而是标记,等有需要了才去更新

 #include <stdio.h>
#include <string.h>
const int N = + ;
struct node
{
int sum;
int tag;
}a[N*];
void pushUp(int rt)
{
a[rt].sum = a[rt<<].sum + a[rt<<|].sum;
}
void pushDown(int rt, int m)
{
if(a[rt].tag != )
{
a[rt<<].tag = a[rt<<|].tag = a[rt].tag;
a[rt<<].sum = (m-(m>>)) * a[rt].tag;
a[rt<<|].sum = (m>>) * a[rt].tag;
a[rt].tag = ;
}
}
void build(int rt, int l, int r)
{
a[rt].tag = ;
if(l==r)
{
a[rt].sum = ;
return ;
}
int mid = (l + r) >> ;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
pushUp(rt);
}
void update(int rt, int L, int R, int val, int l, int r)
{
if(L<=l && r<=R)
{
a[rt].sum = (r-l+)*val;
a[rt].tag = val;
return;
}
pushDown(rt,r-l+);
int mid = (l + r) >> ;
if(L<=mid) update(rt<<,L,R,val,l,mid);
if(R>mid) update(rt<<|,L,R,val,mid+,r);
pushUp(rt);
} int main()
{
int t,tCase,n,i,x,y,z,q;
scanf("%d",&t);
for(tCase=; tCase<=t; ++tCase)
{
scanf("%d",&n);
build(,,n);
scanf("%d",&q);
for(i=; i<q; ++i)
{
scanf("%d%d%d",&x,&y,&z);
update(,x,y,z,,n);
}
printf("Case %d: The total value of the hook is %d.\n",tCase,a[].sum);
}
return ;
}

线段树(单点更新and成段更新)的更多相关文章

  1. 线段树(多维+双成段更新) UVA 11992 Fast Matrix Operations

    题目传送门 题意:训练指南P207 分析:因为矩阵不超过20行,所以可以建20条线段的线段树,支持两个区间更新以及区间查询. #include <bits/stdc++.h> using ...

  2. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  3. HDU 1754 I Hate It 线段树(单点更新,成段查询)

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题解: 单点更新,成段查询. 代码: #include<iostream> ...

  4. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  5. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. Problem 1007 幸运数 线段树成段更新

    题目链接: 题目 Problem 1007 幸运数 Time Limit: 2000 mSec Memory Limit : 131072 KB 问题描述 皮特的幸运数是2和5.只由幸运数字2和5组成 ...

  7. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  8. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  9. Codeforces295A - Greg and Array(线段树的成段更新)

    题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...

随机推荐

  1. 与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器

    原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 [索引页][源码下载] 与众不同 windows phone (18) - Device ...

  2. (step6.3.2)hdu 1068(Girls and Boys——二分图的最大独立集)

    题目大意:第一行输入一个整数n,表示有n个节点.在接下来的n行中,每行的输入数据的格式是: 1: (2) 4 6 :表示编号为1的人认识2个人,他们分别是4.6: 求,最多能找到多少个人,他们互不认识 ...

  3. 所有CN_消息的说明

    Notification Message Corresponding WindowsConstant Message Description cn_CharToItem wm_CharToItem T ...

  4. mysqladmin在SuSE linux系统中--sleep參数使用不准确问题

    我们都知道,在MySQL中.能够使用mysqladmin命令的extended-status选项来查看MySQL的执行状态,比方获取我们经常关注的几个值: # mysqladmin -uroot -p ...

  5. poj3621 Sightseeing Cows --- 01分数规划

    典型的求最优比例环问题 參考资料: http://blog.csdn.net/hhaile/article/details/8883652 此题中,给出每一个点和每条边的权值,求一个环使 ans=∑点 ...

  6. 杭电acm阶段之理工大版

    想參加全国软件设计大赛C/C++语言组的同学,假设前一篇<C和指针课后练习题总结>没看完的,请先看完而且依照上面的训练做完,然后做以下的训练. 传送门:http://blog.csdn.n ...

  7. UVa-Palindromes

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. 在Activity中为什么要用managedQuery()

    刚開始接触android的时候,每次用数据库都会犹豫使用哪种方式,一种是getContentResolver().query(...),还有一种是managedQuery(...),后来习惯了使用前一 ...

  9. HDU4876ZCC loves cards(多校题)

    ZCC loves cards Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...

  10. Java并发专题 带返回结果的批量任务运行 CompletionService ExecutorService.invokeAll

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/27250059 普通情况下,我们使用Runnable作为主要的任务表示形式,可是R ...