codeforces 272C. Dima and Staircase(线段树)

简介: 题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。

题目链接

   题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。

87468438b71f242d1d94c0a17d9ba8ba_30af935b079ab92fd8a62efef54da644c340a251.png

  因为都是放在最左边的,所以只要和最左边的高度比较,这样就不用更新线段树了。

代码:

//cf 272 C
//2013-05-14-20.26
#include <stdio.h>
using namespace std;
const int maxn = 100005;
struct node
{
    int l, r, mid;
    __int64 m;
}tree[maxn<<2];
__int64 a[maxn];
__int64 max(__int64 a, __int64 b)
{
    if (a > b)
        return a;
    return b;
}
void build(int l, int r, int o)
{
    tree[o].l = l;
    tree[o].r = r;
    int mid = (l+r)>>1;
    tree[o].mid = mid;
    if (l == r)
    {
        tree[o].m = a[l];
        return ;
    }
    build(l, mid, o<<1);
    build(mid+1, r, (o<<1)+1);
    tree[o].m = max(tree[o<<1].m, tree[(o<<1)+1].m);
}
int query(int l, int r, int o)
{
    if (l == tree[o].l && tree[o].r == r)
        return tree[o].m;
    if (r <= tree[o].mid)
        return query(l, r, o<<1);
    else if (l > tree[o].mid)
        return query(l, r, (o<<1)+1);
    else
        return max(query(l, tree[o].mid, o<<1), query(tree[o].mid+1, r, (o<<1)+1));
}
int main()
{
    int n, m;
    while (scanf("%d", &n) != EOF)
    {
        for (int i = 1; i <= n; i++)
        {
            scanf("%I64d", &a[i]);
        }
        build(1, n, 1);
        __int64 high = a[1];
        scanf("%d", &m);
        __int64 w, h;
        while (m--)
        {
            scanf("%I64d %I64d", &w, &h);
            printf("%I64d\n", max(query(1, w, 1), high));
            high = max(query(1, w, 1), high) + h;
        }
    }
    return 0;
}
目录
相关文章
|
7月前
|
机器学习/深度学习 存储 C++
[蓝桥杯] 树状数组与线段树问题(C/C++)
[蓝桥杯] 树状数组与线段树问题(C/C++)
53 0
|
12月前
51nod 1711 平均数(二分 + 树状数组好题)
51nod 1711 平均数(二分 + 树状数组好题)
76 0
|
人工智能 vr&ar Perl
codeforces1509 C. The Sports Festival (区间DP)
codeforces1509 C. The Sports Festival (区间DP)
86 0
|
人工智能
codeforces455——A. Boredom(线性DP)
codeforces455——A. Boredom(线性DP)
99 0
codeforces455——A. Boredom(线性DP)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
105 0
|
人工智能
Codeforces-1260-E. Tournament贪心
题意: 有n个人,第i个人有力量值i,这n个人中,每次对局两两之间进行solo,如果说一个人的力量之大于他的对手,这个人是赢的,赢得比赛的选手会进入下一轮比赛中。 然而里面有一个人为你的朋友,他或许并不是里面最强的人,要想让朋友成为冠军,就需要对必要的人进行贿赂,求出最少需要花费多少才能使得朋友成为冠军。在每次对局中,都可以进行任意的对局安排,对局安排只取决于自己
75 0
|
人工智能
线段树水题
Problem Description 很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。 这让很多学生很反感。 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
91 0
|
Java
HDOJ/HDU 5686 Problem B(斐波拉契+大数~)
HDOJ/HDU 5686 Problem B(斐波拉契+大数~)
82 0