当前位置: 代码迷 >> 综合 >> spoj GSS4 Can you answer these queries IV (线段树)
  详细解决方案

spoj GSS4 Can you answer these queries IV (线段树)

热度:18   发布时间:2023-12-17 03:23:47.0

题意:

思路:

        套路题,因为在根号几次以后,继续根号就没有意义了,利用这一点就能AC了

错误及反思:

代码:

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N = 100100;
long long sum[N<<2],val[N<<2];
int lazy[N<<2];
int n,m;void build(int l,int r,int rt){if(l==r){sum[rt]=val[l];return ;}int m=l+r>>1;build(lson);build(rson);sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}void pushdown(int l,int r,int rt){lazy[rt<<1]+=lazy[rt];lazy[rt<<1|1]+=lazy[rt];lazy[rt]=0;
}void pushup(int l,int r,int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}void update(int L,int R,int l,int r,int rt){if(sum[rt]==r-l+1)  return ;if(L<=l&&R>=r){lazy[rt]++;return ;}int m=l+r>>1;if(L<=m) update(L,R,lson);if(R>m) update(L,R,rson);
}long long query(int L,int R,int l,int r,int rt){if(L<=l&&R>=r)return sum[rt];int m=l+r>>1;long long ans=0;if(L<=m) ans+=query(L,R,lson);if(R>m) ans+=query(L,R,rson);return ans;
}void down(int l,int r,int rt){if(sum[rt]==r-l+1) return ;if(l==r){for(int i=0;i<lazy[rt];i++)sum[rt]=(long long)sqrt(sum[rt]);lazy[rt]=0;return ;}pushdown(l,r,rt);int m=l+r>>1;down(lson);down(rson);pushup(l,r,rt);return ;
}void init(){memset(sum,0,sizeof(sum));memset(lazy,0,sizeof(lazy));
}int main(){int cnt=1;while(scanf("%d",&n)!=EOF){init();for(int i=1;i<=n;i++) scanf("%lld",&val[i]);build(1,n,1);scanf("%d",&m);printf("Case #%d:\n",cnt++);while(m--){int a,b,k;scanf("%d%d%d",&k,&a,&b);if(a>b) swap(a,b);if(k){ down(1,n,1); printf("%lld\n",query(a,b,1,n,1));}else update(a,b,1,n,1);}puts("");}
}
  相关解决方案