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.
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]<=
1<=X<=N
0<=Y<=
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~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
- HDU 5057 Argestes and Sequence (离线树状数组 || 分块)
- hdu 5057 Argestes and Sequence(离线处理树状数组)
- HDU 5057 Argestes and Sequence 树状数组+离线
- HDU 5057 Argestes and Sequence 离线处理+树状数组
- HDU 5057——Argestes and Sequence(树状数组+离线)
- hdu 5057 Argestes and Sequence(树状数组)
- HDU 5057 Argestes and Sequence --树状数组(卡内存)
- hdu 5057 Argestes and Sequence (离散的树状数组)
- HDU 5057 Argestes and Sequence(树状数组)
- hdu 5057 Argestes and Sequence
- Argestes and Sequence HDU
- HDU 5057 Argestes and Sequence 分块
- HDU 5057 Argestes and Sequence(平方分割)
- HDU5057-Argestes and Sequence(分块&&树状数组)
- HDOJ 5057 Argestes and Sequence
- BestCoder Round #11 (Div. 2) Argestes and Sequence (hdu 5057)
- hdu 5057 Argestes and Sequence(BestCoder Round #11)
- 20140929 【 树状数组 】 BestCoder Round #11 (Div. 2) Argestes and Sequence
- css实现未知高度水平垂直居中
- 面试题(四)
- Android Studio获取开发版SHA1值和发布版SHA1值的史上最详细方法
- Spring boot/cloud 基础文档
- 使用application内置对象做一个简单的网页访问计数器
- HDU 5057 Argestes and Sequence 树状数组+离线
- Xlistview上拉刷新,下拉加载
- Activity与Fagment生命周期
- wordcloud2的一个小问题
- promise 的基础应用(待修改)
- 使用eclipse通过weblogic开发简单的ejb应用(weblogic 10.x & ejb3.x)
- Java中清楚hashmap和hashtable,看了以后立刻明白
- Tomcat7.0.73-The valid characters are defined in RFC 7230 and RFC 3986
- android小白