Can you answer these queries? HDU - 4027 有点坑

#include<iostream>
#include<cstring>
#include<cstdio>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N];
int n,m;
int op,l,r;
struct node
{
    ll l,r;
    ll sum;
}tr[N*4];
void build(int root,int l, int r)
{
    tr[root]={l,r};
    tr[root].sum=0;
    if(l==r)
    {
        tr[root].sum=a[l];
        return;
    }
    int mid=l+r>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    tr[root].sum=tr[root<<1].sum+tr[root<<1|1].sum;
}
void update(int root,int l,int r)
{
    if(l>tr[root].r||tr[root].l>r)
        return;
    if (tr[root].sum==(tr[root].r-tr[root].l + 1))
        return;
    if (tr[root].l==tr[root].r)
    {
        tr[root].sum=sqrt(tr[root].sum);
        return;
    }
    int mid=tr[root].l+tr[root].r>>1;
    update(root<<1,l,r);
    update(root<<1|1,l,r);
    tr[root].sum=tr[root<<1].sum+tr[root<<1|1].sum;
}
ll Query(int root,int l,int r)
{
    if (l>tr[root].r||tr[root].l>r)
        return 0;
    if (l<=tr[root].l&&tr[root].r<=r)
        return tr[root].sum;
    int mid=tr[root].l+tr[root].r>>1;
    ll res=0;
    res+=Query(root<<1,l,r);
    res+=Query(root<<1|1,l,r);
    return res;
}
int main()
{
    int cnt=0;
    while(~scanf("%d",&n))
    {
        printf("Case #%d:\n",++cnt);
        for (int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        scanf("%d",&m);
        build(1,1,n);
        while (m--)
        {
            scanf("%d%d%d",&op,&l,&r);
            if(l>r)
                swap(l,r);
            if(op==0)
                update(1,l,r);
            else
                printf("%lld\n",Query(1,l,r));
        }
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12293982.html

时间: 2024-08-04 12:25:27

Can you answer these queries? HDU - 4027 有点坑的相关文章

V - Can you answer these queries? HDU - 4027 线段树

V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开十几次就变成1了,所以就直接单点更新,但是这个可以剪枝. 如果碰到区间长度和区间大小相同就可以不用更新了. 我也是无语了,想到刚刚的做法之后很好写了, 不过要注意以下x,y 大小可能x>y, 这个bug如果不是之前看题解的时候瞄到了,我可能找不出来了. 因为我写对拍我肯定会保证x<y 的,还是就是

Can you answer these queries? HDU - 4027

Can you answer these querites? HDU - 4027 普通的线段树题,但是有一个问题是,区间更新时,因为必须更新每个点,才能更新区间,那么线段树更新就很慢了,无法使用lazy数组.有一个小技巧是当区间和等于区间长度时,那么说明已经到最好的情况了,不用再修改了.这一步简化后就可以过了 如果写线段树可以像喝水一样自然就好了 1 #include <iostream> 2 #include <cstring> 3 #include <vector>

H - Can you answer these queries? HDU 4027 (线段树+延迟标记+开根号的速度)

H - Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4027 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our

HDU 4027 Can you answer these queries? (线段树+区间点修改)

题意:给你 n 个数,m个询问(c,x,y) c==0 把x,y区间的值变为原来的平方根(向下取整) c==1 计算x,y区间的和. 利用1的开方永远为1剪枝.. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> //#include<map> #include<cmath> #include<iostream> #include

HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化 #include <stdio.h> #include<math.h> #define N 100010 #define LL __int64 #define lson l,m,rt<<1 #define rson

hdu 4027 Can you answer these queries?

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description 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 b

HDU 4027 Can you answer these queries? 线段树裸题

题意: 给定2个操作 0.把区间的每个数sqrt 2.求和 因为每个数的sqrt次数很少,所以直接更新到底,用个标记表示是否更新完全(即区间内的数字只有0,1就不用再更新了) #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #incl

HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】

Can you answer these queries? Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4027 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use

HDU 4027 Can you answer these queries?(线段树区间开方)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 16260    Accepted Submission(s): 3809 Problem Description A lot of battleships of evil are arranged in a line befor