Can you answer these queries?-HDU4027 区间开方
Posted ljxdtc666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Can you answer these queries?-HDU4027 区间开方相关的知识,希望对你有一定的参考价值。
题意:
给你n个数,两个操作,0为区间开方,1为区间求和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027
思路:
如果当该区间的数都为1,我们没必要进行开方操作,因为1开方还是1,否则找到每个叶子节点,进行开方操作
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> using namespace std; const int MAXN=1e5+5; const int INF=0x7fffffff; typedef long long ll; ll lazy[MAXN<<2],tree[MAXN<<2]; void push_up(int node) { tree[node]=tree[node<<1]+tree[node<<1|1]; } void build(int node,int l,int r) { if(l==r) { scanf("%lld",&tree[node]); return ; } int mid=(l+r)>>1; build(node<<1,l,mid); build(node<<1|1,mid+1,r); push_up(node); } void update(int node,int l,int r,int x,int y) { //区间的长度等于区间内所有数之和则说明所有数都为1 if(x<=l&&y>=r&&(r-l+1==tree[node])) { return; } if(l==r) { tree[node]=(ll)sqrt(tree[node]);return; } int mid=(l+r)>>1; if(x<=mid) update(node<<1,l,mid,x,y); if(y>mid) update(node<<1|1,mid+1,r,x,y); push_up(node); } ll query(int node,int l,int r,int x,int y) { if(x<=l&&y>=r) { return tree[node]; } ll ans=0; int mid=(l+r)>>1; if(x<=mid)ans+=query(node<<1,l,mid,x,y); if(y>mid)ans+=query(node<<1|1,mid+1,r,x,y); return ans; } int main() { int n; int case_=0; while(scanf("%d",&n)!=EOF) { printf("Case #%d: ",++case_); build(1,1,n); int k;scanf("%d",&k); for(int i=1;i<=k;i++) { int op;scanf("%d",&op); int x,y; if(!op) { scanf("%d%d",&x,&y);update(1,1,n,min(x,y),max(x,y)); } else { scanf("%d%d",&x,&y); printf("%lld ",query(1,1,n,min(x,y),max(x,y))); } }printf(" "); } return 0; }
以上是关于Can you answer these queries?-HDU4027 区间开方的主要内容,如果未能解决你的问题,请参考以下文章
HDU4027 Can you answer these queries? —— 线段树 区间修改
HDU-Can you answer these queries? (线段树+区间修改)
HDU4027 Can you answer these queries?
HDU 1027 G - Can you answer these queries?