Can you answer these queries?(区间开方,区间求和)

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.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
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
Sample Output
Case #1:
19
7
6
一个很大的数进行数次若干开平方就会变成1。因为结点的value最大是263,而264也就只需开7次就可以得到1,而1开方永远是1,所以如果更新区间[x,y]的话,那么那些值为1的相应区间根本就不需要任何更新操作。而判断某区间的值是否全为1的方法也很简单,即node.right– node.left + 1 == node.value,通过这个剪枝操作就可以在限定时间之内求解了。

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string> 
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
struct node{
	int l,r;
	ll sum;
}tree[400010];
void build(int i,int l,int r)//建树 
{
 tree[i].l=l;tree[i].r=r;
 if(l==r)
 {
 	cin>>tree[i].sum;
 	return ;
 }
 int mid=(l+r)/2;
 build(i*2,l,mid);//左子树
 build(i*2+1,mid+1,r);//右子树
 tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;
}
ll search(int i,int l,int r)
{
 	if(tree[i].l>=l&&tree[i].r<=r)
    return tree[i].sum;
    ll s=0;
	if(tree[i*2].r>=l) s+=search(i*2,l,r);
	if(tree[i*2+1].l<=r) s+=search(i*2+1,l,r);
	return s;
}
void Sqrt(int i,int l,int r)
{
 if(tree[i].r<l||tree[i].l>r||tree[i].sum==(tree[i].r-tree[i].l+1))//重要剪枝 
  return;
 if(tree[i].l==tree[i].r)
 {
  tree[i].sum=(ll)sqrt(tree[i].sum);
  return;	
 }
 if(tree[i*2].r>=l) Sqrt(i*2,l,r);
 if(tree[i*2+1].l<=r) Sqrt(i*2+1,l,r);
 tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;
 return ; 
}
int main()
{
	ios::sync_with_stdio(false);
    int n,m,a,b,c,t=0;
    while(cin>>n)
    {
     t++;
     cout<<"Case #"<<t<<":"<<endl;
     build(1,1,n);
     cin>>m;
     while(m--)
     {
      cin>>c>>a>>b;
      if(a>b) swap(a,b); 
      if(c==0)
       Sqrt(1,a,b);
      else
       cout<<search(1,a,b)<<endl;
	 }    	
	 cout<<endl;
	}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值