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

 

分块算法:大致就是把一堆(包含n个基本单位)东西分成sqrt(n)块, 每块包含sqrt(n)个基本单位。

如果要修改某个基本单位,只要修改该单位所在的块,因为每块包含sqrt(n)个基本单位,所以时间复杂度为sqrt(n);

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 100010;

struct Block
{
    int ct[10][10];
}block[400];
int _, n, m, a[N], cnt, pp[11];

void build()
{
    int tmp = (int)sqrt(n*1.0), id;
    cnt = n/tmp + 1;

    memset(block, 0, sizeof(block));
    for(int i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
        id = i/cnt, tmp = a[i];
        for(int j=0; j<10; j++)
        {
            block[id].ct[j][tmp%10]++;
            tmp /= 10;
        }
    }
}

void update(int u, int v)
{
    int id = u/cnt;
    for(int i=0; i<10; i++)
    {
        block[id].ct[i][a[u]%10]--;
        a[u] /= 10;
    }
    a[u] = v;
    for(int i=0; i<10; i++)
    {
        block[id].ct[i][v%10]++;
        v /= 10;
    }
}

int query(int l, int r, int d, int p)
{
    int L = l/cnt, R = r/cnt;
    int res = 0, div = pp[d];
    if(L==R)
    {
        for(int i=l; i<=r; i++) if(a[i]/div%10==p) res++;
        return res;
    }

    for(int i=L+1; i<R; i++)
    {
        res += block[i].ct[d][p];
    }

    for(int i=l; i<(L+1)*cnt; i++)
    {
        if(a[i]/div%10==p) res++;
    }

    for(int i=R*cnt; i<=r; i++)
    {
        if(a[i]/div%10==p) res++;
    }
    return res;
}

void solve()
{
    scanf("%d%d", &n, &m);
    build();

    char str[5];
    int u, v, d, p;
    while(m--)
    {
        scanf("%s", str);
        if(str[0]==‘S‘)
        {
            scanf("%d%d", &u, &v);
            u--;
            update(u, v);
        }
        else
        {
            scanf("%d%d%d%d", &u, &v, &d, &p);
            u--, v--, d--;
            printf("%d\n", query(u, v, d, p));
        }
    }
}

void init()
{
    pp[0] = 1;
    for(int i=1; i<10; i++) pp[i] = pp[i-1]*10;
}

int main()
{
//    freopen("in.txt", "r", stdin);
    init();
    cin>>_;
    while(_--) solve();
    return 0;
}

 

时间: 2024-09-29 04:15:21

hdu 5057 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): 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

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

【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

HDU - 1711 A - Number Sequence(kmp

HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

分块算法及模板

此文为博主原创,转载时请通知博主,并把原文链接放在正文醒目位置. 简要介绍 分块算法就是把一串数据分割成几块数据的算法,其实是对暴力的一种优化. 通常在分块时,每块的大小为√n.但最后一块的大小也可能小于√n,只能用暴力来算. 通过把对单个数据的操作转化为对几个块的数据的操作,能够节省时间,提高运算效率. 分块算法在处理大范围的修改.查询问题时有很大优势. 分块算法代码 1 /*此代码主要模仿了钟皓曦大佬的分块算法*/ 2 #include<iostream> 3 #include<cs

HDU 5063 Operation the Sequence(暴力)

HDU 5063 Operation the Sequence 题目链接 把操作存下来,由于只有50个操作,所以每次把操作逆回去运行一遍,就能求出在原来的数列中的位置,输出即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; const ll MOD = 100000