线段树(类似延迟标记) HDU

来源:互联网 发布:臭氧灯能除螨虫知乎 编辑:程序博客网 时间:2024/06/04 08:57

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 15572    Accepted Submission(s): 3654


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 263.
  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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
 

Sample Output
Case #1:1976

 

题意:对于一组数列,有两种操作,输入T, X, Y,T==0表示从第X个数到第Y个数的区间中每个数开根,T==1表示查询区间[X, Y]的和。

解题:线段树,难点在于对于区间每个数的操作不是统一的,方法是对于一个区间和,如果sum[rt] == 1,那么这一段区间就不需要更新了,于是可用类似延迟标记的方法标记一下啊,这个小技巧难点在于需要想到到1时时不需要更新了!

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>using namespace std;typedef long long LL;__int64 sum[100010<<2], col[100010<<2];void Build(int l, int r, int rt){    if(l == r) {        scanf("%I64d", &sum[rt]);        return ;    }    int mid = (l+r)>>1;    Build(l, mid, rt<<1);    Build(mid+1, r, rt<<1|1);    sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void update(int l, int r, int L, int R, int rt){    if(l == r) {        sum[rt] = sqrt(sum[rt]*1.0);        if(sum[rt] == 1) col[rt] = 1;        return ;    }    if(L <= l && r <= R && col[rt]) return ;    int mid = (l+r)>>1;    if(L <= mid) update(l, mid, L, R, rt<<1);    if(mid < R) update(mid+1, r, L, R, rt<<1|1);    sum[rt] = sum[rt<<1]+ sum[rt<<1|1];    col[rt] = col[rt<<1]&&col[rt<<1|1];}__int64 query(int L, int R, int l, int r, int rt){    if(L <= l && r <= R) return sum[rt];    int mid = (l + r) >> 1;    __int64 res = 0;    if(L <= mid) res += query(L, R, l, mid, rt<<1);    if(mid < R) res += query(L, R, mid+1, r, rt<<1|1);    return res;}int main(){    int n, m, t, x, y;    int casee = 1;    while(~scanf("%d", &n)) {        memset(sum, 0, sizeof(sum));        memset(col, 0, sizeof(col));        Build(1, n, 1);        scanf("%d", &m);        printf("Case #%d:\n", casee++);        while(m--) {            scanf("%d%d%d", &t, &x, &y);            if(x > y) {                int s = x;                x = y;                y = s;            }            if(t == 0) update(1, n, x, y, 1);            else printf("%I64d\n", query(x, y, 1, n, 1));        }        printf("\n");    }    return 0;}

0 0