线段树

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.

InputThe 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.
OutputFor 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


思路 : 0 和 1 代表两种操作 , 0 表示将这个区间的数全部变为其开根号后的数, 1 表示查询这个区间

  每个点的数据范围是 long long , 但其在开 7 次根号后也会变为 1 , 当其变为 1 后在操作就没意义的, 又因为查询的操作有很多次, 因此可以对树添加一个标记,当此区间全部为 1 时,不在访问。
  
  对区间上的点全部更新,要留意一下 , 我就是这里超时的 !!!

  
/*
 * Author:  ry 
 * Created Time:  2017/10/11 17:41:28
 * File Name: 1.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long
#define lson l, m, k<<1
#define rson m+1, r, k<<1|1

ll sum[eps<<2];
bool lazy[eps<<2];
int a, b;
ll ans;

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

void update(int l, int r, int k){
    if (lazy[k]) return;
    if (l == r){
        sum[k] = sqrt(sum[k]);
        if (sum[k] <= 1) lazy[k] = true;
        return;
    }
    int m = (l + r) >> 1;
    if (a <= m) update(lson);
    if (b > m) update(rson);
    lazy[k] = lazy[k<<1] & lazy[k<<1|1];
    sum[k] = sum[k<<1] + sum[k<<1|1];
}

void query(int l, int r, int k){
    if (a <= l && r <= b){
        ans += sum[k];
        return;
    }
    int m = (l + r) >> 1;
    if (a <= m) query(lson);
    if (b > m) query(rson);
}

int main() {
    int n, q, f;
    int k = 1;
    
    while (~scanf("%d", &n)){
        build(1, n, 1);
        scanf("%d", &q);
        memset(lazy, 0, sizeof(lazy));
        printf("Case #%d:
", k++);
        while (q--){
            scanf("%d%d%d", &f, &a, &b);
            if (a > b) swap(a, b);
            if (f == 0){
                update(1, n, 1);
            }
            else {
                ans = 0;
                query(1, n, 1);
                printf("%lld
", ans);
                //int cnt = 9;
                //for(int i = 1; i <= n*4; i++){
                    //if (lazy[i]) cnt++;
                //}
                //printf("cnt %d
", cnt);
            }
        }
        printf("
");
    }

    return 0;
}
  
东北日出西边雨 道是无情却有情
原文地址:https://www.cnblogs.com/ccut-ry/p/7652575.html