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;
}
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/Ot ...
- HDU 5057 Argestes and Sequence --树状数组(卡内存)
题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...
- 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< ...
- hdu5057 Argestes and Sequence 分块
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Submiss ...
- 【HDOJ】5057 Argestes and Sequence
树状数组,其实很简单.只是MLE. #include <iostream> #include <cstdio> #include <cstring> using n ...
- 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 ...
- HDU 5783 Divide the Sequence(数列划分)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence
// 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...
- 基于视觉信息的网页分块算法(VIPS) - yysdsyl的专栏 - 博客频道 - CSDN.NET
基于视觉信息的网页分块算法(VIPS) - yysdsyl的专栏 - 博客频道 - CSDN.NET 于视觉信息的网页分块算法(VIPS) 2012-07-29 15:22 1233人阅读 评论(1) ...
随机推荐
- Node.JS文件系统解析
1.Node.js 文件系统 var fs = require("fs") 2.异步和同步 读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFile ...
- PDF.NET SOD 开源框架红包派送活动 && 新手快速入门指引
一.框架的由来 快速入门 有关框架的更多信息,请看框架官方主页! 本套框架的思想是借鉴Java平台的Hibernate 和 iBatis 而来,兼有ORM和SQL-MAP的特性,同时还参考了后来.N ...
- MaterialUp - 寻找材料设计灵感必备的网站
MaterialUp 是一个展示最好的材料设计 APP,网站和概念的地方,每天都会更新.每款设计都精心挑选,展示那些有才华的设计师的伟大工作. MaterialUp 的核心价值是让尽可能多的设计师提供 ...
- 对比MS Test与NUnit Test框架
前言: 项目中进行Unit Test时,肯定会用到框架,因为这样能够更快捷.方便的进行测试. .Net环境下的测试框架非常多,在这里只是对MS Test和NUnit Test进行一下比较, 因为这两个 ...
- 对于Access数据库查询遇到空值的解决办法
1.Access数据库在office环境下对于null是识别的,但是,在开发环境下,Access数据库对于where xxx is null是不识别的. 2.查询空值解决办法:select * fro ...
- 频率直方图(hist)
频率直方图(frequency histogram)亦称频率分布直方图.统计学中表示频率分布的图形.在直角坐标系中,用横轴表示随机变量的取值,横轴上的每个小区间对应一个组的组距,作为小矩形的底边:纵轴 ...
- linux heartbeat v2/v3 的一点资料
http://linux-ha.org http://linux-ha.org/wiki/Pacemaker Heartbeat2 http://blog.taggesell.de/index.php ...
- Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论
Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论 创建用户自定义的类加载器 要创建用户自定义的类加载器,只需要扩展java.lang.ClassLoader类,然后覆盖它的f ...
- Java虚拟机JVM学习03 连接过程:验证、准备、解析
Java虚拟机JVM学习03 连接过程:验证.准备.解析 类被加载后,就进入连接阶段. 连接就是将已经读入到内存的类的二进制数据合并到虚拟机的运行时环境中去. 连接阶段三个步骤:验证.准备和解析. 类 ...
- 百度地图SDK 遇到的问题及解决方案
目前项目工作中用到了百度地图sdk,遇到了不少问题,在此记录一下,顺便吐槽下希望百度能把这地图sdk做的更好用一点. 1,开发环境, Xcode6.0 (6A313) + 百度地图 iOS SDK v ...