hdu5057 Argestes and Sequence 分块

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

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

Source

BestCoder Round #11 (Div. 2)

之前用线段树一直MLE(在码之前看了下内存限制,就觉得会开不下的了),知道可以用分块做之后,就看了下大白的一道分块题,再想这道题就比较简单了

~num[b][a][b]表示第b块所有的数在第a位为b的个数,这些都可以简单预处理出来,然后只是单点修改,把原来的值和新值对比一下更新一下就好了

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10;
const int SIZE = 330;
int n, m;
int A[N];
int block[N / SIZE + 1][SIZE + 1];
int num[N / SIZE + 1][12][10];
void pre(int b, int j)
{
    int *B = &block[b][0];
    for(int i = 0; i < j; ++i) {
        int tmp = B[i];
        int cnt = 1;
        while(cnt <= 10) {
            int x = tmp % 10;
            tmp /= 10;
            num[b][cnt][x]++;
            cnt++;
        }
    }
}
void init()
{
    memset(num, 0, sizeof num);
    scanf("%d%d", &n, &m);
    int j = 0, b = 0;
    for(int i = 0; i < n; ++i) {
        scanf("%d", &A[i]);
        block[b][j] = A[i];
        if(++j == SIZE) {
            pre(b, j);
            b++; j = 0;
        }
    }
    if(j) { pre(b, j); ++b; }
}
int get(int x, int d) {
    int cnt = 1;
    while(cnt < d) {
        x /= 10;
        cnt++;
    }
    return x % 10;
}
int query(int L, int R, int D, int p)
{
    int res = 0;
    int lb = L / SIZE, rb = R / SIZE;
    if(lb == rb) {
        for(int i = L; i <= R; ++i) {
            if(get(A[i], D) == p) res++;
        }
    }
    else {
        for(int i = L; i < (lb + 1) * SIZE; ++i) if(get(A[i], D) == p) res++;
        for(int i = rb * SIZE; i <= R; ++i) if(get(A[i], D) == p) res++;
        for(int i = lb + 1; i < rb; ++i) res += num[i][D][p];
    }
    return res;
}
void modify(int x, int y)
{
    if(A[x] == y) return;
    int old = A[x], now = y, b = x / SIZE, cnt = 1;
    int c1[12], c2[12];
    A[x] = y;
    while(cnt <= 10) {
        c1[cnt] = old % 10;
        c2[cnt] = now % 10;
        old /= 10;
        now /= 10;
        cnt++;
    }
    for(int i = 1; i <= 10; ++i) {
        if(c1[i] != c2[i]) {
            num[b][i][ c2[i] ]++;
            num[b][i][ c1[i] ]--;
        }
    }
}
int main()
{
    int _; scanf("%d", &_);
    while(_ --)
    {
        init();
        char op[2];
        int L, R, D, P;
        while(m --)
        {
            scanf("%s", op);
            if(op[0] == ‘Q‘) {
                scanf("%d%d%d%d", &L, &R, &D, &P);
                L--; R--;
                printf("%d\n", query(L, R, D, P));
            }else {
                scanf("%d%d", &D, &P);
                D--;
                modify(D, P);
            }
        }
    }
    return 0;
}

  

时间: 2024-10-29 19:07:53

hdu5057 Argestes and Sequence 分块的相关文章

hdu 5057 Argestes and Sequence(分块算法)

Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 566    Accepted Submission(s): 142 Problem Description Argestes has a lot of hobbies and likes solving query problems espec

【分块】hdu5057 Argestes and Sequence

分块,v[i][j][k]表示第i块内第j位是k的元素数.非常好写.注意初始化 要注意题意,①第i位是从右往左算的. ②若x没有第i位,则用前导零补齐10位.比如103---->0000000103. 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 using namespace std; 5 const int MOD[]={1,10,100,1000,10000,100000,1000000

hdu 5057 Argestes and Sequence

Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 511    Accepted Submission(s): 127 Problem Description Argestes has a lot of hobbies and likes solving query problems espec

HDU 5057 Argestes and Sequence --树状数组(卡内存)

题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过,于是学习了. 这种区间求和的问题很容易想到树状数组,根据第i位为j(i<10,j<10)建立100棵树状数组(由于内存100*100000被卡,且看到个数,即c[10][10][100000]的值最多为100000,那么最多分两个unsigned short (0~65535),记录一下就可以了

hdu 5057 Argestes and Sequence (数状数组+离线处理)

题意: 给N个数.a[1]....a[N]. M种操作: S X Y:令a[X]=Y Q L R D P:查询a[L]...a[R]中满足第D位上数字为P的数的个数 数据范围: 1<=T<= 501<=N, M<=1000000<=a[i]<=$2^{31}$ - 11<=X<=N0<=Y<=$2^{31}$ - 11<=L<=R<=N1<=D<=100<=P<=9 思路: 直接开tree[maxn][1

【HDOJ】5057 Argestes and Sequence

树状数组,其实很简单.只是MLE. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 #define MAXN 100005 7 8 short sum[10][10][MAXN]; 9 char bit[10][10][MAXN]; 10 int a[MAXN]; 11 int t, n, m; 12 int d, p; 13 const

HDU5057(分块)

Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1279    Accepted Submission(s): 373 Problem Description Argestes has a lot of hobbies and likes solving query problems espec

BestCoder Round #11 (Div. 2) 题解

HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 302    Accepted Submission(s): 229 Problem Description Bob and Alice got separated in the Square, they agreed that if they

BestCoder Round #11 (Div. 2)题解集合

Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155    Accepted Submission(s): 110 Problem Description Bob and Alice got separated in the Square, they agreed that if they get sepa