HDU 5057 argestes and sequence-tree array (card memory)

Source: Internet
Author: User

Question: give n numbers. Two operations each time: 1. Change the number X to Y. 2. query the number of digits where the D-digit is P in the [L, R] range.

Solution: The problem was stuck in the memory at the time. Then I checked the code and found that I could use the unsigned short magic card. So I learned it.

This issue of interval summation is easy to think of tree arrays. The 100 tree arrays are created based on the I-bit J (I <10, j <10) (due to memory 100*100000 card, and the number is displayed, that is, the value of C [10] [10] [100000] is at most 100000, so it can be divided into two unsigned short (0 ~ 65535), record it ). Then the two operations are very common, so we will not proceed.

The time limit is 2500 ms, and the result is 2250 ms, the memory limit is 32768 K, and the result memory is 30008 K, which is just stuck in the past.

It seems that the positive solution is an offline algorithm or a chunk, but it won't be written. It will be written and updated later.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;#define N 100003unsigned 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;}
View code

 

HDU 5057 argestes and sequence-tree array (card memory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.