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

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 10:51   11   0

A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 141093 Accepted: 43762
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 number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

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

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

题意

线段树区间更新求和模板题。

给出n个数和m次操作,Q表示查询该区间内的数的和并输出,C表示把该区间内的所有数都加上一个值。

AC代码

抄的师傅的板子,还不是太理解

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=2e5+10;
const int mod=1e9+7;
using namespace std;
struct wzy
{
 ll left,right,len;
 ll value;
 ll lazy;
}p[maxn<<2];
void push_up(ll o)
{
 p[o].value=p[lson].value+p[rson].value;
}
void push_down(ll o)
{
 if(p[o].lazy)
 {
  p[lson].lazy+=p[o].lazy;
  p[rson].lazy+=p[o].lazy;
  p[lson].value+=p[o].lazy*p[lson].len;
  p[rson].value+=p[o].lazy*p[rson].len;
  p[o].lazy=0;
 }
}
void build(ll o,ll l,ll r)
{
 p[o].left=l;p[o].right=r;
 p[o].len=r-l+1;
 p[o].lazy=0;
 if(l==r)
 {
  ll x;
  scanf("%lld",&x);
  p[o].value=x;
  return ;
 }
 ll mid=(l+r)>>1;
 build(lson,l,mid);
 build(rson,mid+1,r);
 push_up(o);
}
void update(ll o,ll l,ll r,ll v)
{
 if(p[o].left>=l&&p[o].right<=r)
 {
  p[o].lazy+=v;
  p[o].value+=v*p[o].len;
  return ; 
 }
 push_down(o);
 ll mid=(p[o].left+p[o].right)>>1;
 if(r<=mid)
  update(lson,l,r,v);
 else if(l>mid)
  update(rson,l,r,v);
 else
 {
  update(lson,l,mid,v);
  update(rson,mid+1,r,v);
 }
 push_up(o);
}
ll query(ll o,ll l,ll r)
{
 if(p[o].left>=l&&p[o].right<=r)
  return p[o].value;
 ll mid=(p[o].left+p[o].right)>>1;
 push_down(o);
 if(r<=mid)
  return query(lson,l,r);
 else if(l>mid)
  return query(rson,l,r);
 else
  return query(lson,l,mid)+query(rson,mid+1,r);
}
int main(int argc, char const *argv[])
{
 int n,m;
 scanf("%d%d",&n,&m);
 build(1,1,n);
 char ch[5];
 ll a,b,c;
 while(m--)
 {
  scanf("%s",ch);
  if(ch[0]=='Q')
  {
   scanf("%lld%lld",&a,&b);
   printf("%lld\n",query(1,a,b));
  }
  else
  {
   scanf("%lld%lld%lld",&a,&b,&c);
   update(1,a,b,c);
  }
 }
 return 0;
}

转载于:https://www.cnblogs.com/Friends-A/p/10324380.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP