AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

LeetCode E. 发通知. 2025/5/20 差分    原题链接    简单

作者: 作者的头像   夏智彬 ,  2025-05-20 19:25:08 · 江西 ,  所有人可见 ,  阅读 1


0


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int n;
int k;
typedef pair<LL, int> PII1;
typedef pair<LL, LL> PII2;

#define x first
#define y second

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // int a=(2 ^ 3 ^ 7);
    // int b=(((((2 ^ 3 ^ 7)) ^ 3) ^ 7) );
    // cout << a << ' ' << b << "\n";
    cin >> n >> k;

    vector<PII2> h_v;
    vector<PII1> c_v;
    h_v.reserve(n * 2);
    c_v.reserve(n * 2);

    for (int i = 0; i < n; i ++ )
    {
        LL l, r, w;
        cin >> l >> r >> w;

        h_v.push_back({l    ,  w});
        h_v.push_back({r + 1,  w});
        c_v.push_back({l    ,  1});
        c_v.push_back({r + 1, -1});
    }

    struct Thing
    {
        LL t;
        LL t_res;
        int cnt;
    };

    vector<Thing> ev;
    ev.reserve(2 * n);


    sort(h_v.begin(), h_v.end());
    sort(c_v.begin(), c_v.end());

    int h_idx = 0, c_idx = 0;
    int h_len = h_v.size(), c_len = c_v.size();
    // cout << h_len << ' ' << c_len << endl;
    LL t_res = 0;
    int t_cnt = 0;
    while (h_idx < h_len || c_idx < c_len)
    {
        LL t;
        if (c_idx >= c_len || (h_idx < h_len) && (h_v[h_idx].x <= c_v[c_idx].x))
            t = h_v[h_idx].x;
        else
            t = c_v[c_idx].x;

        while (h_idx < h_len && t == h_v[h_idx].x) t_res ^= h_v[h_idx ++ ].y;
        while (c_idx < c_len && t == c_v[c_idx].x) t_cnt += c_v[c_idx ++ ].y;
        // cout <<  h_idx << ' ' << t << ' ' << t_res << ' ' <<  t_cnt << endl;
        ev.push_back({t, t_res, t_cnt});
    }

    int total_len = ev.size();

    bool is_res = false;
    LL res = 0;
    LL tt_res = 0;
    t_cnt = 0;
    for (int i = 0; i < total_len; i ++ )
    {
        LL ti = ev[i].t;
        t_cnt = ev[i].cnt;
        tt_res = ev[i].t_res;
        // cout << ti << ' ' << tt_res << ' ' << t_cnt << endl;
        if (t_cnt >= k)
        {
            res = max(res, tt_res);
            is_res = true;
        }

    }

    if (is_res) cout << res << "\n";
    else cout << -1 << "\n";

    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息