結果
問題 | No.649 ここでちょっとQK! |
ユーザー |
|
提出日時 | 2018-02-09 22:41:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 266 ms / 3,000 ms |
コード長 | 1,786 bytes |
コンパイル時間 | 1,953 ms |
コンパイル使用メモリ | 182,580 KB |
実行使用メモリ | 19,588 KB |
最終ジャッジ日時 | 2024-10-08 16:28:15 |
合計ジャッジ時間 | 6,913 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 32 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; //1-indexであることに注意 template <typename T> struct BIT { int n; vector<T> data; BIT(int n) : n(n), data(n + 1, 0) {} T sum(int i) { T s = 0; while (i > 0) { s += data[i]; i -= i & -i; } return s; } void add(int i, T x) { while (i <= n) { data[i] += x; i += i & -i; } } }; //引数unzipには圧縮前のvectorを入れる int compress(vector<ll>& unzip, map<ll, int>& zip) { sort(unzip.begin(), unzip.end()); unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end()); for (int i = 0; i < unzip.size(); i++) { zip[unzip[i]] = i; } return unzip.size(); } int main() { cin.tie(0); ios::sync_with_stdio(false); int Q, K; cin >> Q >> K; vector<ll> q(Q); vector<ll> v; for (int i = 0; i < Q; i++) { cin >> q[i]; if (q[i] == 1) { ll x; cin >> x; v.push_back(x); q[i] = x; } else { q[i] = -1; } } map<ll, int> zip; compress(v, zip); BIT<int> b(v.size()); for (int i = 0; i < Q; i++) { if (q[i] == -1) { if (b.sum(v.size()) < K) { cout << -1 << endl; } else { int ng = 0, ok = v.size(); while (ok - ng > 1) { int mid = (ok + ng) / 2; if (b.sum(mid) >= K) ok = mid; else ng = mid; } cout << v[ok - 1] << endl; b.add(ok, -1); } } else { b.add(zip[q[i]] + 1, 1); } } return 0; }