码迷,mamicode.com
首页 > 编程语言 > 详细

HDU 4267 A Simple Problem with Integers(树状数组区间更新)

时间:2016-09-20 19:58:13      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

A Simple Problem with Integers

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


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
/*
类似的区间更新,但这个区间不是实际意义上的区间而是,i到j满足条件的点更新
这个题和区间更新类似,用另一个数组维护,满足条件的点的前缀和,询问的时候直接用原数组的值加上满足条件的值
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#define lowbit(x) x&(-x)
#define N 50010
using namespace std;
int c[12][12][N];//
int n,q,op;
void update(int s1,int s2,int x,int val)//间隔为s1,起点为s2,需要更新的点为x
{
    while(x<=n)
    {
        c[s1][s2][x]+=val;
        x+=lowbit(x);
    }
}
int getsum(int s1,int s2,int x)
{
    int s=0;
    while(x>0)
    {
        s+=c[s1][s2][x];
        x-=lowbit(x);
    }
    return s;
}
int num[N];
int main()
{
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        //cout<<n<<endl;
        memset(c,0,sizeof c);
        for(int i=0;i<n;i++)
            scanf("%d",&num[i]);
        scanf("%d",&q);
        int x,y,k,val;
        while(q--)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d%d%d%d",&x,&y,&k,&val);
                x--;
                y--;
                
                int knum=(y-x)/k;//需要更新的点的个数
                int s1=x%k;//这次更新的起点
                update(k,s1,x/k+1,val);
                update(k,s1,x/k+knum+2,-val);
            }
            else if(op==2)
            {
                scanf("%d",&x);
                x--;
                int cur=num[x];
                for(int i=1;i<=10;i++)//遍历的是k的取值
                    cur+=getsum(i,x%i,x/i+1);
                printf("%d\n",cur);
            }
        }
    }
    return 0;
}

 

HDU 4267 A Simple Problem with Integers(树状数组区间更新)

标签:

原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/5890038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!