线段树lazy技巧/常见坑点(Can you answer these queries? HDU-4027)

题目链接
对于这种看似比较高大上的题目。(单点修改运算与pushup运算不符合交换律。)但是,我们发现,对于一个long long咱们开平方没多久就会变成不变的1。咱们只要标记一下这个区间是不是都是1,如果都是,就直接return掉。
ps.线段树常见坑点不保证给出的区间两端点前者小于等于后者。又是一个可以让我wa一天的地
下面是ac代码(采用边读入边建树的方式):

#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1e5+5;
struct Node
{
    int l, r;
    ll w;
    bool cur;
}tr[N<<2];
int n;
int mx;
void build(int p, int l, int r)
{
    mx = max(p, mx);
    tr[p].l = l; tr[p].r = r;
    if (l == r)
    {
        scanf("%lld", &tr[p].w);
        tr[p].cur = tr[p].w == 1?1:0;
        return;
    }
    int mid = (l + r) >> 1;
    build(p<<1, l, mid);
    build(p<<1|1, mid+1, r);
    tr[p].cur = tr[p<<1].cur & tr[p<<1|1].cur;
    tr[p].w = tr[p<<1].w + tr[p<<1|1].w;
}
void change(int p, int l, int r)
{
    if (tr[p].cur) return;
    if (tr[p].l == tr[p].r)
    {
        tr[p].w = (int)sqrt((double)tr[p].w);
        if (tr[p].w == 1) tr[p].cur = 1;
        return;
    }
    int mid = (tr[p].l + tr[p].r) >> 1;
    if(l <= mid) change(p<<1, l, r);
    if (r > mid) change(p<<1|1, l, r);
    tr[p].w = tr[p<<1].w + tr[p<<1|1].w;
    tr[p].cur = tr[p<<1].cur & tr[p<<1|1].cur;
}
ll ask(int p, int l, int r)
{
    if (tr[p].l >= l && tr[p].r <= r)
        return tr[p].w;
    int mid = (tr[p].l + tr[p].r) >> 1;
    ll val = 0;
    if (l <= mid)val += ask(p<<1, l, r);
    if (r > mid) val += ask(p<<1|1, l, r);
    return val;
}
void print()
{
    for (int i = 1; i <= mx; i++)
        cout << tr[i].l << " " << tr[i].r << " " << tr[i].w << " " <<tr[i].cur <<endl;
    cout <<endl;
}
int main()
{
    int t0 = 1;
    while(~scanf("%d", &n))
    {
        build(1, 1, n);
      //  print();
        int q;
        scanf("%d", &q);
        printf("Case #%d:\n", t0++);
        while(q--)
        {
            int op;
            int a, b;
            scanf("%d%d%d", &op, &a, &b);
            if (a > b) swap(a, b);
            if (op)
                printf("%lld\n", ask(1, a, b));
            else
                change(1, a, b);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值