Can you answer these queries?

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. 
 

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

 

/*
题意:有 n艘战舰,每艘战舰都有一定的能量值,炮弹每次炮轰区间内的战舰,区间内战舰的能量值变为原来的想下取整的开方数,
    然后查询区间内的战舰总能量

初步思路:addv数组用来储存这棵树上的节点被轰过几次,向上向下更新的时候不会写了,试一下笨办法单点更新更新函数,不能找
    到满足的区间就更新,必须要跟新到叶子节点才能,addv数组现在的作用是记录

#错误:把case i漏掉了
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
/******************************线段树区间更新模板*************************************/
const int MAXN=100000+100;
#define lson i*2,l,m
#define rson i*2+1,m+1,r
ll sum[MAXN<<2];
ll addv[MAXN<<2];

void PushUp(int i)
{
    sum[i]=sum[i*2]+sum[i*2+1];
    addv[i]=addv[i*2]&&addv[i*2+1];
}

void build(int i,int l,int r)
{
    addv[i]=0;
    if(l==r)
    {
        scanf("%lld",&sum[i]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(i);
}

void update(int ql,int qr,int i,int l,int r)
{
    if(l==r)//必须更新到叶子节点
    {   
        sum[i]= sqrt(sum[i]);
        if(sum[i]<=1) addv[i]=1;
        return ;
    }
    int m=(l+r)>>1;
    if(ql<=m&&!addv[i*2]) update(ql,qr,lson);
    if(m<qr&&!addv[i*2+1]) update(ql,qr,rson);
    PushUp(i);
}

ll query(int ql,int qr,int i,int l,int r)
{
    if(ql<=l&&r<=qr)
    {
        return sum[i];
    }
    int m=(l+r)>>1;
    ll res=0;
    if(ql<=m) res+=query(ql,qr,lson);
    if(m<qr) res+=query(ql,qr,rson);
    return res;
}
/******************************线段树区间更新模板*************************************/
void init(){
    memset(sum,0,sizeof sum);
    memset(addv,0,sizeof addv);
}
int n,q;
int str,x,y;
int Case=1;
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF){
        printf("Case #%d:\n",Case++);
        init();
        build(1,1,n);
        scanf("%d",&q);
        for(int i=0;i<q;i++){
            scanf("%d%d%d",&str,&x,&y);
            if(x>y) swap(x,y);
            if(str){
                printf("%lld\n",query(x,y,1,1,n));
            }else{
                update(x,y,1,1,n);
            }
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2017-03-20 13:04  勿忘初心0924  阅读(246)  评论(0编辑  收藏  举报