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. Junit 测试常见错误

    1. java.lang.Exception: No tests found matching Method deleteUser(mybatis.dao.Usertest) from org.jun ...

  2. iOS自动自动隐藏软键盘

    自动隐藏软键盘,分为两步,一个是单击软键盘外部任意空间:另外一个是单击软键盘上的return键.下面依次实现 单击软键盘外部空间键隐藏软键盘: 一:在viewDidLoad中添加一个UITabGest ...

  3. 什么是core dump?(转)

    什么是Core Dump? 今天调试一个程序, 用到了core dump, 于是写出来, 记于此.什么是Core Dump?Core的意思是内存, Dump的意思是扔出来, 堆出来.开 发和使用Uni ...

  4. 灵动标签内sql语句调用

    本节来介绍帝国cms中,灵动标签中如何写数据库调用我们所要的信息.方便一些没有学习过数据库的朋友 转载请注明出处:谢寒的博客 灵动标签默认的语法 [e:loop={栏目ID/专题ID,显示条数,操作类 ...

  5. Browserify: 使nodejs模块可以在浏览器下使用

    Browserify:浏览器加载Node.js模块--------------------------------------------------随着JavaScript程序逐渐模块化,在ECMA ...

  6. nodejs--express开发个人博客(2)

    上一部分已经实现了视图的雏形,现在加上逻辑操作. 登陆.注册.文章发表都需要用到数据库的数据存取,用的比较多的就是mongodb了. MongoDB 是一个对象数据库,它没有表.行等概念,也没有固定的 ...

  7. 一道月薪3W的java面试题 (小明和小强都是张老师的学生,张老师的生日是某月某日,2人都不知道张老师的生日)

    小明和小强都是张老师的学生,张老师的生日是M月N日,2人都知道张老师的生日 是下列10组中的一天,张老师把M值告诉了小明,把N值告诉了小强,张老师问他们知道他的生日是那一天吗? 3月4日 3月5日 3 ...

  8. 大数据时代之hadoop(五):hadoop 分布式计算框架(MapReduce)

    大数据时代之hadoop(一):hadoop安装 大数据时代之hadoop(二):hadoop脚本解析 大数据时代之hadoop(三):hadoop数据流(生命周期) 大数据时代之hadoop(四): ...

  9. Memory Architecture-SGA-Database Buffer Cache

    启动instance:1.分配内存空间SGA 2.启动后台进程 内存结构:1.SGA 2.PGA 3.UGA 4.Software code areas SGA components:1.Databa ...

  10. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...