/*************
该题 要用快速幂 算10的x次方 快速 砍位
********************/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
const long maxn = 1e5+50;
long n;
long num;
long block;
int l[maxn], r[maxn];
int belong[maxn];
long qpow(long x, long y)
{
long base = x;
long ans = 1;
while(y)
{
if(y%2)
{
ans*=base;
}
base*=base;
y/=2;
}
return ans;
}
void build() // 建立 函数
{
block = (long)sqrt(n);// 求 每个块中元素的个数
/* 求块的个数*/
num = n/block;
if(n%block!=0)num++;
/*求每个块的左右端点*/
for(long i = 1; i <= num; i++)
{
l[i] = (i-1)*block+1;
r[i] = i*block;
}
/*最后一个块的右端点*/
r[num] = n;
/*求每个元素所属的块*/
for(long i = 1; i <= n; i++)
{
belong[i] = (i-1)/block+1;
}
}
long arr[maxn], q;
int cnt[600][11][10];// 区间 地几位 0-9 有几个
void input()
{
for(int i = 1; i <= n; i++)
{
scanf("%ld", &arr[i]);
long tmp = arr[i];
int bit2 = 0;
while(tmp)
{
bit2++;
int mod = tmp%10;
cnt[belong[i]][bit2][mod]++;
tmp/=10;
}
bit2++;
for(; bit2<= 10; bit2++)
{
cnt[belong[i]][bit2][0]++;
}
}
}
void update(long x, long y)
{
if(arr[x] == y)return; // 更新 不变 不操作
long tmp = arr[x];
arr[x] = y;
int bit2 = 0;
while(tmp)
{
bit2++;
int mod = tmp%10;
cnt[belong[x]][bit2][mod]--;
tmp/=10;
}
bit2++;
for(; bit2<= 10; bit2++)
{
cnt[belong[x]][bit2][0]--;
}
bit2 = 0;
tmp = y;
while(tmp)
{
bit2++;
int mod = tmp%10;
cnt[belong[x]][bit2][mod]++;
tmp/=10;
}
bit2++;
for(; bit2<= 10; bit2++)
{
cnt[belong[x]][bit2][0]++;
}
}
long query(long x, long y, long bit3, long z)
{
long res = 0;
long tmp;
if(belong[x] == belong[y]) // 属于 一个块 直接暴力
{
for(long i = x; i <= y; i++)
{
tmp = arr[i];
if((tmp/ qpow(10, bit3-1)%10 == z))res++;
}
}
else
{
for(long i = x; i <= r[belong[x]]; i++)
{
tmp = arr[i];
if((tmp/ qpow(10, bit3-1)%10 == z))res++;
}
for(int i = l[belong[y]]; i <= y; i++)
{
tmp = arr[i];
if((tmp/ qpow(10, bit3-1)%10 == z))res++;
}
for(int i = belong[x]+1; i < belong[y]; i++)
{
res+=cnt[i][bit3][z];
}
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%ld %ld", &n, &q);
memset(cnt, 0, sizeof(cnt));
build();
input();
char op[2];
for(int i = 0; i < q; i++)
{
scanf("%s", op);
if(op[0] == 'S')
{
long int x, y;
scanf("%ld %ld", &x, &y);
update(x, y);
}
else
{
long int x, y, bit3, z;
scanf("%ld %ld %ld %ld", &x, &y, &bit3, &z);
long res = query(x, y, bit3, z);
printf("%ld\n", res);
}
}
}
return 0;
}
B - Argestes and Sequence(分块2)————分块
最新推荐文章于 2022-12-05 09:44:19 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)