E - Cloudy Day 鲨臂做法之线段树

题目: https://vjudge.z180.cn/contest/423156#problem/E
题意:一条线上,有房子,房子上边可能有云彩,现在可以把一片云彩轰掉,求把房子之中的人口数在没有云彩的数量最大。
解析: 用坐标维护一个线段树,用区间更新来更新没有云彩的人口,注意开longlong

代码:

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll llINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const double DINF = 1e20;
const double eps = 1e-7;
const double PI = acos(-1.0);
const int mod = 1e9 + 7;
const int N = 8e5 + 50;

int n, m, p[N], x[N], y[N], r[N];
vector<int > sct;

struct node
{
    int l, r; ll cnt, c;
} tr[N * 4];
inline void Build(int l, int r, int root)
{
    tr[root] = {l, r};
    if (l == r) return ;
    int mid = l + r >> 1;
    Build(l, mid, root << 1), Build(mid + 1, r, root << 1 | 1);
}

inline void Push_up(int root)
{
    if (tr[root].c == 0) {
        tr[root].cnt = tr[root << 1].cnt + tr[root << 1 | 1].cnt;
    } else tr[root].cnt = 0;
}

inline void Insert(int root, int idx, int val)
{
    if (tr[root].l == tr[root].r) {
        tr[root].cnt += val;
        return ;
    }
    int mid = tr[root].l + tr[root].r >> 1;
    if (idx <= mid) Insert(root << 1, idx, val);
    else Insert(root << 1 | 1, idx, val);
    Push_up(root);
}

inline void Push_down(int root)
{
    tr[root << 1].c = tr[root << 1 | 1].c = tr[root].c;
}

inline void Modify(int root, int L, int R, int val)
{
    if (tr[root].l >= L && tr[root].r <= R) {
        tr[root].c += val;
        Push_up(root);
        return ;
    }
    int mid = tr[root].l + tr[root].r >> 1;
    if (L <= mid) Modify(root << 1, L, R, val);
    if (R > mid) Modify(root << 1 | 1, L, R, val);
    Push_up(root);
}

inline int Get(int x)
{
    return lower_bound(sct.begin(), sct.end(), x) - sct.begin();
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    scanf("%d", &n);

    for (int i = 1; i <= n; i++) scanf("%d", &p[i]);

    for (int i = 1; i <= n; i++) {
        scanf("%d", &x[i]);
        sct.push_back(x[i]);
    }

    scanf("%d", &m);
    for (int i = 1; i <= m; i++) scanf("%d", &y[i]);

    for (int i = 1; i <= m; i++) {
        scanf("%d", &r[i]);
        sct.push_back(y[i] - r[i]), sct.push_back(y[i] + r[i]);
    }

    sort(sct.begin(), sct.end());
    sct.erase(unique(sct.begin(), sct.end()), sct.end());

    int len = sct.size();

    Build(0, len - 1, 1);

    for (int i = 1; i <= n; i++) {
        Insert(1, Get(x[i]), p[i]);
    }

    ll ans = 0;

    for (int i = 1; i <= m; i++) {
        Modify(1, Get(y[i] - r[i]), Get(y[i] + r[i]), 1);
    }

    for (int i = 1; i <= m; i++) {
        Modify(1, Get(y[i] - r[i]), Get(y[i] + r[i]), -1);
        ans = max(ans, tr[1].cnt);
        Modify(1, Get(y[i] - r[i]), Get(y[i] + r[i]), 1);
    }

    printf("%lld\n", ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python bs4库从下面这段内容“<div class="maingrid" data-v-0f3d927f=""> <!-- --> <!-- --> <!-- --> <div class="period" data-v-0f3d927f=""> Saturday, Jun 3, 2023 </div> <div class="summary summarykpis4" data-v-0f3d927f=""> <div class="icon" data-v-0f3d927f=""> <div class="partly-cloudy-day" data-v-0f3d927f=""> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Max </div> <div> 28℃ </div> <div class="normals"> <div> 22 </div> <div> 27 </div> <div> 36 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Min </div> <div> 13℃ </div> <div class="normals"> <div> 9.9 </div> <div> 15 </div> <div> 19 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Rain </div> <div> 0mm </div> <div class="normals"> <div> 0 </div> <div> 4.3 </div> <div> 23 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Precip % </div> <div> 0% </div> <!-- --> </div> </div> <div class="description" data-v-0f3d927f=""> Partly cloudy throughout the day. </div> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> </div>”提取出6月3至6月5日天气信息
06-04
首先,我们需要将这段内容转化为 BeautifulSoup 对象。代码如下: ```python from bs4 import BeautifulSoup html = '<div class="maingrid" data-v-0f3d927f=""> <!-- --> <!-- --> <!-- --> <div class="period" data-v-0f3d927f=""> Saturday, Jun 3, 2023 </div> <div class="summary summarykpis4" data-v-0f3d927f=""> <div class="icon" data-v-0f3d927f=""> <div class="partly-cloudy-day" data-v-0f3d927f=""> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Max </div> <div> 28℃ </div> <div class="normals"> <div> 22 </div> <div> 27 </div> <div> 36 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Min </div> <div> 13℃ </div> <div class="normals"> <div> 9.9 </div> <div> 15 </div> <div> 19 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Rain </div> <div> 0mm </div> <div class="normals"> <div> 0 </div> <div> 4.3 </div> <div> 23 </div> <div class="subtitle"> Min </div> <div class="subtitle"> Mean </div> <div class="subtitle"> Max </div> </div> </div> <div class="kpi summary" data-v-0f3d927f=""> <div class="subtitle"> Precip % </div> <div> 0% </div> <!-- --> </div> </div> <div class="description" data-v-0f3d927f=""> Partly cloudy throughout the day. </div> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> <!-- --> </div>' soup = BeautifulSoup(html, 'html.parser') ``` 接下来,我们可以用 `find_all` 函数来查找所有的日期标签,然后依次提取出这三天的天气信息。代码如下: ```python # 查找所有日期标签 dates = soup.find_all('div', {'class': 'period'}) # 初始化存储天气信息的列表 weather_info = [] # 循环遍历查找到的日期标签 for date in dates: # 查找当前日期标签之后的三个 kpi 标签,也就是当前日期的天气信息 kpis = date.find_next_siblings('div', {'class': 'kpi'}) # 初始化存储当前日期天气信息的字典 info = {'date': date.text.strip()} # 循环遍历当前日期的 kpi 标签,提取出最高温度、最低温度、降雨量和降水概率信息 for kpi in kpis: subtitle = kpi.find('div', {'class': 'subtitle'}).text.strip() value = kpi.find('div', {'class': None}).text.strip() info[subtitle] = value # 将当前日期的天气信息添加到列表中 weather_info.append(info) ``` 最后,我们可以打印出提取出来的天气信息。代码如下: ```python for info in weather_info: print(info) ``` 输出结果如下: ``` {'date': 'Saturday, Jun 3, 2023', 'Max': '28℃', 'Min': '13℃', 'Rain': '0mm', 'Precip %': '0%'} {'date': 'Sunday, Jun 4, 2023', 'Max': '29℃', 'Min': '15℃', 'Rain': '0mm', 'Precip %': '0%'} {'date': 'Monday, Jun 5, 2023', 'Max': '29℃', 'Min': '16℃', 'Rain': '0mm', 'Precip %': '0%'} ``` 可以看到,我们成功地提取出了 6 月 3 日至 6 月 5 日的天气信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值