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 ...
随机推荐
- poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...
- CSS命名法
一.Css命名法: 1.驼峰命名法:除第一个单词的首字母小写之外,其余的单词首字母均大写.如:#headBlock(2). 2.帕斯卡命名法:所有单词的首字母均大写.如:#HeadBlock(3). ...
- xmpp即时通讯的笔记(摘抄)
xmpp的使用: 即时通讯 instant messaging(IM) : -->实时收发信息! 即时通讯相关软件: **QQ,MSN,GoogleTalk,AIM,Jabber(XMPP别名 ...
- Oracle读写分离架构
读写分离是架构分布式系统的一个重要思想.不少系统整体处理能力并不能同业务的增长保持同步,因此势必会带来瓶颈,单纯的升级硬件并不能一劳永逸.针对业务类型特点,需要从架构模式上进行一系列的调整,比如业务模 ...
- C++面向对象基础知识
多态是为了接口重用,封装和继承是为了代码重用 子类重新定义父类虚函数的方法叫做继承,不是重载! 一.基本概念 对于C++中经常出现的函数名称相同但是参数列表或者返回值不同的函数,主要存在三种情况: 1 ...
- iOS - 开发类库
开发类库 UI 项目名称 项目信息 1.MJRefresh 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明. ...
- 【翻译十九】-java之执行器
Executors In all of the previous examples, there's a close connection between the task being done by ...
- 异常:System.BadImageFormatException,未能加载正确的程序集XXX
IDE:VS2015 语言:C# 异常:System.BadImageFormatException,未能加载正确的程序集XXX或其某一依赖项... 一般是由于目标程序的目标平台与其某一依赖项的目标编 ...
- hdu 3032 sg打表找规律 *
有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 打表代码: #include ...
- FreeSWITCH 体系配置结构
转自:http://www.cnblogs.com/logo-fox/archive/2013/12/09/3465440.html FreeSWITCH总体结构: FreeSWITCH 由一个稳定的 ...