很模板的题

在建树的时候输入

求和后更新

#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. Code(poj 1850)

    大致题意:(与POJ1496基本一致) 输出某个str字符串在字典中的位置,由于字典是从a=1开始的,因此str的位置值就是 在str前面所有字符串的个数 +1 规定输入的字符串必须是升序排列.不降序 ...

  2. 案例(JQuery的ajax无刷新评论)

    CommentsTest.html代码: <head> <meta http-equiv="Content-Type" content="text/ht ...

  3. kvm NET 和 BRIDGE

    net: <interface type='network'> <mac address='52:54:00:e1:ac:43'/> <source network='d ...

  4. linux Xtrabackup安装及使用方法

    [root@centos01 ~]# rpm -Uvh http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x ...

  5. CentOS下源码安装mplayer播放器

    http://www.mplayerhq.hu/MPlayer/releases/ [root@ok MPlayer-1.2.1]# pwd /root/MPlayer-1.2.1 http://ww ...

  6. (二)WebRTC手记之框架与接口

    转自:http://www.cnblogs.com/fangkm/p/4370492.html 转载请注明出处:http://www.cnblogs.com/fangkm/p/4370492.html ...

  7. Html页面插入flash代码

    转自:http://www.educity.cn/jianzhan/402117.html 转自:http://www.cnblogs.com/yxc_fj/articles/1390621.html ...

  8. 建模算法(八)——插值

    插值:求过已知有限个数据点的近似函数 拟合:已知有限个数据点,求近似函数,不要求过已知数据点,只要求在某种意义下在这些点的误差最小 (一)插值方法 一.拉格朗日多项式插值 1.插值多项式 就是做出一个 ...

  9. web异常、并发与安全

  10. loadrunner个版本历程

    1.工具介绍: LoadRunner是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题, LoadRunner能够对整个企业架构进行测试.通过 ...