HDU 4267 A Simple Problem with Integers 第37届ACM/ICPC长春赛区网络赛1001题 (树状数组)

A Simple Problem with Integers

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


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 1 14 2 1 2 2 2 3 2 4 1 2 3 1 2 2 1 2 2 2 3 2 4 1 1 4 2 1 2 1 2 2 2 3 2 4
 

 

Sample Output
1 1 1 1 1 3 3 1 2 3 4 1
 

 

Source
 

 

Recommend
liuyiding
 
 
 
长春网络赛第一题。
一看这题没有什么思路,后来发现k比较小。
马上想到建很多树状数组可以解决。
写好之后交上去就1A了。
做得比较顺手的题目了。
#include<stdio.h>

#include<queue>
#include<iostream>
#include<algorithm>
#include<string.h>
const int MAXN=50020;


int c[12][12][MAXN];
int n;

int lowbit(int x)
{
    return x&(-x);
}

void update(int t1,int t2,int i,int val)
{
    while(i<=n)
    {
        c[t1][t2][i]+=val;
        i+=lowbit(i);
    }

}
int sum(int t1,int t2,int i)
{

    int s=0;
    while(i>0)
    {
        s+=c[t1][t2][i];
        i-=lowbit(i);
    }
    return s;
}
int num[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int m;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)scanf("%d",&num[i]);
        for(int i=0;i<12;i++)
          for(int j=0;j<12;j++)
            for(int k=0;k<MAXN;k++)
              c[i][j][k]=0;
        scanf("%d",&m);
        int a,b,k,q;
        int t;
        while(m--)
        {
            scanf("%d",&t);
            if(t==1)
            {
                scanf("%d%d%d%d",&a,&b,&k,&q);
                a--;
                b--;
                int num=(b-a)/k;
                int s=a%k;
                update(k,s,a/k+1,q);
                update(k,s,a/k+num+2,-q);
            }
            else
            {
                scanf("%d",&a);
                a--;
                int ss=num[a];
                for(int i=1;i<=10;i++)
                {
                    ss+=sum(i,a%i,a/i+1);
                }
                printf("%d\n",ss);
            }
        }
    }
    return 0;
}

 

posted on 2012-09-10 17:15  kuangbin  阅读(1295)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: