HDU 5057 Argestes and Sequence

传送门:点击打开链接

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]<= 231 - 1
1<=X<=N
0<=Y<= 231 - 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

看着这数据量顿时明白要离线做,一阵窃喜(貌似训练赛排名能进前五呦)

想都没想就开了三维树状数组,内存炸了...内心知道应该降维,然而身体与智商都很诚实地说不会

原本我是这么写的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000+5;
int e[N][11][10], a[N];
int n, m;
int lb(int k)
{
    return k&-k;
}
void add(int k, int v, int mm)
{
    while(k <= n)
    {
        e[k][mm][v]++;
        k+=lb(k);
    }
}
int sum(int k, int mm, int nn)
{
    int res = 0;
    while(k>0)
    {
        res += e[k][mm][nn];
        k -= lb(k);
    }
    return res;
}
void ac(int num, int k)
{
    int tmp[10], cnt = 0;
    int numc = num;
    while(num)
    {
        num /= 10;
        cnt++;
    }
    for(int i = 1; i <= cnt; i++)
    {
        int nn = numc % 10;
        tmp[i] = nn;
        numc /= 10;
    }
    for(int i = 1; i <= cnt; i++)
        add(k, tmp[i], i);
    for(int i = cnt+1; i <= 10; i++)
        add(k, 0, i);
}
int main()
{
    int t, X, Y, L, R, D, P;
    char op;
    scanf("%d", &t);
    while(t--)
    {
        memset(e, 0, sizeof(e));
        scanf("%d%d", &n,&m);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            ac(a[i], i);
        }
        for(int i = 1; i <= m; i++)
        {
            cin >> op;
            if(op == 'S')
            {
                scanf("%d%d", &X, &Y);
                ac(Y, X);
            }
            else
            {
                scanf("%d%d%d%d", &L, &R, &D, &P);
                printf("%d\n", sum(R, D, P) - sum(L-1, D,P));
            }
        }
    }
}

个人认为代码的逻辑性是没问题的,只是爆了内存(-_-||)

后来看到大佬的代码后,恍然大悟,我们不需要D(即位数)的维度,而可以存储各个操作的值,然后把位数一样的统一处理,即先初始化树状数组,然后往里面add,求sum等等各种操作一起上即可...大佬不愧是大佬...

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int c[100005][15];
int n;
int a[100005];
int vis[100005];
struct node
{
    int flag,L,R,D,P,x,y,ans;
}p[100005];
int lowbit(int x)
{
    return x&(-x);
}
void update(int i,int num,int x)
{
    while(i<=n)
    {
        c[i][num]+=x;
        i+=lowbit(i);
    }
}
int sum(int num,int i)
{
    int ans=0;
    while(i>0)
    {
        ans+=c[i][num];
        i-=lowbit(i);
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int M;
        scanf("%d%d",&n,&M);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        char op[10];
        for(int i=1;i<=M;i++)
        {
            scanf("%s",op);
            if(op[0]=='S')
            {
                p[i].flag=0;
                scanf("%d%d",&p[i].x,&p[i].y);
            }
            else
            {
                p[i].flag=1;
                scanf("%d%d%d%d",&p[i].L,&p[i].R,&p[i].D,&p[i].P);
            }
        }
        int temp=1;
        for(int i=1;i<=10;i++)
        {
            memset(c,0,sizeof(c));///存储每一位时初始化树状数组
            for(int j=1;j<=n;j++)
            {
                update(j,a[j]/temp%10,1);///将每个数的第i位存储在树状数组中
                vis[j]=a[j]/temp%10;
            }
            for(int j=1;j<=M;j++)       ///在每一位中遍历查询,是否有在此位的进行操作
            {
                if(p[j].flag==1&&p[j].D==i)
                    p[j].ans=sum(p[j].P,p[j].R)-sum(p[j].P,p[j].L-1);
                else if(p[j].flag==0)
                {
                    update(p[j].x,vis[p[j].x],-1);///要注意改变某一个数时,会对其他结果有影响
                    update(p[j].x,p[j].y/temp%10,1);///所以要记录其改变的值,而不能直接改变这个数的值
                    vis[p[j].x]=p[j].y/temp%10;
                }
            }
            temp*=10;
        }
        for(int i=1;i<=M;i++)
            if(p[i].flag==1)
            printf("%d\n",p[i].ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值