HDU4027(线段树单点更新区间)

Can you answer these queries?

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



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

#include"cstdio"
#include"cmath"
#include"algorithm"
using namespace std;
const int MAXN=100005;
typedef long long LL;
struct node{
    int l,r;
    LL sum;
}a[MAXN*4];

void build(int rt,int l,int r)
{
    a[rt].l=l;
    a[rt].r=r;
    if(l==r)
    {
        scanf("%lld",&a[rt].sum);
        return ;
    }

    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build((rt<<1)|1,mid+1,r);
    a[rt].sum=a[rt<<1].sum+a[(rt<<1)|1].sum;
}

void update(int rt,int l,int r)
{
    if(a[rt].sum==(a[rt].r-a[rt].l+1))    return ;//优化,各个叶子节点的值为1后不用再更新
    if(a[rt].l==a[rt].r)
    {
        a[rt].sum=(LL)sqrt(a[rt].sum*1.0);
        return ;
    }    

    int mid=(a[rt].l+a[rt].r)>>1;
    if(r<=mid)    update(rt<<1,l,r);
    else if(mid<l)    update((rt<<1)|1,l,r);
    else{
        update(rt<<1,l,mid);
        update((rt<<1)|1,mid+1,r);
    }
    a[rt].sum=a[rt<<1].sum+a[(rt<<1)|1].sum;
}

LL query(int rt,int l,int r)
{
    if(a[rt].l==l&&a[rt].r==r)
    {
        return a[rt].sum;
    }

    int mid=(a[rt].l+a[rt].r)>>1;
    if(r<=mid)    return    query(rt<<1,l,r);
    else if(mid<l)    return    query((rt<<1)|1,l,r);
    else{
        return query(rt<<1,l,mid)+query((rt<<1)|1,mid+1,r);
    }
}

int main()
{
    int cas=1;
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,1,n);
        printf("Case #%d:\n",cas++);
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int t,x,y;
            scanf("%d%d%d",&t,&x,&y);//x有可能比y大
            if(x>y)    swap(x,y);
            if(t==0)
            {
                update(1,x,y);
            }
            else
            {
                printf("%lld\n",query(1,x,y));
            }
        }
        printf("\n");
    }

    return 0;
}
时间: 2024-10-06 04:24:15

HDU4027(线段树单点更新区间)的相关文章

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

hdu 3308 线段树单点更新 区间合并

http://acm.hdu.edu.cn/showproblem.php?pid=3308 学到两点: 1.以区间端点为开始/结束的最长......似乎在Dp也常用这种思想 2.分类的时候,明确标准逐层分类,思维格式: 条件一成立: { 条件二成立: { } else { } } else { 条件二成立: { } else { } } 上面的这种方式很清晰,如果直接想到那种情况iif(条件一 &条件二)就写,很容易出错而且把自己搞乱,或者情况不全,,,我就因为这WA了几次 3.WA了之后 ,

hdu2795(线段树单点更新&amp;区间最值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要尽量将其靠上贴,并输出其最上能贴在哪个位置: 思路:可以将每行剩余空间大小存储到一个数组中,那么对于当前 1 * x 的广告,只需找到所有剩余空间大于的 x 的行中位置最小的即可: 不过本题数据量为 2e5,直接暴力因该会 tle.可以用个线段树维护一下区间最大值,然后查询时对线段树二分即可: 代码

hdu - 4973 - A simple simulation problem.(线段树单点更新 + 区间更新)

题意:初始序列 1, 2, ..., n,m次操作(1 <= n,m<= 50000),每次操作可为: D l r,将区间[l, r]中的所有数复制一次: Q l r,输出区间[l, r]中同一数字个数的最大值. (0 <= r – l <= 10^8, 1 <= l, r <= 序列元素个数) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4973 -->>因为区间内数字是依次递增的,所以可以以数字为叶建线段

POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

HDU 3308 LCIS (线段树&#183;单点更新&#183;区间合并)

题意  给你一个数组  有更新值和查询两种操作  对于每次查询  输出对应区间的最长连续递增子序列的长度 基础的线段树区间合并  线段树维护三个值  对应区间的LCIS长度(lcis)  对应区间以左端点为起点的LCIS长度(lle)  对应区间以右端点为终点的LCIS长度(lri)  然后用val存储数组对应位置的值  当val[mid + 1] > val[mid] 的时候就要进行区间合并操作了 #include <cstdio> #include <algorithm>

HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)

Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every vill

HDU 3308 线段树单点更新+区间查找最长连续子序列

LCIS                                                              Time Limit: 6000/2000 MS (Java/Others)                                                                 Memory Limit: 65536/32768 K (Java/Others)                                       

hdu1394(枚举/树状数组/线段树单点更新&amp;区间求和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 或 -1, 交换两个不相邻数 a, b, 逆序数 += 两者间大于 a 的个数 - 两者间小于 a 的个数: 所以只要求出初始时的逆序对数,就可以推出其余情况时的逆序对数.对于求初始逆序对数,这里 n 只有 5e3,可以直接暴力 / 树状数组 / 线段树 / 归并排序: 代码: 1.直接暴力 1