其他分享
首页 > 其他分享> > [HDU3333]Turing Tree

[HDU3333]Turing Tree

作者:互联网

莫队模板题...

不过树状数组也可以做...跟HH的项链几乎一模一样,离线询问,然后记录前缀,更新的时候把前缀删掉就好了,然而这题开long long,卡空间

 

 1 //hgs AK IOI,IMO,ICHO,IPHO
 2 #include<bits/stdc++.h>
 3 #define int long long
 4 #define writeln(x)  write(x),puts("")
 5 #define writep(x)   write(x),putchar(' ')
 6 using namespace std;
 7 inline int read(){
 8     int ans=0,f=1;char chr=getchar();
 9     while(!isdigit(chr)){if(chr=='-') f=-1;chr=getchar();}
10     while(isdigit(chr)){ans=(ans<<3)+(ans<<1)+chr-48;chr=getchar();}
11     return ans*f;
12 }void write(int x){
13     if(x<0) putchar('-'),x=-x;
14     if(x>9) write(x/10);
15     putchar(x%10+'0');
16 }const int M = 3e5+5;
17 int n,m,a[M],pos[M],pre[M],b[M],len,Ans[M];
18 struct Que{int l,r,id;}q[M];
19 bool cmp(const Que&x,const Que&y){return x.r<y.r;}
20 int s[M];
21 #define lowbit(x) (x&-x)
22 inline void Update(int x,int y){if(x==0)return;for(;x<=n;x+=lowbit(x))s[x]+=y;}
23 inline int  Query (int x){int ans=0;for(;x;x-=lowbit(x))ans+=s[x];return ans;}
24 signed main(){
25     int Time=read();
26     while(Time--){memset(pre,0,sizeof(pre)),memset(pos,0,sizeof(pos));
27         n=read();for(int i=1;i<=n;++i)b[i]=a[i]=read();
28         sort(b+1,b+n+1),len=unique(b+1,b+n+1)-b-1;memset(s,0,sizeof(s));
29         for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+len+1,a[i])-b,pre[i]=pos[a[i]],pos[a[i]]=i;
30         m=read();for(int i=1;i<=m;++i)q[i].l=read(),q[i].r=read(),q[i].id=i;sort(q+1,q+m+1,cmp);
31         for(int i=1,j=1;i<=m;i++){
32             for(;j<=q[i].r;j++)Update(pre[j],-b[a[pre[j]]]),Update(j,b[a[j]]);
33             Ans[q[i].id]=Query(q[i].r)-Query(q[i].l-1);
34         }for(int i=1;i<=m;i++)printf("%lld\n",Ans[i]);
35     }
36     return 0;
37 }

 

标签:const,int,Turing,Tree,long,chr,Que,HDU3333,define
来源: https://www.cnblogs.com/zhenglw/p/11780995.html