結果
| 問題 |
No.649 ここでちょっとQK!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-02-11 19:10:59 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 292 ms / 3,000 ms |
| コード長 | 4,292 bytes |
| コンパイル時間 | 2,553 ms |
| コンパイル使用メモリ | 210,320 KB |
| 最終ジャッジ日時 | 2025-01-05 08:20:04 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename Base>
class BinaryIndexedTree
{
public:
using T = typename Base::T;
using AbelGroup = Base;
BinaryIndexedTree(const int n) : data_num(n), size(1 << (__lg(2 * data_num - 1))), value(size + 1, AbelGroup::identity()) { assert(n > 0); }
BinaryIndexedTree(const vector<T>& val) : data_num(val.size()), size(1 << (__lg(2 * data_num - 1))), value(size + 1, AbelGroup::identity())
{
for (int i = 1; i <= data_num; i++) {
value[i] = val[i - 1];
}
for (int x = 1; x < size; x++) {
value[x + (x & -x)] = op(value[x + (x & -x)], value[x]);
}
}
T accumulate(const int a) const
{
assert(0 <= a and a < data_num);
int ind = a + 1;
T sum = AbelGroup::identity();
while (ind > 0) {
sum = op(sum, value[ind]);
ind &= ind - 1;
}
return sum;
}
T accumulate(const int l, const int r) const // [l,r)
{
assert(0 <= l and l < r and r <= data_num);
return op(accumulate(r - 1), op.inv(l == 0 ? AbelGroup::identity() : accumulate(l - 1)));
}
void add(const int a, const T& val)
{
assert(0 <= a and a < data_num);
int ind = a + 1;
while (ind <= size) {
value[ind] = op(value[ind], val);
ind += ind & (-ind);
}
}
void set(const int a, const T& val)
{
const int v = get(a);
add(a, op(val, op.inv(v)));
}
T get(const int a) const
{
assert(0 <= a and a < data_num);
return accumulate(a, a + 1);
}
int lowerBound(T w) const
{
if (w <= AbelGroup::identity()) {
return 0;
}
int x = 0;
for (int k = ((size == data_num) ? size : size / 2); k > 0; k /= 2) {
if (x + k <= size and value[x + k] < w) {
w = op(w, op.inv(value[x + k]));
x += k;
}
}
return x;
}
int upperBound(T w) const
{
if (w <= AbelGroup::identity()) {
return 0;
}
int x = 0;
for (int k = ((size == data_num) ? size : size / 2); k > 0; k /= 2) {
if (x + k <= size and value[x + k] <= w) {
w = op(w, op.inv(value[x + k]));
x += k;
}
}
return min(x, data_num);
}
private:
const int data_num;
const int size;
const AbelGroup op{};
vector<T> value;
};
template <typename T>
ostream& operator<<(ostream& os, const BinaryIndexedTree<T>& bit)
{
os << "[";
for (int i = 0; i < bit.data_num; i++) {
os << bit.get(i) << ",";
}
os << "]" << endl;
return os;
}
struct Sum {
using T = ll;
T operator()(const T& a, const T& b) const
{
return a + b;
}
T inv(const T& a) const
{
return -a;
}
static constexpr T identity()
{
return 0;
}
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int Q, K;
cin >> Q >> K;
using P = pair<int, ll>;
vector<P> query(Q);
vector<ll> v;
for (int q = 0; q < Q; q++) {
cin >> query[q].first;
if (query[q].first == 1) {
cin >> query[q].second;
v.push_back(query[q].second);
}
}
auto zip = v;
sort(zip.begin(), zip.end());
zip.erase(unique(zip.begin(), zip.end()), zip.end());
map<ll, int> mp;
const int size = zip.size();
if (size != 0) {
for (int i = 0; i < zip.size(); i++) {
mp[zip[i]] = i;
}
BinaryIndexedTree<Sum> bit(size);
for (int q = 0; q < Q; q++) {
if (query[q].first == 1) {
const int t = mp[query[q].second];
bit.add(t, 1);
} else {
if (bit.accumulate(size - 1) < K) {
cout << -1 << "\n";
} else {
const int l = bit.lowerBound(K);
cout << zip[l] << "\n";
bit.add(l, -1);
}
}
}
} else {
for (int q = 0; q < Q; q++) {
cout << -1 << endl;
}
}
return 0;
}