Argestes and Sequence
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1435 Accepted Submission(s): 432
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]<= 231 - 1
1<=X<=N
0<=Y<= 231 - 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]<= 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
Source
Recommend
heyang
题意:有n个数,有两种操作,一种是将第x位的数字变为y,还有一种是询问l~r总满足第d位数字为p的个数
解题思路:一开始我是开了三维的树状数组,结果MLE了,网上说可以用时间来换空间,即数字的每一位都不互相影响,可以将所有位分开处理, 每一位都用树状数组维护,就压缩为二维了,这题还可以分块做
树状数组:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
#define LL long long
int x[100005][12],a[100005], b[100005];
int n,q;
struct node
{
int flag, l, r, d, p, x, y, ans;
}p[100005];
int lowbit(int x)
{
return x&(-x);
}
void update(int k, int p, int val)
{
while (k <= n)
{
x[k][p] += val;
k += lowbit(k);
}
}
int getsum(int k, int p)
{
int sum = 0;
while (k>0)
{
sum += x[k][p];
k -= lowbit(k);
}
return sum;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
char ch[10];
for (int i = 1; i <= q; i++)
{
scanf("%s", ch);
if (ch[0] == 'S')
{
p[i].flag = 0;
scanf("%d%d", &p[i].x, &p[i].y);
}
else
{
p[i].flag = 1;
scanf("%d%d%d%d", &p[i].l, &p[i].r, &p[i].d, &p[i].p);
}
}
int temp = 1;
for (int i = 1; i <= 10; i++)
{
memset(x, 0, sizeof x);
for (int j = 1; j <= n; j++)
{
update(j, a[j] / temp % 10, 1);
b[j] = a[j] / temp % 10;
}
for (int j = 1; j <= q; j++)
{
if (p[j].flag == 1 && p[j].d == i)
p[j].ans = getsum(p[j].r, p[j].p) - getsum(p[j].l - 1, p[j].p);
else if (p[j].flag == 0)
{
update(p[j].x, b[p[j].x], -1);
update(p[j].x, p[j].y / temp % 10, 1);
b[p[j].x] = p[j].y / temp % 10;
}
}
temp *= 10;
}
for (int i = 1; i <= q; i++)
if (p[i].flag == 1) printf("%d\n", p[i].ans);
}
return 0;
}
分块:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
struct node
{
int sum[10][10];
}x[400];
int t, n, m,l,r,y,z;
int a[100010], cnt, xx[11];
char ch[10];
void init()
{
xx[1] = 1;
for (int i = 2; i<=10; i++) xx[i] = xx[i - 1] * 10;
}
int query(int l, int r, int d, int p)
{
int L = l / cnt, R = r / cnt;
int ans = 0, k = xx[d];
if (L == R)
{
for (int i = l; i <= r; i++)
if (a[i] / k % 10 == p) ans++;
return ans;
}
for (int i = L + 1; i<R; i++) ans += x[i].sum[d][p];
for (int i = l; i<(L + 1)*cnt; i++)
if (a[i] / k % 10 == p) ans++;
for (int i = R*cnt; i <= r; i++)
if (a[i] / k % 10 == p) ans++;
return ans;
}
int main()
{
init();
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
int tmp = (int)sqrt(1.0*n);
cnt = n / tmp + 1;
memset(x, 0, sizeof x);
for (int i = 0; i<n; i++)
{
scanf("%d", &a[i]);
int id = i / cnt, k = a[i];
for (int j = 1; j<=10; j++)
{
x[id].sum[j][k % 10]++;
k /= 10;
}
}
while (m--)
{
scanf("%s", ch);
if (ch[0] =='S')
{
scanf("%d%d", &y, &z);
y--;
int id = y / cnt;
for (int i = 1; i<=10; i++)
{
x[id].sum[i][a[y] % 10]--;
a[y] /= 10;
}
a[y] = z;
for (int i = 1; i <= 10; i++)
{
x[id].sum[i][z % 10]++;
z /= 10;
}
}
else
{
scanf("%d%d%d%d", &l, &r, &y, &z);
l--, r--;
printf("%d\n", query(l, r, y, z));
}
}
}
return 0;
}