zoukankan      html  css  js  c++  java
  • HDU 4267 A Simple Problem with Integers

    A Simple Problem with Integers

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


    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
     
    题目大意:给予一个数列和两种操作
    操作一 输入left right mod add
        for i from [left] to [right]
          if((i-left)%mod==0) sequence[i]+=add;
    操作二 输入一下标,返回当前下标值
     
    解题思路: 考虑等式  (i-left)%mod = 0  =>   i%mod==left%mod
        然后题目转换 区间修改和单点查询,使用魔改树状数组解决
        
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    #define N 50005
    int data[N][11][11];
    int num[N],n;
    int lowbit(int x){
        return x&(-x);
    }
    
    void update(int mod,int ans,int cur,int add){///所有 mod, ans(left%mod) 属于一家人 把他们放在一起进行update操作
        while (cur>0){
            data[cur][mod][ans]+=add;
            cur-=lowbit(cur);
        }
    }
    
    int query(int x){
        int signs;
        int sum = 0;
        for(int i = 1 ; i <= 10 ; ++i){///查询所有mod的可能情况,得到所有进行过的和
            signs = x;
            while (signs<=n){
                sum+=data[signs][i][x%i];
                signs+=lowbit(signs);
            }
        }
        return sum;
    }
    int main(){
        int q,ans,left,right,mod,add;
        while (cin>>n){
            memset(data,0, sizeof(data));
            for(int i = 1 ; i <= n ; ++i)scanf("%d",num+i);
            cin>>q;
            while (q--){
                scanf("%d",&ans);
                if(ans==1){
                    scanf("%d %d %d %d",&left,&right,&mod,&add);
                    update(mod,left%mod,right,add);
                    update(mod,left%mod,left-1,-add);
                }
                else{
                    scanf("%d",&ans);
                    printf("%d
    ",query(ans)+num[ans]);
                }
            }
        }
    }
     
     
     
  • 相关阅读:
    eclipse常用的快捷键
    如何保留小数点后N位?
    EditText设置/隐藏光标位置、选中文本和获取/清除焦点(转)
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常(转)
    STL优先队列 hdu1896
    工作排序问题 poj2376
    STL存储邻接表
    二叉树的遍历
    快速排序
    并查集 并查集来判断是否存在环路
  • 原文地址:https://www.cnblogs.com/DevilInChina/p/9375234.html
Copyright © 2011-2022 走看看