这道题目如果不剪枝的话会超时,众所周知,1的平方根还是1,所以当某个区间下的值全为1时,就不需要再开方了,(一个极大的数,比如,2^63=9223372036854775808,6次开方之后就是1.776034517001307,取整之后就是1,最多7次update之后这个区间的值就不需要在更新了)这时就需要一个记录区间长度的lenth变量(就是记录这个区间有多少个数)同时题目要求求和也需要定义一个sum变量,若lenth==sum,则表示这个区间的数字个数等于这些数字的和,即这个区间的数的大小都为1(不存在负数)
题解:
点击打开链接http://www.acmerblog.com/hdu-4027-can-you-answer-these-queries-7030.html
1060ms 8.7MB
而我在看完题解后改的代码时间和内存却是 811ms 11.7MB(搞不懂为什么时间内存会差这么多,可能真的跟线段树的写法有关,或者就是long long和__int64的区别)
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.
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.
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.
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Case #1: 19 7 6
#include<stdio.h>
#include<math.h>
#include<string.h>
#define maxn 100005
struct list
{
int left;
int right;
long long sum;
int weight;
int len;
}tree[4*maxn];
long long ans,an[maxn];
void build(int i,int left,int right);
void update(int i,int ql,int qr,int left,int right);
void query(int i,int ql,int qr,int left,int right);
void pushup(int i);
void pushdown(int i,int left,int right);
int main()
{
int n,cnt=1;
while(scanf("%d",&n)!=EOF)
{
memset(tree,0,sizeof(tree));
memset(an,0,sizeof(an));
printf("Case #%d:\n",cnt++);
for(int i=1;i<=n;i++)
{
scanf("%lld",&an[i]);
}
build(1,1,n);
int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int t,a,b,c;
scanf("%d%d%d",&t,&a,&b);
if(a>b)
{
c=a;
a=b;
b=c;
}
if(t==0)
{
update(1,a,b,1,n);
}
else if(t==1)
{
ans=0;
query(1,a,b,1,n);
printf("%lld\n",ans);
}
}
putchar('\n');
}
return 0;
}
void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
tree[i].sum=0;
tree[i].weight=0;
tree[i].len=right-left+1;
if(left==right)
{
tree[i].sum=an[left];
return ;
}
int mid=(left+right)>>1;
build(i<<1,left,mid);
build(i<<1|1,mid+1,right);
pushup(i);
}
void update(int i,int ql,int qr,int left,int right)
{
if(ql<=left&&qr>=right)
{
if(tree[i].sum!=tree[i].len)
pushdown(i,ql,qr);
return ;
}
int mid=(left+right)>>1;
if(ql<=mid)
update(i<<1,ql,qr,left,mid);
if(qr>=mid+1)
update(i<<1|1,ql,qr,mid+1,right);
pushup(i);
}
void query(int i,int ql,int qr,int left,int right)
{
if(ql<=left&&qr>=right)
{
ans+=tree[i].sum;
return ;
}
int mid=(left+right)>>1;
if(ql<=mid)
query(i<<1,ql,qr,left,mid);
if(qr>=mid+1)
query(i<<1|1,ql,qr,mid+1,right);
}
void pushup(int i)
{
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
void pushdown(int i,int left,int right)
{
if(tree[i].left==tree[i].right)
{
tree[i].sum=sqrt(tree[i].sum);
return ;
}
int mid=(left+right)>>1;
pushdown(i<<1,left,mid);
pushdown(i<<1|1,mid+1,right);
pushup(i);
}