Argestes and Sequence
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 640 Accepted Submission(s): 163
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.
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
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
以为是很简单的树状数组,开了个三维的,直接MLE了,可以离线处理,通过枚举每一位降低空间消耗。
代码:
//1296ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100000+1000;
int a[11][maxn];
int b[maxn];
int c[maxn];
int ans[maxn];
int n,m;
struct node
{
int kind;
int l,r,d,p,pos,value;
}op[maxn];
int low(int k)
{
return k&(-k);
}
void update(int x,int k,int v)
{
while(k<maxn)
{
a[x][k]+=v;
k+=low(k);
}
}
int getsum(int x,int k)
{
int ans=0;
while(k>0)
{
ans+=a[x][k];
k-=low(k);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
c[i]=b[i];
}
char s[10];
for(int i=0;i<m;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
op[i].kind=0;
scanf("%d%d%d%d",&op[i].l,&op[i].r,&op[i].d,&op[i].p);
}
else
{
op[i].kind=1;
scanf("%d%d",&op[i].pos,&op[i].value);
}
}
int po=1;
for(int i=1;i<=10;i++)//按位枚举
{
memset(a,0,sizeof(a));
for(int j=1;j<=n;j++)
{
b[j]=c[j];//每次恢复为原来的值
int temp=(b[j]/po)%10;
update(temp,j,1);
}
for(int j=0;j<m;j++)
{
if(op[j].kind==1)//更改操作
{
int temp=(b[op[j].pos]/po)%10;
update(temp,op[j].pos,-1);
temp=(op[j].value/po)%10;
update(temp,op[j].pos,1);
b[op[j].pos]=op[j].value;
}
else
{
if(op[j].d==i)//对应位的查询
ans[j]=getsum(op[j].p,op[j].r)-getsum(op[j].p,op[j].l-1);
else
;
}
}
po=po*10;
}
for(int j=0;j<m;j++)
if(op[j].kind==0)
{
printf("%d\n",ans[j]);
}
}
return 0;
}