HDU 4267 A Simple Problem with Integers

来源:互联网 发布:兼职淘宝美工工资 编辑:程序博客网 时间:2024/04/20 06:58

 

                      A Simple Problem with Integers

                                              Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
                                                                   Total Submission(s): 2037 Accepted Submission(s): 673


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. The other is to query the value of some element.

Input
There are a lot of test cases.
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)

Output
For each test case, output several lines to answer all query operations.

Sample Input
4 1 1 1 1142 12 22 32 41 2 3 1 22 1 2 22 32 41 1 4 2 12 12 22 32 4

Sample Output
111113312341

Source
2012 ACM/ICPC Asia Regional Changchun Online

Recommend
liuyiding
     
这题刚开始看到的时候,以为是用线段树做,实际上用线段树完全可以解出来,但是没有想法,就想是不是方法错了,后来发现或许最大流可以,但是用最大流肯定会超时,只是建图就超时了,就更别说其他的了。然后实在没有办法的情况下,搜了一下解题报告,看到能用树状数组做,结果自己不会这算法,就重新开始学这算法,学好了后,看解题报告的时候,看到人家的代码和那算法的代码几乎就是一样的,少部分做了修改。可他们的思路我实在是想不通。后来终于找到了一个我能理解的了。 他的update() 函数,和求和函数,里面的循环次序与   树状数组正好倒了过来。举了个例子正好对。神一样的思路啊。
#include <stdio.h>#include <math.h>#include <string.h>int tree[12][12][50010];int a[50010],n;int main(){    void update(int k,int mod,int border,int val);    int sum(int k,int mod,int border);    int i,j,m,s,t,l,r,k,val;    while(scanf("%d",&n)!=EOF)    {        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        memset(tree,0,sizeof(tree));        scanf("%d",&m);        while(m--)        {            scanf("%d",&t);            if(t==1)            {                scanf("%d %d %d %d",&l,&r,&k,&val);                update(k,l%k,r,val);                update(k,l%k,l-1,-val);            }else            {                scanf("%d",&l);                s=a[l];                for(i=1;i<=10;i++)                {                    s+=sum(i,l%i,l);                }                printf("%d\n",s);            }        }    }    return 0;}int lowbit(int x){    return (x&-x);}void update(int k,int mod,int border,int val){    while(border>0)    {        tree[k][mod][border]+=val;        border=border- lowbit(border);    }}int sum(int k,int mod,int border){    int s=0;    while(border<=n)    {        s+=tree[k][mod][border];        border=border + lowbit(border);    }    return s;}

原创粉丝点击