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

HDOJ 4027 Can you answer these queries?(线段树+区间标记)

2016-07-28 09:28 423 查看
Can you answer these queries?
Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our
secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of
the weapon, so he asks you for help. 

You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line. 

Notice that the square root operation should be rounded down to integer.
 

Input

The input contains several test cases, terminated by EOF. 

  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000) 

  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63. 

  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000) 

  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query
of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive. 

 

选取某个区间进去开平方操作,看数的范围是2的63次方,如果用区间更新是不好算和的,每个数最多开方6.7次就变为1了,对1进行开方操作是没有意义的,所以在节点加一个标记,这个标记表示,在节点所负责的区间每个数是否为1或者0,如果flag==0表示全为0,那么区间和就为r-l+1,否者就用sum来计算,在查询和更新时都以flag为区分来更新,当一个数开方为1更新flag,并回溯更新父节点,我觉得相对于区间更新更好理解。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define maxn 100005
#define ll long long
struct node
{
int l;
int r;
ll flag; //标记
ll sum;
}tree[maxn*3];
ll a[maxn];
int n,q;
long long querysum(int id,int ql,int qr)
{
int l=tree[id].l;
int r=tree[id].r;
int mid=(l+r)/2;
if(r<ql||l>qr)
return 0;
if(l==r)
return tree[id].sum;
if(l>=ql&&r<=qr)
{
if(tree[id].flag==0) //如果区间的数都为1
return r-l+1;
else
return querysum(id*2,ql,qr)+querysum(id*2+1,ql,qr);
}
if(mid>=qr) //分区间查询
return querysum(id*2,ql,qr);
else if((mid+1)<=ql)
return querysum(id*2+1,ql,qr);
else
return querysum(id*2,ql,qr)+querysum(id*2+1,ql,qr);
}
void update(int id,int ql,int qr)
{
int l=tree[id].l;
int r=tree[id].r;
int mid=(r+l)/2;
if(tree[id].flag==0)
return;
if(qr<l||ql>r)
return;
if(l==r)
{
tree[id].sum=(ll)sqrt(1.0*tree[id].sum);
if(tree[id].sum<=1)
tree[id].flag=0;
return;
}
if(l>=ql&&qr>=r)
{
if(tree[id].flag==0)
return;
else
{
if(mid>=qr)
update(id*2,ql,qr);
else if((mid+1)<=ql)
update(id*2+1,ql,qr);
else
{
update(id*2,ql,qr);
update(id*2+1,ql,qr);
}

}
tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
tree[id].flag=tree[id*2].flag+tree[id*2+1].flag;
return;
}
if(mid>=qr)
update(id*2,ql,qr);
else if((mid+1)<=ql)
update(id*2+1,ql,qr);
else
{
update(id*2,ql,qr);
update(id*2+1,ql,qr);
}
tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
tree[id].flag=tree[id*2].flag+tree[id*2+1].flag;
}
void build(int id,int l,int r)
{
tree[id].l=l;
tree[id].r=r;
tree[id].flag=r-l+1;
if(l==r)
{
tree[id].sum=a[l];
if(tree[id].sum<=(ll)1)
tree[id].flag=(ll)0;
else
tree[id].flag=(ll)1;
return ;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
tree[id].flag=tree[id*2].flag+tree[id*2+1].flag;
}
int main()
{
int o=0;
while(scanf("%d",&n)!=EOF)
{
o++;
tree[5].flag;
tree[5].sum;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
scanf("%d",&q);
build(1,1,n);
printf("Case #%d:\n",o);
while(q--)
{
int k,ql,qr;
scanf("%d%d%d",&k,&ql,&qr);
if(ql>qr)
swap(ql,qr);
if(k==1)
{
long long ans=querysum(1,ql,qr);
printf("%lld\n",ans);
}
else
update(1,ql,qr);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树