FZU 2171 线段树 区间更新求和
很模板的题
在建树的时候输入
求和后更新
#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 线段树 区间更新求和的更多相关文章
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 线段树区间更新&&求和poj3486
给出了一个序列,你需要处理如下两种询问. ≤ c ≤ ). "Q a b" 询问[a, b]区间中所有值的和. Input 第一行包含两个整数N, Q. ≤ N,Q ≤ . 第二行 ...
- HDU 1689 Just a Hook (线段树区间更新+求和)
Just a Hook Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible ...
- 树链剖分(线段树区间更新求和(lazy操作)hdu3966)
题意:给出一颗树形图,有三种操作,I:在u到v的路径上的每个点的权值+d,D:在u到v的路径上的每个点的权值都-d,Q询问u点的权值 #pragma comment(linker, "/ST ...
- POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...
随机推荐
- 设置IIS会话过期时间
打开默认网站----双击ASP--展开会话属性---更改超时时间-
- protostuff简单应用
protobuf是谷歌推出的与语言无关.平台无关的通信协议,一个对象经过protobuf序列化后将变成二进制格式的数据,所以他可读性差,但换来的是占用空间小,速度快.居网友测试,它的序列化效率是xml ...
- hp unix_ssh
http://www.cyberciti.biz/faq/howto-hpux-sshd-service-startup-shutdown/ http://searchnetworking.techt ...
- Android之ViewDragHelper
在自定义ViewGroup中,很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等),针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是 ...
- Snmp配置
http://www.07net01.com/linux/CentOSxiaSNMPfuwuanzhuang_496848_1372907142.html
- CM12同步源码及编译教程
同时提供基于安卓5.0的MKL魔趣猪扒饭编译教程~[玩机组出品]魔趣猪扒饭MKL50.1编译教程http://www.oneplusbbs.com/forum.php?mod=viewthread&a ...
- hdu 3746 kmp求循环节
题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数.
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- C#学习笔记(五)——函数
一.定义和使用函数. 直接通过例子进行说明吧 class Program { static void Write() { Console.WriteLine("Test output fro ...
- OutOfMemoryError异常穷举
本文内容的目的有两个:第一,通过代码验证Java虚拟机规范中描述的各个运行时区域存储的内容:第二,在工作中遇到实际的内存溢出异常时,能根据异常的信息快速判断是哪个区域的内存溢出,知道什么样的代码可能会 ...