HDU 5057 Argestes and Sequence 树状数组+离线

来源:互联网 发布:虚拟社交网络语言规则 编辑:程序博客网 时间:2024/05/22 17:47

Argestes and Sequence

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1466    Accepted Submission(s): 445


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
15 710 11 12 13 14Q 1 5 2 1Q 1 5 1 0Q 1 5 1 1Q 1 5 3 0Q 1 5 3 1S 1 100Q 1 5 3 1
 

Sample Output
511501
 

题意:给一些数字,两种操作:1、修改某个数字 2、询问[l, r]之间的数字的第d位数为p有几个。

解析:这是一个修改+询问的题目,开始是用线段树做的,超内存了,题目给的数据范围有点大。参考网上大神的做法,采用离线化处理,用时间换取空间,减少一维。就是先将这些操作存下来,每个数字一共1~10位,把以前对全部数字的操作改为遍历每一位,对每一位进行修改和查询操作。

代码:

#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;#define N 100002#define lowbit(x) (x)&(-(x))struct no{int l, r, d, p, pos, value;bool flag;}A[N];int a[N], b[N],c[N][10], ans[N];int n, m;int Sum(int k, int p){int ans = 0;while(k){ans += c[k][p];k -= lowbit(k);}return ans;}int Pow(int base, int k){int ans = 1;while(k-- > 0) ans *= base;return ans;}void update(int flag, int k,int p){while(k<=n){c[k][p] += flag;k+=lowbit(k);}}int query(int l, int r, int p){return Sum(r, p) - Sum(l-1, p);}int main(){int t;char str[2];scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++){scanf("%d", &a[i]);}for(int i = 0; i < m; i++){scanf("%s", str);if(str[0] == 'Q'){scanf("%d%d%d%d", &A[i].l, &A[i].r, &A[i].d, &A[i].p);A[i].flag = false;}else if(str[0] == 'S'){scanf("%d%d", &A[i].pos, &A[i].value);A[i].flag = true;}}for(int i = 0; i < 10; i++){memset(c, 0, sizeof(c));for(int j = 1; j <= n; j++){int x = a[j] / Pow(10, i) % 10;b[j] = x;update(1, j, x);}for(int j = 0; j < m; j++){if(A[j].flag){int x = A[j].value / Pow(10, i) % 10;if(x == b[A[j].pos])continue;update(-1, A[j].pos, b[A[j].pos]);update(1, A[j].pos, x);b[A[j].pos] = x;}else{if(A[j].d == i+1){ans[j] = query(A[j].l, A[j].r, A[j].p);}}}}for(int i = 0; i < m; i++)if(!A[i].flag)printf("%d\n", ans[i]);}return 0;}

阅读全文
0 0