博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3468 A Simple Problem with Integers
阅读量:4583 次
发布时间:2019-06-09

本文共 2443 字,大约阅读时间需要 8 分钟。

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 31956   Accepted: 9089
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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

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

Source

, Yang Yi
//线段树进阶、成段更新

 

#include <iostream>

#include <stdio.h>
#include <string.h>
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
#define N 100001
using namespace std;
struct node
{
    long long sum,add;
};
node  st[N<<2];
void up(int &k)
{
    st[k].sum=st[k<<1].sum+st[k<<1|1].sum;
}
void down(int &k,int m)//将附加信心传递给孩子
{
    st[k<<1].add+=st[k].add;
    st[k<<1|1].add+=st[k].add;
    st[k<<1].sum+=st[k].add*(m-(m>>1));//这里注意m>>1要加括号、估计是优先级比较低呀
    st[k<<1|1].sum+=st[k].add*(m>>1);
    st[k].add=0;
}
void build(int l,int r,int k)
{
    st[k].add=0;
    if(l==r)
    {
        scanf("%lld",&st[k].sum);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    up(k);
}
long long add;
void updata(int &L,int &R,int l,int r,int k)
{
    if(L<=l&&R>=r)
    {
        st[k].add+=add;
        st[k].sum+=add*(r-l+1);//这里要注意,开始s[k].sum=s[k].add*(r-l+1),当k是根时就会错的.
        return ;
    }
    if(st[k].add)
      down(k,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,lson);
    if(R>m)  updata(L,R,rson);
    up(k);
}
long long query(int &L,int &R,int l,int r,int k)
{
    if(L<=l&&R>=r)
    {
        return st[k].sum;
    }
    if(st[k].add)
        down(k,r-l+1);
    int m=(l+r)>>1;
    long long t1=0,t2=0;
    if(L<=m) t1=query(L,R,lson);
    if(R>m)  t2=query(L,R,rson);
    up(k);
    return t1+t2;
}
int main()
{
    int n,p;
    char c;
    while(scanf("%d%d",&n,&p)!=EOF)
    {
        build(1,n,1);
        int L,R;
        while(p--)
        {
            getchar();
          scanf("%c",&c);
          if(c=='Q')
          {
             scanf("%d%d",&L,&R);
             printf("%lld\n",query(L,R,1,n,1));
          }
          else
          {
             scanf("%d%d%lld",&L,&R,&add);
             updata(L,R,1,n,1);
          }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/372465774y/archive/2012/07/18/2597194.html

你可能感兴趣的文章
博客园主题样式修改教程
查看>>
apache commons-email1.3使用
查看>>
Linux学习 - 压缩解压命令
查看>>
Objective-C和C++的区别
查看>>
C#基础-第6章:类型和成员基础
查看>>
TextView实现多个TextView对象的走马灯效果
查看>>
感悟成功
查看>>
学员管理示例:Ajax删除学生
查看>>
线程组和未处理的异常
查看>>
Oracle管理监控之为11g asm磁盘组添加磁盘
查看>>
javasrcipt中的for in 循环
查看>>
ThetaSome_ThetaAll子查询
查看>>
BZOJ1499 单调队列+DP
查看>>
用鼠标键盘来控制你的Android手机——同屏显示简单教程
查看>>
文件上传
查看>>
js面向对象
查看>>
在CentOS下面编译WizNote Qt Project
查看>>
android list view 实现动态加载
查看>>
NFS工作原理
查看>>
nginx配置用户认证
查看>>