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
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.
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
可以分塊統計,塊節點內數據:cnt[d][p]:d位爲p的數字個數,num[i]:記錄數字。
時間複雜度O(n * sqrt(n))
方法二:
離線算法:讀取全部的操作,將更新與詢問操作別分存起來,並且詢問操作位數不同的位也分別存起來,即開長度爲10的數組,分別存1~10的詢問,更新操作統一存起來就好了,操作的先後順序不要變。
用樹狀數組維護每一位的區間和。
然後遍歷1~10位,對於當前位遍歷當前位的詢問操作,對於每個詢問 在詢問之前要把該詢問操作之前的更新操作插到樹狀數組中。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 100005;
int N, M;
int a[MAXN][10][10];
int b[10], c[MAXN];
int lowbit(int x){
return x&(-x);
}
void div(int c){
for(int i=0;i<10;i++){
b[i] = c%10;
c /= 10;
}
}
void change(int r, int v){
for(int j=0;j<10;j++){
for(int i=r;i<=N;i+=lowbit(i)){
a[i][j][b[j]] += v;
}
}
}
int query(int n, int d, int p){
int ans = 0;
while(n>0){
ans += a[n][d][p];
n -= lowbit(n);
}
return ans;
}
int main(){
int T;
char SQ;
scanf("%d%*c", &T);
while(T--){
memset(a, 0, sizeof(a));
scanf("%d %d%*c", &N, &M);
for(int i=1;i<=N;i++){
scanf("%d%*c", &c[i]);
div(c[i]);
change(i, 1);
}
while(M--){
scanf("%c", &SQ);
if('S' == SQ){
int x, y;
scanf("%d %d%*c", &x, &y);
div(c[x]);
change(x, -1);
div(y);
change(x, 1);
c[x] = y;
}else{
int l, r, d, p;
scanf("%d %d %d %d%*c", &l, &r, &d, &p);
d--;
printf("%d\n", query(r, d, p) - query(l-1, d, p));
}
}
}
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 100005;
const int MOD = 32768;
int N, M;
short a[MAXN][10][10];
char jin[MAXN][10][10];
int b[10], c[MAXN];
int lowbit(int x){
return x&(-x);
}
void div(int c){
for(int i=0;i<10;i++){
b[i] = c%10;
c /= 10;
}
}
void change(int r, int v){
int t;
for(int j=0;j<10;j++){
for(int i=r;i<=N;i+=lowbit(i)){
t = a[i][j][b[j]] + v;
a[i][j][b[j]] = t%MOD;
jin[i][j][b[j]] += t/MOD;
}
}
}
int query(int n, int d, int p){
int ans = 0;
while(n>0){
ans += jin[n][d][p]*MOD + a[n][d][p];
n -= lowbit(n);
}
return ans;
}
int main(){
int T;
char SQ;
scanf("%d%*c", &T);
while(T--){
memset(a, 0, sizeof(a));
memset(jin, 0, sizeof(jin));
scanf("%d %d%*c", &N, &M);
for(int i=1;i<=N;i++){
scanf("%d%*c", &c[i]);
div(c[i]);
change(i, 1);
}
while(M--){
scanf("%c", &SQ);
if('S' == SQ){
int x, y;
scanf("%d %d%*c", &x, &y);
div(c[x]);
change(x, -1);
div(y);
change(x, 1);
c[x] = y;
}else{
int l, r, d, p;
scanf("%d %d %d %d%*c", &l, &r, &d, &p);
d--;
printf("%d\n", query(r, d, p) - query(l-1, d, p));
}
}
}
return 0;
}