poj3468A Simple Problem with Integers 线段树

//两个操作,第一个区间[a , b]内的所有数加c

//第二个询问区间[a , b]的值

//很标准的线段数

#include<cstdio>

#include<cstring>

#include<iostream>

using namespace std ;

const int maxn = 1e5+10 ;

#define left v<<1

#define right v<<1|1

typedef __int64 ll ;

ll h[maxn] ;

struct node

{

int l , r ;

ll lazy ;

ll value;

}tree[maxn<<3] ;

void build(int l , int r , int v)

{

tree[v].l = l ;

tree[v].r = r ;

tree[v].lazy = 0 ;

if(l == r)

{

tree[v].value = h[l] ;

return  ;

}

int mid = (l + r) >> 1 ;

build(l , mid  , left) ;

build(mid+ 1 , r , right) ;

tree[v].value = tree[left].value + tree[right].value ;

}

void push_up(int v)

{

if(tree[v].lazy)

{

tree[left].lazy += tree[v].lazy ;

tree[right].lazy += tree[v].lazy ;

tree[left].value += (tree[left].r - tree[left].l + 1)*tree[v].lazy ;

tree[right].value += (tree[right].r - tree[right].l + 1)*tree[v].lazy ;

tree[v].lazy = 0 ;

}

}

void update(int a , int b , int v , ll num)

{

push_up(v) ;

if(tree[v].l == a && tree[v].r == b)

{

tree[v].lazy += num ;

tree[v].value += (tree[v].r - tree[v].l + 1)*tree[v].lazy ;

return  ;

}

int mid = (tree[v].l  + tree[v].r) >> 1 ;

if(a > mid)update(a , b , right , num) ;

else if(b <= mid)update(a , b , left , num) ;

else

{

update(a , mid , left , num) ;

update(mid + 1 , b , right ,num) ;

}

tree[v].value = tree[left].value + tree[right].value ;

}

ll query(int a , int b ,int v)

{

push_up(v) ;

if(tree[v].l == a && tree[v].r == b)

return tree[v].value ;

int mid = (tree[v].l  + tree[v].r) >> 1 ;

if(a > mid)return query(a , b , right) ;

else if(b <= mid)return query(a , b , left) ;

else return query(a , mid , left) + query(mid+1 , b , right) ;

}

int main()

{

//freopen("in.txt" ,"r" , stdin) ;

int Q ,N ;

while(~scanf("%d%d" , &N ,&Q) )

{

for(int i = 1;i <= N;i++)

scanf("%I64d" ,&h[i]) ;

build(1 , N , 1) ;

char ch[10] ;

int a , b ; ll c ;

while(Q--)

{

scanf("%s" , ch) ;

if(ch[0] == ‘Q‘)

{

scanf("%d%d" ,&a , &b) ;

printf("%I64d\n" , query(a , b , 1)) ;

}

if(ch[0] == ‘C‘)

{

scanf("%d%d%I64d" , &a , &b , &c) ;

update(a , b ,1 ,  c) ;

}

}

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 20:23:32

poj3468A Simple Problem with Integers 线段树的相关文章

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. T

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each

POJ3468__A Simple Problem with Integers (线段树)

本文出自blog.csdn.net/svitter --我大C++的指针岂是尔等能够简单领悟! 题意 给N个节点,标号A1~An,然后有Q个操作,操作分为Q i j,查询i,j间的区间和.C i j k,i到j个数字,每个数字增加k,并且输出. 输入输出分析 给N,Q,然后跟操作.注意判断Q,C使用scanf("%s"). 测试数据: Sample Input 10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 Samp

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of

POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 62431   Accepted: 19141 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

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

A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of