(线段树单点更新)Can you answer these queries? 几许情愁

Problem 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.
 

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
0 1 10 
1 1 10 
1 1 5 
0 5 8 
1 4 8
 
Sample Output
 
  
Case #1: 
19
 7
 6
 


 

有n艘战舰,每艘战舰的防护罩都有一定的能量,我军有秘密武器,每轰一次,一个区间内的战舰防护罩将为原来能量的开方,查询的则是这个区间内战舰防护罩的能量之和。第一个数字是1表查询,0的话表更新;

要注意的是,区间的两边的边界值输入时大小是不确定的

思路:线段树,单点更新,需要注意输出格式;代码很长,希望大家能认真看完;

上代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stack>
#include<ctype.h>
#include<stdlib.h>
#include<map>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int M=100005;
ll tree[M*4];//本题要用longlong,因为耐力值总和为2的63次方
int mark[M*4];//用来标记船是否被破坏,注意和构成的数同步的
int n,m;

void add(int r)//每次不仅要存储子树和,也要看子树是否被破坏
{
    tree[r]=tree[r*2]+tree[r*2+1];
    mark[r]=mark[r*2]&&mark[2*r+1];
}

void build(int a,int b,int r)//构树
{
    if(a==b)
    {
        scanf("%lld",&tree[r]);
        return;
    }
    int mid=(a+b)/2;
    build(a,mid,r*2);
    build(mid+1,b,r*2+1);
    add(r);
}

void change(int a,int b,int r,int qa,int qb)//更新从qa到qb
{
    if(a==b)
    {
        tree[r]=sqrt(tree[r]);
        if(tree[r]<=1)
            mark[r]=1;
        return;
    }
    int mid=(a+b)/2;
    if(mid>=qa&&mark[r*2]==0)
        change(a,mid,r*2,qa,qb);
    if(mid<qb&&mark[r*2+1]==0)
        change(mid+1,b,r*2+1,qa,qb);
    add(r);
}

ll query(int a,int b,int r,int qa,int qb)//查询从qa到qb
{
    if(qa<=a&&b<=qb)
        return tree[r];
    int mid=(a+b)/2;
    ll ans=0;
    if(mid>=qa)
        ans+=query(a,mid,r*2,qa,qb);
    if(mid<qb)
        ans+=query(mid+1,b,r*2+1,qa,qb);
    return ans;
}

int main()
{
    int N=0;
    while(~scanf("%d",&n))
    {
        int i,j,a,b,c;
        memset(tree,0,sizeof(tree));
        memset(mark,0,sizeof(mark));
        build(1,n,1);
        printf("Case #%d:\n",++N);//这里也需要注意,注意格式
        scanf("%d",&m);
        for(i=0; i<m; i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            if(b>c)
                swap(b,c);
            if(a==0)
                change(1,n,1,b,c);
            if(a==1)
            {
                ll ans=query(1,n,1,b,c);
                printf("%lld\n",ans);
            }
        }
        printf("\n");//注意一组样例后有一空行
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41984014/article/details/80369232