Can you answer these queries?( HDU - 4027,线段树 + 细节)

一.题目链接:

HDU-4027

二.题目大意:

n 个数.

有两种操作.

0 X Y:将区间 [X, Y] 的数开方(向下取整)

1 X Y:查询区间 [X, Y] 的和.

三.分析:

直接线段树单点更新会 TLE,由于 2^{63} 根号 7 次后向下取整为 1.

所以只需要记录每段区间被更新过的次数.

注意:所给的 X,Y 大小关系不确定 因为这里 WA 了三天啊啊啊啊啊啊 555.

还有不要 PE 就好了!

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define pi acos(-1.0)
#define ll long long int
using namespace std;

const int M = (int)1e5;

struct node
{
    int l;
    int r;
    ll w;
    int f;
}tree[M * 4 + 5];

ll ans;

void build(int k, int l, int r)
{
    tree[k].l = l;
    tree[k].r = r;
    tree[k].f = 0;
    if(l == r)
    {
        scanf("%lld", &tree[k].w);
        return;
    }
    int mid = (l + r) / 2;
    build(k * 2, l, mid);
    build(k * 2 + 1, mid + 1, r);
    tree[k].w = tree[k * 2].w + tree[k * 2 + 1].w;
}

void push(int k)
{
    tree[k * 2].f = max(tree[k * 2].f, tree[k].f);
    tree[k * 2 + 1].f = max(tree[k * 2 + 1].f, tree[k].f);
    tree[k].f = min(tree[k * 2].f, tree[k * 2 + 1].f);
}

void interver(int k, int l, int r, int a, int b)
{
    if(tree[k].f >= 7)
        return;
    if(l == r)
    {
        tree[k].w = (ll)sqrt(1.0 * tree[k].w);
        tree[k].f++;
        return;
    }
    push(k);
    int mid = (l + r) / 2;
    if(a <= mid)
        interver(k * 2, l, mid, a, b);
    if(mid < b)
        interver(k * 2 + 1, mid + 1, r, a, b);
    tree[k].w = tree[k * 2].w + tree[k * 2 + 1].w;
}

void query(int k, int l, int r, int a, int b)
{
    if(l >= a && r <= b)
    {
        ans += tree[k].w;
        return;
    }
    push(k);
    int mid = (l + r) / 2;
    if(a <= mid)
        query(k * 2, l, mid, a, b);
    if(mid < b)
        query(k * 2 + 1, mid + 1, r, a, b);
}

int main()
{
    int n;
    int ca = 0;
    while(~scanf("%d", &n))
    {
        printf("Case #%d:\n", ++ca);
        build(1, 1, n);
        int m;
        scanf("%d", &m);
        while((m--) > 0)
        {
            int t, a, b;
            scanf("%d %d %d", &t, &a, &b);
            if(a > b)
                swap(a, b);
            if(!t)
            {
                interver(1, 1, n, a, b);
            }
            else
            {
                ans = 0;
                query(1, 1, n, a, b);
                printf("%lld\n", ans);
            }
        }
        printf("\n");
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值