HDU 4027 (线段树, 区间和(对区间内的每一个数操作不同),单点更新)

Can you answer these queries?

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


 

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

 

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

 

 

Source

The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

 

题意: 给定 n 个数, 有m次操作, 1: 对区间 [x,y] 中的每个数 对自己开方(向下取整), 2: 输出区间 [x,y] 的和。

思路: 由于对区间中每个数的操作都是对自己开方, 显然只能使用单点更新。 但是数据是100000, 显然直接单点更新会TLE, 故增加一个flag 判断 区间[x,y]是否全部为0或1,  因为数 0 或 1 对自己开方不变。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn = 100000 + 10;
struct{
    ll sum;
    bool flag;                //flag 为 1, 则此区间全部为1
}tree[maxn * 4];
ll k;

void build(int l,int r,int root){
    tree[root].flag = 0;
    if(l == r){
        scanf("%lld",&k);
        tree[root].sum = k;
        tree[root].flag = (k <= 1? 1 : 0);
    }else {
        int mid = (l + r) / 2;
        build(l,mid,root * 2);
        build(mid + 1,r,root * 2 + 1);
        tree[root].sum = tree[root*2].sum + tree[root*2+1].sum;
        tree[root].flag = tree[root*2].flag & tree[root*2+1].flag;
    }
}

void update(int l,int r,int ql,int qr,int root){
    if(l == r){
        tree[root].sum = floor(sqrt(1.0*tree[root].sum));
        if(tree[root].sum <= 1) tree[root].flag = 1;
    }else {
        int mid = (l + r) / 2;
        if(ql <= mid && !tree[root].flag) update(l,mid,ql,qr,root * 2);
        if(qr > mid && !tree[root].flag) update(mid + 1,r,ql,qr,root * 2 + 1);
        tree[root].sum = tree[root*2].sum + tree[root*2+1].sum;
        tree[root].flag = tree[root*2].flag & tree[root*2+1].flag;
    }
}

ll query(int l,int r,int ql,int qr,int root){
    if(ql <=l && r <= qr)
        return tree[root].sum;
    int mid = (l + r) / 2;
    ll ans = 0;
    if(ql <= mid) ans += query(l,mid,ql,qr,root * 2);
    if(qr > mid) ans += query(mid + 1,r,ql,qr,root * 2 + 1);
    return ans;
}

int main()
{
    int n,m; int cas = 1;
    while(~scanf("%d",&n)){
        build(1,n,1);
        scanf("%d",&m);
        printf("Case #%d:\n",cas++);
        int t,x,y;
        while(m --){
            scanf("%d%d%d",&t,&x,&y);
            if(x > y) swap(x,y);
            if(t) printf("%lld\n",query(1,n,x,y,1));
            else update(1,n,x,y,1);
        }
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值