zoukankan      html  css  js  c++  java
  • HDU4267 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): 5991 Accepted Submission(s): 1919

    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
    2012 ACM/ICPC Asia Regional Changchun Online

    题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。

    思路:区间操作,离散化思想去写。
    不来想着用线段树写的,可是会内存超限,看大佬用树状数组写的,开55棵树状数组,用cnt[x][k][mod] 表示x 对k 取余为mod,这样在询问的时候只要循环到10就可以 了。而且(i - a) % k == 0即是 i % k == a % k == mod,这样更新的时候就可以更新(1,b) 和(1,a)了。

    随后用线段树写一下再补上。

    /*
     区间修改树状数组 和离散化
    
    */
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int mm=50010;
    int cnt[mm][11][11];
    int num[mm];
    int lowbit(int x)
    {
        return x&(-x);
    }
    void update(int x,int k,int mod,int add)//对a%K 进行修改存储
    {
        while(x>0)
        {
            cnt[x][k][mod]+=add;
            x-=lowbit(x);
        }
    }
    int sum(int x,int a)
    {
        int s=0;
        while(x<mm)
        {
            for(int i=1;i<=10;i++)//k小于等于10
                s+=cnt[x][i][a%i];
            x+=lowbit(x);
        }
        return s;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
                scanf("%d",&num[i]);
            memset(cnt,0,sizeof(cnt));
            int m;
            scanf("%d",&m);
            while(m--)
            {
                int o,a,b,k,c;
                scanf("%d",&o);
                if(o==1)
                {
                    scanf("%d%d%d%d",&a,&b,&k,&c);
                    update(b,k,a%k,c);
                    update(a-1,k,a%k,-1*c);//由于b 算的是 b到0的  需要修改的树状数组树 a到 b  所以要对a前面的树状数组进行修改,以免影响后面的操作
                }
                else
                {
                    scanf("%d",&a);
                    int ans=sum(a,a);
                    printf("%d
    ",ans+num[a]);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    python初级 0 出发吧
    10 个免费的服务器监控工具推荐
    Nginx 的线程池与性能剖析
    Java中 Comparator接口 与Comparable 的区别
    ORACLE分区表、分区索引详解
    搜索引擎爬虫蜘蛛的USERAGENT大全
    ios和android的发展前景比较
    DES、3DES、AES加密方式
    jsp、freemarker、velocity区别详解
    面向对象五大原则(SRP、OCP、LSP、DIP、ISP)
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900070.html
Copyright © 2011-2022 走看看