hdu5057 Argestes and Sequence 分块
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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[],a[],...,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[],a[],...,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]
<=T<=
<=N, M<=
<=a[i]<= -
<=X<=N
<=Y<= -
<=L<=R<=N
<=D<=
<=P<= Output
For each operation Q, output a line contains the answer. Sample Input Q
Q
Q
Q
Q
S
Q Sample Output Source
BestCoder Round # (Div. ) 之前用线段树一直MLE(在码之前看了下内存限制,就觉得会开不下的了),知道可以用分块做之后,就看了下大白的一道分块题,再想这道题就比较简单了
~num[b][a][b]表示第b块所有的数在第a位为b的个数,这些都可以简单预处理出来,然后只是单点修改,把原来的值和新值对比一下更新一下就好了
#include <bits/stdc++.h> using namespace std;
const int N = 1e5 + ;
const int SIZE = ;
int n, m;
int A[N];
int block[N / SIZE + ][SIZE + ];
int num[N / SIZE + ][][];
void pre(int b, int j)
{
int *B = &block[b][];
for(int i = ; i < j; ++i) {
int tmp = B[i];
int cnt = ;
while(cnt <= ) {
int x = tmp % ;
tmp /= ;
num[b][cnt][x]++;
cnt++;
}
}
}
void init()
{
memset(num, , sizeof num);
scanf("%d%d", &n, &m);
int j = , b = ;
for(int i = ; i < n; ++i) {
scanf("%d", &A[i]);
block[b][j] = A[i];
if(++j == SIZE) {
pre(b, j);
b++; j = ;
}
}
if(j) { pre(b, j); ++b; }
}
int get(int x, int d) {
int cnt = ;
while(cnt < d) {
x /= ;
cnt++;
}
return x % ;
}
int query(int L, int R, int D, int p)
{
int res = ;
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 + ) * 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 + ; 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 = ;
int c1[], c2[];
A[x] = y;
while(cnt <= ) {
c1[cnt] = old % ;
c2[cnt] = now % ;
old /= ;
now /= ;
cnt++;
}
for(int i = ; i <= ; ++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[];
int L, R, D, P;
while(m --)
{
scanf("%s", op);
if(op[] == '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 ;
}
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/Ot ...
- 【分块】hdu5057 Argestes and Sequence
分块,v[i][j][k]表示第i块内第j位是k的元素数.非常好写.注意初始化 要注意题意,①第i位是从右往左算的. ②若x没有第i位,则用前导零补齐10位.比如103---->00000001 ...
- hdu 5057 Argestes and Sequence
Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 5057 Argestes and Sequence --树状数组(卡内存)
题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...
- hdu_5286_wyh2000 and sequence(分块)
题目链接:hdu_5286_wyh2000 and sequence 题意: 给一段长度为N的序列,每次询问l-r(l和r和上一次询问的答案有关)内 不同的数的 出现次数的次方 的和.强制在线 题解: ...
- HDU - 6395 Sequence (分块+快速矩阵幂)
给定递推式: 求Fn. 分析:给出的公式可以用快速矩阵幂运算得到,但 P/n 整除对于不同的i,值是不同的. 可以根据P将3-n分成若干块,每块中P整除n的值是相同的.分块的时候要注意判断. 将每块的 ...
- [hdu-6395]Sequence 分块+矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时 加的下取整的数随 ...
- 【HDOJ】5057 Argestes and Sequence
树状数组,其实很简单.只是MLE. #include <iostream> #include <cstdio> #include <cstring> using n ...
- Sequence( 分块+矩阵快速幂 )
题目链接 #include<bits/stdc++.h> using namespace std; #define e exp(1) #define pi acos(-1) #define ...
随机推荐
- UVA 156 Ananagrams ---map
题目链接 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列( ...
- 51nod 1605 棋盘问题 (博弈)
题目:传送门. 题意:中文题.T组数据,每组给定一个n*m的棋盘,棋盘中的1代表黑色,0代表白色,每次可以将1或者非2质数的全黑色方形区域变为白色,不能操作者输,问谁能赢. 题解:每次可以将1或者非2 ...
- [聊天框]让DIV的滚动条自动滚动到最底部 - 4种方法
要制作一个在线聊天的程序,在做最后的修饰时,需要对获得的信息即时滚动以保证用户总能看到最新消息. 聊天程序是基于AJAX设计的,没有用框架,消息容器是一个DIV,所以问题就在于如何控制DIV的滚动条. ...
- IOS - delegate为什么不使用retain
循环引用所有的引用计数系统,都存在循环应用的问题.例如下面的引用关系: 对象a创建并引用了对象b.对象b创建并引用了对象c.对象c创建并引用了对象b. 这时候b和c的引用计数分别是2和1.当a不再使用 ...
- IOS - 唯一标识符的获得和更新
苹果公司不可能让其他人获得个人终端的唯一标识符,所以一个终端给另一个终端发送消息,必须经过苹果的APNS(Apple Push Notification Service)....而且苹果为了防止苹果用 ...
- MyBatis之多表关联查询
1使用resultType.ResultMap处理返回结果 处理返回结果 resultType:指定返回值结果的完全限定名,处理多表查询的结果. 多表查询需要定义vo封装查询的结果. 需求:查询部门和 ...
- 简单获取input file 选中的图片,并在一个div的img里面赋值src实现预览图片
html代码: <input id="file_upload" type="file" /> <div class="image_c ...
- 忘记Mysql登录密码
1,使用安全模式跳过验证: 如果 Mysql在运行,Kill掉. 如果mysqld_safe无法启动,可用管理员权限sudo . 2,本地登录: 启动Mysql 3,修改密码: 5.7之后, 更改密码 ...
- android app 内部文件路径
public class MainActivity extends Activity { final String FILE_NAME = "crazyit.bin"; @Over ...
- 2.3顺序容器-deque
deque(双向队列) 1) * :包含deque头文件 ** :deque也是一个可变长数组,适用于vector的操作都适用于deque ***:对比vector的优势在于在头部存取元素可以 ...