您的位置:首页 > 产品设计 > UI/UE

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: