HDOJ 5057 Argestes and Sequence

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
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

官方題解:
 方法一:
可以分塊統計,塊節點內數據: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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章