标签:line 解决 struct mes algo typedef printf bit 关系
这个题目不是很难,因为给你的这个S是单调递增的,所以就用优先队列+权值线段树就可以很快的解决了。
这个+读入挂可以优化,不过不用也没关系。
#include <cstdio> #include <cstring> #include <cstdlib> #include <queue> #include <bitset> #include <string> #include <algorithm> #include <iostream> #include <map> #define inf 0x3f3f3f3f #define inf64 0x3f3f3f3f3f3f3f3f using namespace std; const int maxn = 1e6 + 10; const int mod = 1e9 + 7; const int M = 1e6; typedef long long ll; int num[maxn * 4]; void build(int id, int l, int r) { num[id] = 0; if (l == r) return; int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); } struct node { int l, r, val; node(int l=0,int r=0,int val=0):l(l),r(r),val(val){} bool operator<(const node &a)const { return a.r < r; } }; priority_queue<node>que; void update(int id, int l, int r, int pos, int f) { if (f) num[id]++; else num[id]--; if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) update(id << 1, l, mid, pos, f); else update(id << 1 | 1, mid + 1, r, pos, f); } int query(int id, int l, int r, int k) { if (l == r) return l; int mid = (l + r) >> 1; int val1 = num[id << 1]; if (val1 >= k) return query(id << 1, l, mid, k); return query(id << 1 | 1, mid + 1, r, k - val1); } inline int read() { int X = 0; bool flag = 1; char ch = getchar(); while (ch<‘0‘ || ch>‘9‘) { if (ch == ‘-‘) flag = 0; ch = getchar(); } while (ch >= ‘0‘&&ch <= ‘9‘) { X = (X << 1) + (X << 3) + ch - ‘0‘; ch = getchar(); } if (flag) return X; return ~(X - 1); } inline void write(int X) { if (X < 0) { putchar(‘-‘); X = ~(X - 1); } int s[20], top = 0; while (X) { s[++top] = X % 10; X /= 10; } if (!top) s[++top] = 0; while (top) putchar(s[top--] + ‘0‘); putchar(‘\n‘); } int main() { int t = read(); for (int cas = 1; cas <= t; cas++) { printf("Case %d:\n", cas); while (!que.empty()) que.pop(); int n = read(); memset(num, 0, sizeof(num)); while (n--) { int opt; scanf("%d", &opt); if (opt == 1) { int l, val, r; l = read(), val = read(), r = read(); update(1, 1, M, val, 1); que.push(node(l, r, val)); } else { int s, k; s = read(), k = read(); while (!que.empty()&&que.top().r < s) { update(1, 1, M, que.top().val, 0); que.pop(); } if (k > que.size()) write(-1); else write(query(1, 1, M, k)); } } } return 0; }
组队训练 K K - The Stream of Corning 2
标签:line 解决 struct mes algo typedef printf bit 关系
原文地址:https://www.cnblogs.com/EchoZQN/p/11596031.html