hdu 5057 Argestes and Sequence(离线处理树状数组)
2016-07-08 14:48
411 查看
Argestes and Sequence
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 998 Accepted Submission(s): 291
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
.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
—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
题意:给n个数,Q询问区间[l,r]中的数字第D位为P的有多少个,如果一个数字比如79只有两位,那么他的第三位到第十位就是0,可以看做0000000079
S将第X个数改为Y
思路:明显的树状数组,更新,求和操作,可是如果直接用树状数组的话,很明显要开个三维数组,c[10][10][100000]会炸内存。
所以我们考虑离线处理,时间换空间。 把所有的询问操作和更新操作单独存下来,排个序然后在进行操作。 树状数组就不需要多第一维取保存位数了。
(离线写起来好麻烦啊- - ,,,)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100001
int a
,b
,ans
,c[10]
;
int n;
struct Node
{
int l,r,d,v,id;
} p
;
struct P
{
int t,v,last,id;
} q
;
int lowbit(int x)
{
return x&-x;
}
bool cmp(Node a,Node b)
{
if(a.d==b.d) return a.id<b.id;
return a.d<b.d;
}
void add(int num,int m,int v)
{
for(int i=m;i<=n;i+=lowbit(i))
c[num][i]+=v;
}
int sum(int num,int m)
{
int ans=0;
for(int i=m;i;i-=lowbit(i))
ans+=c[num][i];
return ans;
}
void init(int m,int k,int d,int flag)///第m个数字为k,取第d位,flag为1为加,0为减
{
int num=0;
while(k)
{
num++;
int w=k%10;
k/=10;
if(num==d)
if(flag) add(w,m,1);
else add(w,m,-1);
}
if(num<d)
if(flag) add(0,m,1);
else add(0,m,-1);
}
int main()
{
int T,m;
int t,v;
int l,r;
char op[2];
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(c,0,sizeof(c));
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
int cnt=0,cnt1=0;
for(int i=1; i<=m; i++)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d %d %d %d",&l,&r,&t,&v);
p[cnt].l=l,p[cnt].r=r,p[cnt].d=t,p[cnt].v=v,p[cnt++].id=i;
}
else
{
scanf("%d %d",&t,&v);
q[cnt1].t=t,q[cnt1].v=v,q[cnt1].last=a[t],q[cnt1++].id=i;
a[t]=v;
}
}
sort(p,p+cnt,cmp);
int now=0,qq;
memset(ans,-1,sizeof(ans));
for(int i=0; i<cnt; i++)
{
int dd=p[i].d;
if(now!=dd)
{
memset(c,0,sizeof(c));
qq=0;
now=dd;
for(int j=1;j<=n;j++)
init(j,b[j],dd,1);
}
for(; qq<cnt1&&q[qq].id<p[i].id; qq++)
{
init(q[qq].t,q[qq].last,dd,0);
init(q[qq].t,q[qq].v,dd,1);
}
ans[p[i].id]=sum(p[i].v,p[i].r)-sum(p[i].v,p[i].l-1);
}
for(int i=1;i<=m;i++)
if(ans[i]!=-1)
printf("%d\n",ans[i]);
}
return 0;
}
相关文章推荐
- 自定义Collection View布局
- Buileder(生成器)—对象创建型模式
- JAVA后台拼接json map类型,MAP类型中的VALUE对应List
- build.gradle配置参数详解
- 这些天 我踩过的easyui的坑
- iOS6新特征:UICollectionView高级使用示例之CircleLayout
- 【源码】StringBuilder和StringBuffer源码深度剖析
- 审核相关 内购 UUID
- mysql 创建视图时提示View's SELECT contains a subquery in the FROM clause
- CnCrypt 文件属性批量修改工具,通过修改文件属性达到更好的伪装效果
- UIMenuViewController使用
- Easyui excel表格的导出
- 去掉UItableview headerview黏性
- 【打CF,学算法——四星级】CodeForces 689D Friends and Subsequences (RMQ+二分)
- (原创软件)CnCrypt 密码生成工具,提取自CnCrypt的一个独立版本
- BlockingQueue
- 【WWDC2016 Session笔记】 iOS 10 UICollectionView新特性
- Codeforces 689D Friends and Subsequences(二分+RMQ)
- 根据Request获取客户端IP 内网IP及外网IP
- javax.naming.NameNotFoundException: QueueConnectionFactory not bound