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. JavaWeb解释一下什么是 servlet?

    Servlet是一种独立于平台和协议的服务端的java技术,可以生成动态WEB页面与传统的CGI(计算机图形接口)和其他类似的CGI技术相比.Servlet具有更好的可移植性.更强大的功能,更少的投资 ...

  2. BZOJ 1207: [HNOI2004]打鼹鼠( dp )

    dp.. dp[ i ] = max( dp[ j ] + 1 ) ------------------------------------------------------------------ ...

  3. HDU1712-ACboy needs your help

    描述: ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profi ...

  4. <转> Python的优雅技巧

    枚举 不要这么做: 全选复制放进笔记 i = 0 for item in iterable: print i, item i += 1 而是这样: 全选复制放进笔记 for i, item in en ...

  5. 【android开发】小说阅读器

    新人开发理念 1 activity 是每个功能页面的入口 2 动画效果需要配合资源文件中,动画的定义 3 文件的读取是有权限控制的 4 布局应该尽量简单,这样才能让程序跑的飞快 前记 重新开始一个新的 ...

  6. access数据库 top语句失效解决方法

    使用查询语句  select   top 1  *  from  News  order  by  [PublicTime] desc   就不一定管用了,如果News表里面的PublicTime字段 ...

  7. Android studio如何使用SVN进行版本控制?

    通过这两天对Android Studio的研究,终于搞通了Android Studio的基本操作及与SVN的相关关联操作(这样才能在公司的开发工作中使用):Google年底将会停止ADT插件的更新和支 ...

  8. pomelo

    简介 Pomelo 是基于 Node.js 的高性能.分布式游戏服务器框架.它包括基础的开发框架和相关的扩展组件(库和工具包),可以帮助你省去游戏开发枯燥中的重复劳动和底层逻辑的开发.Pomelo 不 ...

  9. linux进程间通信之信号

    1.wait()函数 原型:pid_t  wait(int *status) 子进程退出时,它向父进程发送一个SIGCHLD信号,默认情况是总是忽略SIGCHLD信号,此时进程状态一直保留在内存中,因 ...

  10. html 5 废弃的标签和属性

    第一类:表现性元素 basefont big center font s strike tt u 建议用语义正确的元素代替他们,并使用CSS来确保渲染后的效果 第二类:框架类元素 因框架有很多可用性及 ...