HDU 5057:Argestes and Sequence ← 分块算法(单点更新、区间查询)

本文详细介绍了分块算法的应用,通过HDU 5057题目的解答过程,阐述了如何使用分块算法解决单点更新和区间查询的问题。文章讲解了分块算法的基本要素,包括块的大小、数量、边界以及元素位置,并提供了正确代码示例,指出了错误代码导致超时的原因。
摘要由CSDN通过智能技术生成

【题目来源】
http://acm.hdu.edu.cn/showproblem.php?pid=5057

【题目描述】
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.

Argestes有很多爱好,尤其喜欢解决查询问题。
有一天,Argestes 提出了这样一个问题。给定一个由 N 个非负整数 a[1],a[2],…,a[N] 组成的序列。序列上有 M 个操作:
S X Y:将 a[X] 的值设置为 Y,即 a[X]=Y。
Q L R D P:在 [L, R] 中,查询有多少个数的第 D 位是 P。注:数字的第 1 位是最低有效位。

【输入格式】
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.

在第一行有一个整数 T,表示测试用例的数量。
对于每个测试,第一行包含两个数字 N 和 M,第二行包含 N个整数,用空格分开。接下来的 M 行,每行代表一种操作。
S X Y:将 a[X] 的值设置为 Y,即 a[X]=Y。
Q L R D P:在 [L, R] 中,查询有多少个数的第 D 位是 P。

【输出格式】
For each operation Q, output a line contains the answer.
对于每个操作Q,输出一行答案。

【输入样例】
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

【输出样例】
5
1
1
5
0
1

【数据范围】
1<=T<= 50
1<=N, M<=100000
0<=a[i]<=23^1-1 
1<=X<=N
0<=Y<=23^1-1
1<=L<=R<=N
1<=D<=10
0<=P<=9

【算法分析】
● 本题其实就是
线段树单点更新+区间查询”的模板题,这里用来练习分块

● 分块是用线段树的分区思想改良的暴力法。代码比线段树简单。效率比普通暴力法高。分块适合求解 m=n=
10^5 规模的问题。或 m*sqrt(n)≈10^7 的问题。其中,n 为元素个数,m 为操作次数。

● 分块操作的基本要素
(1)块的大小用 block 表示。通常,令
block=sqrt(n)。其中,n 为元素个数。
(2)块的数量用 cnt 表示。计算块的数量的代码如

  • 31
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值