hdu 5057 Argestes and Sequence(BestCoder Round #11)

Argestes and Sequence

                                                         Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                     Total Submission(s): 640    Accepted Submission(s): 163


Problem Description
Argestes has a lot of hobbies and likes solving query problems especially. One day Argestes came up with such a problem. You are given a sequence a consisting of N nonnegative integers, a[1],a[2],...,a[n].Then there are M operation on the sequence.An operation can be one of the following:
S X Y: you should set the value of a[x] to y(in other words perform an assignment a[x]=y).
Q L R D P: among [L, R], L and R are the index of the sequence, how many numbers that the Dth digit of the numbers is P.
Note: The 1st digit of a number is the least significant digit.
 

Input
In the first line there is an integer T , indicates the number of test cases.
For each case, the first line contains two numbers N and M.The second line contains N integers, separated by space: a[1],a[2],...,a[n]—initial value of array elements.
Each of the next M lines begins with a character type.
If type==S,there will be two integers more in the line: X,Y.
If type==Q,there will be four integers more in the line: L R D P.

[Technical Specification]
1<=T<= 50
1<=N, M<=100000
0<=a[i]<=$2^{31}$ - 1
1<=X<=N
0<=Y<=$2^{31}$ - 1
1<=L<=R<=N
1<=D<=10
0<=P<=9
 

Output
For each operation Q, output a line contains the answer.
 

Sample Input
  
  
1 5 7 10 11 12 13 14 Q 1 5 2 1 Q 1 5 1 0 Q 1 5 1 1 Q 1 5 3 0 Q 1 5 3 1 S 1 100 Q 1 5 3 1
 

Sample Output
  
  
5 1 1 5 0 1
 

以为是很简单的树状数组,开了个三维的,直接MLE了,可以离线处理,通过枚举每一位降低空间消耗。

代码:
//1296ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100000+1000;
int a[11][maxn];
int b[maxn];
int c[maxn];
int ans[maxn];
int n,m;
struct node
{
    int kind;
    int l,r,d,p,pos,value;
}op[maxn];
int low(int k)
{
    return k&(-k);
}
void update(int x,int k,int v)
{
    while(k<maxn)
    {
        a[x][k]+=v;
        k+=low(k);
    }
}
int getsum(int x,int k)
{
    int ans=0;
    while(k>0)
    {
        ans+=a[x][k];
        k-=low(k);
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            c[i]=b[i];
        }
        char s[10];
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            if(s[0]=='Q')
            {
                op[i].kind=0;
                scanf("%d%d%d%d",&op[i].l,&op[i].r,&op[i].d,&op[i].p);
            }
            else
            {
                op[i].kind=1;
                scanf("%d%d",&op[i].pos,&op[i].value);
            }
        }
        int po=1;
        for(int i=1;i<=10;i++)//按位枚举
        {
            memset(a,0,sizeof(a));
            for(int j=1;j<=n;j++)
            {
                b[j]=c[j];//每次恢复为原来的值
                int temp=(b[j]/po)%10;
                update(temp,j,1);
            }
            for(int j=0;j<m;j++)
            {
                if(op[j].kind==1)//更改操作
                {
                    int temp=(b[op[j].pos]/po)%10;
                    update(temp,op[j].pos,-1);
                    temp=(op[j].value/po)%10;
                    update(temp,op[j].pos,1);
                    b[op[j].pos]=op[j].value;
                }
                else
                {
                    if(op[j].d==i)//对应位的查询
                    ans[j]=getsum(op[j].p,op[j].r)-getsum(op[j].p,op[j].l-1);
                    else
                    ;
                }
            }
            po=po*10;
        }
        for(int j=0;j<m;j++)
        if(op[j].kind==0)
        {
            printf("%d\n",ans[j]);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值