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 ...
随机推荐
- September 10th 2016 Week 37th Saturday
An innovation that goes beyond imagination again raised the standard. 颠覆想象的创新,再一次刷新标准. An advertisem ...
- eclipse svn设置忽略文件
- iOS 文件大小转换成 KB、MB、GB 。。。
-(NSString *) convertFileSize:(long long)size { ; ; ; if (size >= gb) { return [NSString stringWi ...
- 昨天在公司加班,上午好像就是弄一个ftp的linux服务问题
在网上找了一些方法,可是其中有通过匿名方式登陆,但是在root的权限下才能存放文件,可是把匿名用户登陆取消之后又不能登陆,就是没有列出怎么来添加一个ftp的用户,今天打算直接装一个linux系统在虚拟 ...
- jquery学习笔记----元素筛选
1.eq() 筛选指定索引号的元素2.first() 筛选出第一个匹配的元素3.last() 筛选出最后一个匹配的元素4.hasClass() 检查匹配的元素是否含有指定的类5.filter() 筛 ...
- 与你相遇好幸运,Tippecanoe在Centos下の安装
全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...
- VS使用技巧(转)
转自http://www.cnblogs.com/xpvincent/p/3596553.html i. Ctrl-M-O 折叠所有方法 ii. Ctrl-M-P 展开所有方法并停止大纲显示(不可以再 ...
- 以 MAMP 为 Mac OS X 安装并设置 PHP开发环境
PHP 页需要通过 Web 服务器处理.因此,要在 Dreamweaver 中使用 PHP 进行开发,您需要访问支持 PHP 的 Web 服务器和 MySQL 数据库.phpMyAdmin 也很实用, ...
- .net学习之CTS、CLS和CLR
CLR:公共语言运行时,就是所有.net语言写的程序的公共运行时环境,比如C#.VB.Net等语言写的程序需要运行在CLR上,然后CLR解析执行操作系统的相关指令,CLR是.net程序运行在操作系统的 ...
- Qt Designer怎样加入资源文件
Qt Designer中如果在设计UI界面的时候要加入一些图素,图标等资源的时候是不能直接添加进去的,需要在Qt开发目录下编写QRC文件 qrc文件格式如下: <RCC> <qres ...