很模板的题

在建树的时候输入

求和后更新

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
using namespace std;
///线段树 成段更新
struct node
{
int l;
int r;
int total;
int mark;
};
node a[100050*4];
int c[100050];
int build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].mark=0;
if(l==r)
{
scanf("%d",&c[l]);
return a[rt].total=c[l];
}
int mid=(l+r)>>1;
return a[rt].total=build(rt<<1,l,mid)+build(rt<<1|1,mid+1,r);
}
void update_mark(int rt)
{
if(a[rt].mark!=0)
{
a[rt].total+=(a[rt].r-a[rt].l+1)*a[rt].mark;
if(a[rt].l!=a[rt].r)
{
a[rt<<1].mark+=a[rt].mark;
a[rt<<1|1].mark+=a[rt].mark;
}
a[rt].mark=0;
}
}
int cal(int rt,int l,int r)
{
update_mark(rt);
if(l>a[rt].r||r<a[rt].l)
return 0;
if(l<=a[rt].l&&a[rt].r<=r)
{
return a[rt].total;
}
return cal(rt<<1,l,r)+cal(rt<<1|1,l,r);
}
int update(int rt,int l,int r,int va)
{
update_mark(rt);
if(l>a[rt].r||r<a[rt].l)
return a[rt].total;
if(l<=a[rt].l&&a[rt].r<=r)
{
a[rt].mark+=va;
update_mark(rt);
return a[rt].total;
}
return a[rt].total=update(rt<<1,l,r,va)+update(rt<<1|1,l,r,va);
}
int main(){
int n,m,q;
while(~scanf("%d%d%d",&n,&m,&q))
{
build(1,1,n);
for(int i=0;i<q;i++)
{
int x;
scanf("%d",&x);
int ans=cal(1,x,x+m-1);
printf("%d\n",ans);
update(1,x,x+m-1,-1);
}
}
}

  

FZU 2171 线段树 区间更新求和的更多相关文章

  1. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  2. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. 线段树区间更新&&求和poj3486

    给出了一个序列,你需要处理如下两种询问. ≤ c ≤ ). "Q a b" 询问[a, b]区间中所有值的和. Input 第一行包含两个整数N, Q. ≤ N,Q ≤ . 第二行 ...

  5. HDU 1689 Just a Hook (线段树区间更新+求和)

    Just a Hook Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible ...

  6. 树链剖分(线段树区间更新求和(lazy操作)hdu3966)

    题意:给出一颗树形图,有三种操作,I:在u到v的路径上的每个点的权值+d,D:在u到v的路径上的每个点的权值都-d,Q询问u点的权值 #pragma comment(linker, "/ST ...

  7. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 141093 ...

  8. hdu 1698 线段树 区间更新 区间求和

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

  9. poj3468(线段树区间更新&区间求和模板)

    题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...

随机推荐

  1. SQL 函数

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...

  2. typedef用法

    参考文章:http://www.cnblogs.com/shenlian/archive/2011/05/21/2053149.html

  3. android国外网站

    转载来自 http://www.23apk.com/?p=305 http://www.androidboards.com/ http://www.androidev.com/ http://andr ...

  4. java阿拉伯数字表示的金额转换成中文大写金额

    最大数字要处理到千亿也就是12位整数部分我们可以分成3段处理,xxxx亿,xxxx万,xxxx元,然后小数部分比较好处理我们发现0比较难处理什么时候会出现零呢那就是两个数字之间出现一个或多个零那么数字 ...

  5. Intent的七大属性

    1.Action Action属性代表系统要执行的动作 系统提供如下常用的Action属性 *ACTION_MAIN:应用程序入口点 *ACTION_VIEW:显示指定数据 *ACTION_EDIT: ...

  6. lr参数化——500户并发迭代1次 循环取5条数据

    lr参数化——500户并发迭代1次 循环取5条数据 比如vuser1.vuser2.vuser3..........,vuser500 shuju1,shuju2,shuju3,shuju4,shuj ...

  7. throws 和 throw

    package unit5; public class Person { private int age; private String name; public String getName() { ...

  8. JdbcTemplate主要提供以下五类方法:

    execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句: update方法及batchUpdate方法:update方法用于执行新增.修改.删除等语句:batchUpdate方法用于执 ...

  9. Ajax 学习之创建XMLHttpRequest对象------Ajax的核心

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. js:语言精髓笔记2--表达式

    表达式:由运算符和运算元构成:JS中没有运算符的表达式称为单值表达式:没有运算元,孤立与代码上下文的运算符是不符合语法的:(表达式是有返回值的) 单值表达式: this引用: 变量引用: 直接量: n ...