Argestes and Sequence
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 273 Accepted Submission(s): 14
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]<= 231 - 1
1<=X<=N
0<=Y<= 231 - 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
官方题解:
1004 Argestes and Sequence 方法一: 可以分块统计,块节点内数据:cnt[d][p]:d位为p的数字个数,num[i]:记录数字。 时间复杂度O(n * sqrt(n)) 方法二: 离线算法:读取全部的操作,将更新与询问操作别分存起来,并且询问操作位数不同的位也分别存起来,即开长度为10的数组,分别存1~10的询问,更新操作统一存起来就好了,操作的先后顺序不要变。 用树状数组维护每一位的区间和。 然后遍历1~10位,对于当前位遍历当前位的询问操作,对于每个询问 在询问之前要把该询问操作之前的更新操作插到树状数组中。
明显一个树状数组,但是 卡内存又卡时间。
如果直接开一个 [10000][10][10] 的 int 数组会爆内存。。。
10000*10*10*4 byte
只能开 10000*10*10*3 byte (就是 short 型 + char 型)
同样,如果每次都使用 memset() 对整个数组初始化也会 超时。
只能根据输入的 n ,决定初始化前 n 个位置。
#include <stdio.h>
#include <string.h>
#define lowbit(x) ((x)&-(x))
int const MAXN = 100000+10;
int const MOD = 32768;
int n, m;
short a[MAXN][10][10];
char jin[MAXN][10][10];
int b[10], len, c[MAXN];
int div(int x){
memset(b, 0, sizeof(b));
for(int i=0; i<=10; i++){
if( x<=0 ) return i;
b[i] = x%10;
x /= 10;
}
}
void updata(int no, int v){
int t;
for(int k=0; k<10; k++){
for(int i=no; i<=n; i+=lowbit(i)){
t = (int)a[i][k][b[k]] + v;
a[i][k][b[k]] = t%MOD;
jin[i][k][b[k]] += t/MOD;
}
}
}
int query(int i, int bit, int v){
int sum = 0;
while( i>0 ){
sum += (int)jin[i][bit][v]*MOD + (int)a[i][bit][v];
i -= lowbit(i);
}
return sum;
}
int main(){
int T, x, y, bit, v;
char op;
scanf("%d\n", &T);
while( T-- ){
while( EOF != scanf("%d %d\n", &n, &m) ){
memset(a, 0, sizeof(short)*(n+1)*100);
memset(jin, 0, sizeof(char)*(n+1)*100);
for(int i=1, x; i<=n; i++){
scanf("%d%*c", &c[i]);
len = div(c[i]);
updata(i, 1);
}
while( m-- ){
scanf("%c %d %d%*c", &op, &x, &y);
if( 'S'==op ){
len = div(c[x]);
updata(x, -1);
len = div(y);
updata(x, 1);
c[x] = y;
}
else{
scanf("%d %d%*c", &bit, &v);
bit--;
int ans = query(y, bit, v) - query(x-1, bit, v);
printf("%d\n", ans);
}
}
}
}
return 0;
}