很模板的题

在建树的时候输入

求和后更新

#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. vector data() [c++11]

    Example 12345678910111213141516171819202122 // vector::data #include <iostream> #include <v ...

  2. 一、HTML和CSS基础--网页布局--网页布局基础

    W3C标准: 由万维网联盟制定的一系列标准,包括: 结构化标准语言(HTML和XML) 表现标准语言(CSS) 行为标准语言(DOM和ECMAScript) 倡导结构.样式.行为分离. CSS 规定的 ...

  3. SQL 查询45题

    表格代码 create table student ( sno ) primary key, sname ) not null, ssex ) not null, sbirthday datetime ...

  4. ARP协议和DNS协议对比

    DNS是在域名和IP之间进行解析,ARP是在IP和MAC之间解析. ARP协议不需要服务,DNS需要开启服务. ARP协议需要互联的主机处于同一个物理网段之内(即局域网环境)

  5. HTML 调用iscroll.js主要事项

    iscroll是一款很不错的滚动控件(js),但是在调用的时候,需要注意一些事项: 1.iscroll支持水平和垂直滚动,那么在调用的时候,一定注意滚动div必须大于父div,也就是说,父div的宽度 ...

  6. strcat函数造成的段错误(Segmentation fault)

    转自:http://book.51cto.com/art/201311/419441.htm 3.21  strcat函数造成的段错误 代码示例 int main() { char dest[7]=& ...

  7. 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符

    1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...

  8. CodeForces 404C Ivan and Powers of Two

    Ivan and Powers of Two Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  9. Encoding

    Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...

  10. A C[HDU1570]

    A C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...