The Stream of Corning 2 --- Gym - 102091K(区间第 k 大--权值线段树)
Posted stay-online
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Stream of Corning 2 --- Gym - 102091K(区间第 k 大--权值线段树)相关的知识,希望对你有一定的参考价值。
题目
https://vjudge.net/problem/Gym-102091K
题意
给出 T 组数据,每组数据给出 n 个操作,操作分为下列两种:
操作 1:在 L 到 R 这个时间段内加入 W。(输入顺序:op,L,K,W)
操作 2:问 T 这个时间点第 K 大的数是什么。(输入顺序:op,T,K)
tip:题目保证时间段 L 和 时间点 T 按照增序输入。
题解
因为时间一定按照增序输入,那么只需要用优先队列记录时间段,将 R 小于 T 的时间段都退出,维护权值线段树即可。即可个鬼啊,输入数据 T 到死……
小提示:加一个神仙快读,可以直接少3倍时间(wdnmd)。
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define met(a, b) memset(a, b, sizeof(a)) #define rep(i, a, b) for(int i = a; i <= b; i++) #define bep(i, a, b) for(int i = a; i >= b; i--) #define lowbit(x) (x&(-x)) #define MID ((l + r) / 2) #define ls (pos<<1) #define rs ((pos<<1)+1) #define pb push_back #define ios() ios::sync_with_stdio(0) using namespace std; const int maxn = 1e5 + 1010; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll mod = 123456789; const double eps = 1e-4; struct NODE { int op, l, w, r; }arr[maxn]; struct node { int l, r, w; bool operator < (const node &a) const { return a.r < r; } }; int root[maxn*12]; int b[maxn], tail; priority_queue<node> que; // void root_init(int pos, int l, int r) { // root[pos] = 0; // if(l == r) return; // root_init(ls, l, MID); // root_init(rs, MID + 1, r); // } void update(int pos, int l, int r, int k, int val) { if(l == r) { root[pos] += val; return; } if(k <= MID) update(ls, l, MID, k, val); else update(rs, MID + 1, r, k, val); root[pos] = root[ls] + root[rs]; } int query(int pos, int l, int r, int k) { // cout << root[pos] << ‘ ‘ << k << ‘ ‘ << l << ‘ ‘ << r << endl; if(k > root[pos]) return -1; if(l == r) return l; if(root[ls] >= k) return query(ls, l, MID, k); else return query(rs, MID + 1, r, k - root[ls]); } int main() { int T, k = 0; scanf("%d", &T); while(T--) { tail = 0; int n; // que = priority_queue<node>(); scanf("%d", &n); rep(i, 1, n) { scanf("%d", &arr[i].op); if(arr[i].op == 1) scanf("%d%d%d", &arr[i].l, &arr[i].w, &arr[i].r); else scanf("%d%d", &arr[i].l, &arr[i].r); if(arr[i].op == 1) b[++tail] = arr[i].w; } sort(b + 1, b + 1 + tail); tail = unique(b + 1, b + 1 + tail) - b - 1; // rep(i, 1, tail) cout << b[i] << ‘ ‘; // cout << endl; // root_init(1, 1, tail); printf("Case %d: ", ++k); rep(i, 1, n) { if(arr[i].op == 1) { int pos = lower_bound(b + 1, b + 1 + tail, arr[i].w) - b; que.push((node){arr[i].l, arr[i].r, arr[i].w}); update(1, 1, tail, pos, 1); } else { while(!que.empty()) { node t = que.top(); if(t.r >= arr[i].l) break; que.pop(); // cout << t.w << ‘ ‘; int pos = lower_bound(b + 1, b + 1 + tail, t.w) - b; update(1, 1, tail, pos, -1); } // cout << arr[i].r << ‘ ‘ ; int pos = query(1, 1, tail, arr[i].r); if(pos == -1) printf("-1 "); else printf("%d ", b[pos]); } } while(!que.empty()) { node t = que.top(); que.pop(); int pos = lower_bound(b + 1, b + 1 + tail, t.w) - b; update(1, 1, tail, pos, -1); } } return 0; }
以上是关于The Stream of Corning 2 --- Gym - 102091K(区间第 k 大--权值线段树)的主要内容,如果未能解决你的问题,请参考以下文章
error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly before end of the underlying st...
error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly before end of the underlying st...
error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly before end of the underlying st...
Most efficient way to get the last element of a stream
EOFError: Compressed file ended before the end-of-stream marker was reached
lto1: fatal error: bytecode stream generated with LTO version 8.1 instead of the expected 4.0