HDOJ 5057 Argestes and Sequence

来源:互联网 发布:博物君的淘宝店 编辑:程序博客网 时间:2024/05/02 04:57

Argestes and Sequence


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


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
15 710 11 12 13 14Q 1 5 2 1Q 1 5 1 0Q 1 5 1 1Q 1 5 3 0Q 1 5 3 1S 1 100Q 1 5 3 1
 
Sample Output
511501

官方题解:
 方法一:
可以分块统计,块节点内数据:cnt[d][p]:d位为p的数字个数,num[i]:记录数字。
时间复杂度O(n * sqrt(n))
方法二:
离线算法:读取全部的操作,将更新与询问操作别分存起来,并且询问操作位数不同的位也分别存起来,即开长度为10的数组,分别存1~10的询问,更新操作统一存起来就好了,操作的先后顺序不要变。
用树状数组维护每一位的区间和。
然后遍历1~10位,对于当前位遍历当前位的询问操作,对于每个询问 在询问之前要把该询问操作之前的更新操作插到树状数组中。

代码:(树状数组卡内存, 开int的超了)
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int MAXN = 100005;int N, M;int a[MAXN][10][10];int b[10], c[MAXN];int lowbit(int x){    return x&(-x);}void div(int c){    for(int i=0;i<10;i++){        b[i] = c%10;        c /= 10;    }}void change(int r, int v){    for(int j=0;j<10;j++){        for(int i=r;i<=N;i+=lowbit(i)){            a[i][j][b[j]] += v;        }    }}int query(int n, int d, int p){    int ans = 0;    while(n>0){        ans += a[n][d][p];        n -= lowbit(n);    }    return ans;}int main(){    int T;    char SQ;    scanf("%d%*c", &T);    while(T--){        memset(a, 0, sizeof(a));        scanf("%d %d%*c", &N, &M);        for(int i=1;i<=N;i++){            scanf("%d%*c", &c[i]);            div(c[i]);            change(i, 1);        }        while(M--){            scanf("%c", &SQ);            if('S' == SQ){                int x, y;                scanf("%d %d%*c", &x, &y);                div(c[x]);                change(x, -1);                div(y);                change(x, 1);                c[x] = y;            }else{                int l, r, d, p;                scanf("%d %d %d %d%*c", &l, &r, &d, &p);                d--;                printf("%d\n", query(r, d, p) - query(l-1, d, p));            }        }    }    return 0;}


优化后(开short数组+char数组记录进位,注意进位值MOD值应为short所能表达的最大值32767+1):
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int MAXN = 100005;const int MOD = 32768;int N, M;short a[MAXN][10][10];char jin[MAXN][10][10];int b[10], c[MAXN];int lowbit(int x){    return x&(-x);}void div(int c){    for(int i=0;i<10;i++){        b[i] = c%10;        c /= 10;    }}void change(int r, int v){    int t;    for(int j=0;j<10;j++){        for(int i=r;i<=N;i+=lowbit(i)){            t = a[i][j][b[j]] + v;            a[i][j][b[j]] = t%MOD;            jin[i][j][b[j]] += t/MOD;        }    }}int query(int n, int d, int p){    int ans = 0;    while(n>0){        ans += jin[n][d][p]*MOD + a[n][d][p];        n -= lowbit(n);    }    return ans;}int main(){    int T;    char SQ;    scanf("%d%*c", &T);    while(T--){        memset(a, 0, sizeof(a));        memset(jin, 0, sizeof(jin));        scanf("%d %d%*c", &N, &M);        for(int i=1;i<=N;i++){            scanf("%d%*c", &c[i]);            div(c[i]);            change(i, 1);        }        while(M--){            scanf("%c", &SQ);            if('S' == SQ){                int x, y;                scanf("%d %d%*c", &x, &y);                div(c[x]);                change(x, -1);                div(y);                change(x, 1);                c[x] = y;            }else{                int l, r, d, p;                scanf("%d %d %d %d%*c", &l, &r, &d, &p);                d--;                printf("%d\n", query(r, d, p) - query(l-1, d, p));            }        }    }    return 0;}

0 0
原创粉丝点击