Argestes and Sequence

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 511    Accepted Submission(s): 127

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
 
Source
 

题解:

这道题有三种版本号的 题解,本来题目不难,就是限制空间:1.分块算法解决,2.离线树状数组,3.卡空间的树状数组

这里先介绍第一种算法:

学习了一下分块算法,事实上还蛮简单的,就是将n组元素分成m组,每组合并成一块,查询时,仅仅要看元素在那几块,相加即可了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; struct Block
{
int nt[10][10];
}block[400];
int num[100010]; int cal(int d)
{
int ans=1;
for(int i=1;i<=d;i++)
{
ans*=10;
}
return ans;
} int init(int n)
{
int s=(int)sqrt((double)n),t=0;
int m=n/s+1; memset(block,0,sizeof(block));
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
s=i/m;t=num[i];
for(int j=0;j<=9;j++)
{
block[s].nt[j][t%10]++;
t/=10;
}
}
return m;
} void work(int k,int n,int m)
{
char s[2];
int l,r,d,p,tl,tr,td,tp,ans=0;
while(m--)
{
scanf("%s",s);
if(s[0]=='S')
{
scanf("%d%d",&d,&p);
td=d;td/=k;
for(int j=0;j<=9;j++)
{
block[td].nt[j][num[d]%10]--;
num[d]/=10;
}
num[d]=p;tp=p;
for(int j=0;j<=9;j++)
{
block[td].nt[j][tp%10]++;
tp/=10;
}
}
else
{
ans=0;
scanf("%d%d%d%d",&l,&r,&d,&p);
tl=l;tl/=k;tr=r;tr/=k;d--;
td=cal(d);
if(tl==tr)
{ for(int i=l;i<=r;i++)
if(num[i]/td%10==p)
{
ans++;
}
printf("%d\n",ans);
}
else
{
for(int i=tl+1;i<tr;i++)
{
ans+=block[i].nt[d][p];
}
tl=(tl+1)*k;
for(int i=l;i<tl;i++)
if(num[i]/td%10==p)
{
ans++;
}
tr*=k;
for(int i=tr;i<=r;i++)
if(num[i]/td%10==p)
{
ans++;
}
printf("%d\n",ans);
}
//cout<<"??"<<endl;
}
}
}
int main()
{
int cas,m,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
int k=init(n);
work(k,n,m);
}
return 0;
}

以下还写一写离线处理的代码,随后跟上。

hdu 5057 Argestes and Sequence的更多相关文章

  1. hdu 5057 Argestes and Sequence(分块算法)

    Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU 5057 Argestes and Sequence --树状数组(卡内存)

    题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...

  3. 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< ...

  4. 【HDOJ】5057 Argestes and Sequence

    树状数组,其实很简单.只是MLE. #include <iostream> #include <cstdio> #include <cstring> using n ...

  5. HDU 5783 Divide the Sequence(数列划分)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  7. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  8. Hdu 5496 Beauty of Sequence (组合数)

    题目链接: Hdu 5496 Beauty of Sequence 题目描述: 一个整数序列,除去连续的相同数字(保留一个)后,序列的和成为完美序列和.问:一个整数序列的所有子序列的完美序列和? 解题 ...

  9. Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)

    Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...

随机推荐

  1. ftp读取txt数据并插入数据库

    去官网下载http://enterprisedt.com/ .netftp组件 目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll,在项目中添加引用. using Enter ...

  2. vs2010更改默认环境设置

    今天刚刚装vs2010手欠点击了新建团队项目,在百度上各种查找说让我去 visual studio tools的命令提示中进行 devenv命令行修改 ResetString但是没找到我设置文件的路径 ...

  3. 我用过的linux命令--安装JDK

    首先,我的测试环境是CentOS的linux虚拟机,如果想安装JDK,首先要有一个JDK.利用的软件就是WinSCP,把JDK从windows中传送到Linux中去. 1. JDK从Windows到L ...

  4. JS学习之事件冒泡

    (1)什么是事件起泡      首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什 ...

  5. How to configure connectingstrings in Web.config

    先来看一下默认的连接SQL Server数据库配置<connectionStrings>   <add name="LocalSqlServer" connect ...

  6. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  7. hadoop配置及无法移动文件到hdfs故障解析

    首先博主用的64位ubuntu,hadoop官方只提供32位版本,这样的话启动本地库无法兼容,需要自己编译为64位版本,或下载别人编译好的64位版本. 下载好需要在etc/hadoop目录下改动以下几 ...

  8. JS声明语句提升与作用域

    <!DOCTYPE html><html><head></head><body><script>//-------------- ...

  9. (C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开

    原文 (C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一篇:(C#)Windows Shell 编 ...

  10. Mac 让 iTerm2 记住用户名密码 expect 脚本

    刚刚用iTerm2的时候,总是要一遍遍的敲用户名.密码. 我在想, 能不能像Windows的软件一样,可以直接让软件记住.然后只要点击一下,就直接ssh到远程服务器上面去了. 之后经过搜索,可以用ex ...