• ACM POJ 3468 A Simple Problem with Integers(线段树) by kuangbin


    题目链接:http://poj.org/problem?id=3468

    本文作者:kuangbin 

    博客地址:http://www.cnblogs.com/kuangbin/

    题目:

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 22796   Accepted: 6106
    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.

    Source

     
     
    简单的线段树练习题。
    主要是树的节点存储哪些信息。
    每个一个数都更新到叶子节点不是明智的做法,很消耗时间。
    故每个节点加一个Inc来记录增量的累加。
    具体看代码:
    /*
    POJ 3468 A Simple Problem with Integers
    题目意思:
    给定Q个数:A1,A2,```,AQ,以及可能多次进行下列两个操作:
    1)对某个区间Ai```Aj的数都加n(n可变)
    2)对某个区间Ai```Aj求和
    */
    #include
    <stdio.h>
    #include
    <algorithm>
    #include
    <iostream>
    usingnamespace std;
    constint MAXN=100000;
    int num[MAXN];
    struct Node
    {
    int l,r;//区间的左右端点
    longlong nSum;//区间上的和
    longlong Inc;//区间增量的累加
    }segTree[MAXN*3];
    void Build(int i,int l,int r)
    {
    segTree[i].l
    =l;
    segTree[i].r
    =r;
    segTree[i].Inc
    =0;
    if(l==r)
    {
    segTree[i].nSum
    =num[l];
    return;
    }
    int mid=(l+r)>>1;
    Build(i
    <<1,l,mid);
    Build(i
    <<1|1,mid+1,r);
    segTree[i].nSum
    =segTree[i<<1].nSum+segTree[i<<1|1].nSum;
    }
    void Add(int i,int a,int b,longlong c)//在结点i的区间(a,b)上增加c
    {
    if(segTree[i].l==a&&segTree[i].r==b)
    {
    segTree[i].Inc
    +=c;
    return;
    }
    segTree[i].nSum
    +=c*(b-a+1);
    int mid=(segTree[i].l+segTree[i].r)>>1;
    if(b<=mid) Add(i<<1,a,b,c);
    elseif(a>mid) Add(i<<1|1,a,b,c);
    else
    {
    Add(i
    <<1,a,mid,c);
    Add(i
    <<1|1,mid+1,b,c);
    }
    }
    longlong Query(int i,int a,int b)//查询a-b的总和
    {
    if(segTree[i].l==a&&segTree[i].r==b)
    {
    return segTree[i].nSum+(b-a+1)*segTree[i].Inc;
    }
    segTree[i].nSum
    +=(segTree[i].r-segTree[i].l+1)*segTree[i].Inc;
    int mid=(segTree[i].l+segTree[i].r)>>1;
    Add(i
    <<1,segTree[i].l,mid,segTree[i].Inc);
    Add(i
    <<1|1,mid+1,segTree[i].r,segTree[i].Inc);
    segTree[i].Inc
    =0;
    if(b<=mid) return Query(i<<1,a,b);
    elseif(a>mid) return Query(i<<1|1,a,b);
    elsereturn Query(i<<1,a,mid)+Query(i<<1|1,mid+1,b);
    }
    int main()
    {
    int n,q;
    int i;
    int a,b,c;
    char ch;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
    for(i=1;i<=n;i++) scanf("%d",&num[i]);
    Build(
    1,1,n);
    for(i=1;i<=q;i++)
    {
    cin
    >>ch;
    if(ch=='C')
    {
    scanf(
    "%d%d%d",&a,&b,&c);
    Add(
    1,a,b,c);
    }
    else
    {
    scanf(
    "%d%d",&a,&b);
    printf(
    "%I64d\n",Query(1,a,b));
    }
    }
    }
    return0;
    }
  • 相关阅读:
    自动关联
    如何提高测试效率
    检查点
    windows server 2008 安装vs2008 的问题
    【转】xampp mysql 忘记密码的解决方案
    zencart 目录产品显示控制
    静态html文件执行php语句的方法
    UNIX主机访问PHP程序提示“Internal Server Error”的处理办法
    【转】javascript 点击 <a> 链接
    u880刷机
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2138408.html
Copyright © 2020-2023  润新知