zoukankan      html  css  js  c++  java
  • POJ 3468 A Simple Problem with Integers

     A Simple Problem with Integers

    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 53810   Accepted: 16150
    Case Time Limit: 2000MS

    Description

    You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+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

    给一个数字序列A1,A2,A3,…,An,对它进行如下两种操作:

    1、C a b c 对Aa,Aa+1,…,Ab每个数都加上c

    2、Q a b 求Aa,Aa+1,…,Ab的和

    首先想到涉及区间维护的可以用线段树求解,每个结点k中维护如下两个信息:

    1、结点k所对应的区间[l,r)中每个Ai都要加上的数add[k]

    2、结点k中各个数自己的值以及它们各自加上的值的和

    代码如下:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 int n,q,a,b,c;
     8 char w;
     9 long long num[100050],sum[300000],add[300000];
    10 
    11 void Init(int k,int l,int r)
    12 {
    13     if(l>=r-1)
    14     {
    15         sum[k]=num[l];
    16         add[k]=0;
    17         return ;
    18     }
    19 
    20     int mid=(l+r)/2;
    21     Init(k*2+1,l,mid);
    22     Init(k*2+2,mid,r);
    23     sum[k]=sum[k*2+1]+sum[k*2+2];
    24     add[k]=0;
    25 }
    26 
    27 void Add(int x,int a,int b,int k,int l,int r)
    28 {
    29     if(a>=r||b<=l)
    30         return ;
    31     if(a<=l&&b>=r)
    32     {
    33         add[k]+=x;
    34         return ;
    35     }
    36     sum[k]+=(long long)x*(min(b,r)-max(a,l));
    37     Add(x,a,b,k*2+1,l,(l+r)/2);
    38     Add(x,a,b,k*2+2,(l+r)/2,r);
    39 }
    40 
    41 long long Query(int a,int b,int k,int l,int r)
    42 {
    43     if(a>=r||b<=l)
    44         return 0;
    45     if(a<=l&&b>=r)
    46         return add[k]*(r-l)+sum[k];
    47     return Query(a,b,k*2+1,l,(l+r)/2)+Query(a,b,k*2+2,(l+r)/2,r)+(long long)add[k]*(min(b,r)-max(a,l));
    48 }
    49 
    50 
    51 
    52 int main()
    53 {
    54     while(scanf("%d %d",&n,&q)==2)
    55     {
    56         memset(sum,0,sizeof(sum));
    57         memset(add,0,sizeof(add));
    58 
    59         for(int i=1;i<=n;i++)
    60             scanf("%lld",&num[i]);
    61 
    62         Init(0,1,n+1);
    63         for(int i=1;i<=q;i++)
    64         {
    65             getchar();
    66             scanf("%c",&w);
    67             if(w=='C')
    68             {
    69                 scanf("%d %d %d",&a,&b,&c);
    70                 Add(c,a,b+1,0,1,n+1);
    71             }
    72             else if(w=='Q')
    73             {
    74                 scanf("%d %d",&a,&b);
    75                 printf("%lld
    ",Query(a,b+1,0,1,n+1));
    76             }
    77         }
    78     }
    79 
    80     return 0;
    81 }
    [C++]

    之后用数状数组又写了一遍,比线段树要快一些

    还是维护两个树状数组bit1和bit2,当要在区间[l,r]上同时加上x时,我们就执行如下四个操作:

    1、在bit1的l位置加上-x(l-1)

    2、在bit1的r+1位置加上xr

    3、在bit2的l位置加上x

    4、在bit2的r+1位置加上-x

    设sum(bit,i)表示树状数组bit的前i项和,那么A1~Ai的和即为sum(bit1,i)+sum(bit2,i)*i(画个图很容易理解)

    代码如下:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define MAX 100050
     5 
     6 using namespace std;
     7 
     8 int n,q;
     9 long long bit1[MAX],bit2[MAX];
    10 
    11 int lowbit(int x)
    12 {
    13     return x&(-x);
    14 }
    15 
    16 long long sum(long long *c,int x)
    17 {
    18     long long ret=0;
    19 
    20     while(x>0)
    21     {
    22         ret+=c[x];
    23         x-=lowbit(x);
    24     }
    25 
    26     return ret;
    27 }
    28 
    29 void add(long long *c,int x,long long t)
    30 {
    31     while(x<=MAX)
    32     {
    33         c[x]+=t;
    34         x+=lowbit(x);
    35     }
    36 }
    37 
    38 int main()
    39 {
    40     while(scanf("%d %d",&n,&q)==2)
    41     {
    42         char ch;
    43         int a,b,c;
    44         long long temp;
    45         for(int i=1;i<=n;i++)
    46         {
    47             scanf("%lld",&temp);
    48             add(bit1,i,temp);
    49         }
    50         for(int i=0;i<q;i++)
    51         {
    52             getchar();
    53             ch=getchar();
    54             if(ch=='C')
    55             {
    56                 scanf("%d %d %d",&a,&b,&c);
    57                 add(bit1,a,(long long)c*(1-a));
    58                 add(bit1,b+1,(long long)c*b);
    59                 add(bit2,a,c);
    60                 add(bit2,b+1,-c);
    61             }
    62             else if(ch=='Q')
    63             {
    64                 scanf("%d %d",&a,&b);
    65                 printf("%lld
    ",sum(bit1,b)+sum(bit2,b)*b-sum(bit1,a-1)-sum(bit2,a-1)*(a-1));
    66             }
    67         }
    68     }
    69 
    70     return 0;
    71 }
    [C++]

    最近写线段树和数状数组发现,这类题目最关键的地方在于结点中要维护什么样的信息,以及这些信息如何组合出答案

  • 相关阅读:
    .Net Core部署到CentOS
    Docker容器中开始.Net Core之路
    自我总结和学习表单提交的几种方式 (二)
    自我总结和学习表单提交的几种方式 (一)
    Asp.Net MVC下自定义错误页和展示错误页的几种方式
    .Net下发送邮件遇到问题及解决方案
    Asp.Net MVC CodeFirst模式数据库迁移步骤
    利用微软认知服务实现语音识别功能
    Asp.Net MVC路由生成URL过程
    针对于多线程概念的理解
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3583956.html
Copyright © 2011-2022 走看看