HDU 5057 Argestes and Sequence --树状数组(卡内存)

题意:给n个数字,每次两种操作: 1.修改第x个数字为y。 2.查询[L,R]区间内第D位为P的数有多少个。

解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过,于是学习了。

这种区间求和的问题很容易想到树状数组,根据第i位为j(i<10,j<10)建立100棵树状数组(由于内存100*100000被卡,且看到个数,即c[10][10][100000]的值最多为100000,那么最多分两个unsigned short (0~65535),记录一下就可以了)。 然后两种操作都非常常规,就不讲了。

限时2500ms,结果跑了2250MS,限内存32768K,结果内存30008K,纯粹是卡过去的。

好像正解是离线算法或者分块,但是不会写,以后会写了再更新吧。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100003

unsigned short c[10][10][N];
bool f[10][10][N];
int n,m;
int a[N];
int ten[11] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

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

void modify(int D,int P,int x,int val)
{
    while(x <= n)
    {
        c[D][P][x] += val;
        if(c[D][P][x] > 50000) c[D][P][x] -= 50000, f[D][P][x] = 1;
        x += lowbit(x);
    }
}

int getsum(int D,int P,int x)
{
    int res = 0;
    while(x > 0)
    {
        res += c[D][P][x];
        if(f[D][P][x]) res += 50000;
        x -= lowbit(x);
    }
    return res;
}

int main()
{
    int i,j,t;
    char ss[4];
    int x,y,L,R,D,P;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(c,0,sizeof(c));
        memset(f,0,sizeof(f));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(j=0;j<10;j++)
            {
                int num = a[i]/ten[j];
                num %= 10;
                modify(j,num,i,1);
            }
        }
        while(m--)
        {
            scanf("%s",ss);
            if(ss[0] == ‘Q‘)
            {
                scanf("%d%d%d%d",&L,&R,&D,&P);
                D--;
                printf("%d\n",getsum(D,P,R) - getsum(D,P,L-1));
            }
            else
            {
                scanf("%d%d",&x,&y);
                for(i=0;i<10;i++)
                {
                    int num = a[x]/ten[i];
                    num %= 10;
                    modify(i,num,x,-1);
                }
                for(i=0;i<10;i++)
                {
                    int num = y/ten[i];
                    num %= 10;
                    modify(i,num,x,1);
                }
                a[x] = y;
            }
        }
    }
    return 0;
}

时间: 2024-08-01 07:18:27

HDU 5057 Argestes and Sequence --树状数组(卡内存)的相关文章

hdu 5057 Argestes and Sequence (数状数组+离线处理)

题意: 给N个数.a[1]....a[N]. M种操作: S X Y:令a[X]=Y Q L R D P:查询a[L]...a[R]中满足第D位上数字为P的数的个数 数据范围: 1<=T<= 501<=N, M<=1000000<=a[i]<=$2^{31}$ - 11<=X<=N0<=Y<=$2^{31}$ - 11<=L<=R<=N1<=D<=100<=P<=9 思路: 直接开tree[maxn][1

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

HDU 2852 KiKi&#39;s K-Number (树状数组 &amp;&amp; 二分)

题意:给出对容器的总操作次数n, 接下来是这n个操作.这里对于一个容器提供三种操作, 分别是插入.删除和查找.输入0  e表示插入e.输入1  e表示删除e,若元素不存在输出No Elment!.输入2  e  k表示查找比e大且第k大的数, 若不存在则输出Not Find! 分析:这里考虑树状数组做的原因是在第三个操作的时候, 只要我们记录了元素的总数, 那通过求和操作, 便能够高效地知道到底有多少个数比现在求和的这个数要大, 例如 tot - sum(3)就能知道整个集合里面比3大的数到底有

HDU 3584 Cube (三维 树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3584 Cube Problem Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. Initially we have A[i, j, k] = 0 (1 <= i, 

HDU 5592 ZYB&#39;s Premutation(树状数组+二分)

题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字之外,它是在剩下的数字当中第k+1大的. 知道这个之后,可以用树状数组来帮助找出剩下的数中第k大的数,刚开始我们可以让1-n中每个元素都标记为1,那么他们的前缀和就代表它是第几小.所以,我们可以对于他们的和来二分快速寻找第k大数.其实在树状数组里面是按照第(i-k)小来找的.找完之后要删除这个元素的

hdu 5592 ZYB&#39;s Game 树状数组

ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5592 Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to restore the premutation

HDU 1394 Minimum Inversion Number 树状数组&amp;&amp;线段树

题目给了你一串序列,然后每次 把最后一个数提到最前面来,直到原来的第一个数到了最后一个,每次操作都会产生一个新的序列,这个序列具有一个逆序数的值,问最小的你逆序数的值为多少 逆序数么 最好想到的是树状数组,敲了一把很快,注意把握把最后一个数提上来对逆序数的影响即可, #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #i

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a