結果

問題 No.618 labo-index
ユーザー しらっ亭しらっ亭
提出日時 2017-12-18 00:28:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 6,000 ms
コード長 11,229 bytes
コンパイル時間 1,429 ms
コンパイル使用メモリ 128,568 KB
実行使用メモリ 8,816 KB
最終ジャッジ日時 2024-05-09 11:02:08
合計ジャッジ時間 4,669 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 62 ms
5,376 KB
testcase_20 AC 72 ms
5,504 KB
testcase_21 AC 62 ms
5,376 KB
testcase_22 AC 67 ms
5,376 KB
testcase_23 AC 65 ms
5,376 KB
testcase_24 AC 64 ms
5,376 KB
testcase_25 AC 70 ms
5,376 KB
testcase_26 AC 67 ms
5,376 KB
testcase_27 AC 65 ms
5,504 KB
testcase_28 AC 59 ms
5,376 KB
testcase_29 AC 66 ms
5,504 KB
testcase_30 AC 60 ms
5,376 KB
testcase_31 AC 64 ms
5,504 KB
testcase_32 AC 67 ms
5,376 KB
testcase_33 AC 67 ms
5,504 KB
testcase_34 AC 152 ms
8,816 KB
testcase_35 AC 177 ms
8,812 KB
testcase_36 AC 51 ms
6,124 KB
testcase_37 AC 106 ms
6,256 KB
testcase_38 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'void Treap<Val>::insert_b(Val) [with Val = long long int]',
    inlined from 'void Main()' at main.cpp:389:15:
main.cpp:305:39: warning: 'void operator delete(void*, std::size_t)' called on unallocated object 'pool' [-Wfree-nonheap-object]
  305 |                 root = insert_b(root, new node(val));
      |                                       ^~~~~~~~~~~~~
main.cpp: In function 'void Main()':
main.cpp:103:37: note: declared here
  103 |                         static Node pool[500010];
      |                                     ^~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <array>
#include <unordered_map>
#include <complex>
#include <deque>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <chrono>
#include <random>
#include <numeric>
#include <tuple>
#include <cstring>
using namespace std;

#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL<<(n))
#define sz(x) ((int)(x).size())
#define TEN(n) ((ll)(1e##n))
#define fst first
#define snd second

string DBG_DLM(int &i){return(i++==0?"":", ");}
#define DBG_B(exp){int i=0;os<<"{";{exp;}os<<"}";return os;}
template<class T>ostream&operator<<(ostream&os,vector<T>v);
template<class T>ostream&operator<<(ostream&os,set<T>v);
template<class T>ostream&operator<<(ostream&os,queue<T>q);
template<class T>ostream&operator<<(ostream&os,priority_queue<T>q);
template<class T,class K>ostream&operator<<(ostream&os,pair<T,K>p);
template<class T,class K>ostream&operator<<(ostream&os,map<T,K>mp);
template<class T,class K>ostream&operator<<(ostream&os,unordered_map<T,K>mp);
template<int I,class TPL>void DBG(ostream&os,TPL t){}
template<int I,class TPL,class H,class...Ts>void DBG(ostream&os,TPL t){os<<(I==0?"":", ")<<get<I>(t);DBG<I+1,TPL,Ts...>(os,t);}
template<class T,class K>void DBG(ostream&os,pair<T,K>p,string delim){os<<"("<<p.first<<delim<<p.second<<")";}
template<class...Ts>ostream&operator<<(ostream&os,tuple<Ts...>t){os<<"(";DBG<0,tuple<Ts...>,Ts...>(os,t);os<<")";return os;}
template<class T,class K>ostream&operator<<(ostream&os,pair<T,K>p){DBG(os,p,", ");return os;}
template<class T>ostream&operator<<(ostream&os,vector<T>v){DBG_B(forr(t,v){os<<DBG_DLM(i)<<t;});}
template<class T>ostream&operator<<(ostream&os,set<T>s){DBG_B(forr(t,s){os<<DBG_DLM(i)<<t;});}
template<class T>ostream&operator<<(ostream&os,queue<T>q){DBG_B(for(;q.size();q.pop()){os<<DBG_DLM(i)<<q.front();});}
template<class T>ostream&operator<<(ostream&os,priority_queue<T>q){DBG_B(for(;q.size();q.pop()){os<<DBG_DLM(i)<<q.top();});}
template<class T,class K>ostream&operator<<(ostream&os,map<T,K>m){DBG_B(forr(p,m){os<<DBG_DLM(i);DBG(os,p,"->");});}
template<class T,class K>ostream&operator<<(ostream&os,unordered_map<T,K>m){DBG_B(forr(p,m){os<<DBG_DLM(i);DBG(os,p,"->");});}
#define DBG_OVERLOAD(_1,_2,_3,_4,_5,_6,macro_name,...)macro_name
#define DBG_LINE(){char s[99];sprintf(s,"line:%3d | ",__LINE__);cerr<<s;}
#define DBG_OUTPUT(v){cerr<<(#v)<<"="<<(v);}
#define DBG1(v,...){DBG_OUTPUT(v);}
#define DBG2(v,...){DBG_OUTPUT(v);cerr<<", ";DBG1(__VA_ARGS__);}
#define DBG3(v,...){DBG_OUTPUT(v);cerr<<", ";DBG2(__VA_ARGS__);}
#define DBG4(v,...){DBG_OUTPUT(v);cerr<<", ";DBG3(__VA_ARGS__);}
#define DBG5(v,...){DBG_OUTPUT(v);cerr<<", ";DBG4(__VA_ARGS__);}
#define DBG6(v,...){DBG_OUTPUT(v);cerr<<", ";DBG5(__VA_ARGS__);}
#define DEBUG0(){DBG_LINE();cerr<<endl;}
#ifdef LOCAL
#define out(...){DBG_LINE();DBG_OVERLOAD(__VA_ARGS__,DBG6,DBG5,DBG4,DBG3,DBG2,DBG1)(__VA_ARGS__);cerr<<endl;}
#else
#define out(...)
#endif

using ll=long long;
using pii=pair<int,int>;using pll=pair<ll,ll>;using pil=pair<int,ll>;using pli=pair<ll,int>;
using vs=vector<string>;using vvs=vector<vs>;using vvvs=vector<vvs>;
using vb=vector<bool>;using vvb=vector<vb>;using vvvb=vector<vvb>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template<class A,class B>bool amax(A&a,const B&b){return b>a?a=b,1:0;}
template<class A,class B>bool amin(A&a,const B&b){return b<a?a=b,1:0;}
ll ri(){ll l;cin>>l;return l;} string rs(){string s;cin>>s;return s;}

template <class Val> struct Treap {
	struct Node {
		static Val comp(const Val& l, const Val& r) { return min(l, r); };

		Val v;
		int priority;
		Node *lef, *rig;
		int size_;
		Val summary;

		Node() {}
		Node(Val v) : v(v), priority(xor128()), lef(nullptr), rig(nullptr), size_(1), summary(v) {}

		void *operator new(std::size_t) {
			static Node pool[500010];
			static int p = 0;
			return pool + p++;
		}

		int size() const { return size_;}
		string to_string() const { string res; to_string(res); return res; }

		void to_string(string &res) const {
			res += "Node [v="; res += std::to_string(v);
			res += ", size="; res += std::to_string(size_);
			res += "]";
		}

		static int xor128() {
			static random_device rnd;
			static int x = 123456789, y = 362436069, z = 521288629, w = rnd();
			int t = x ^ (x << 11);
			x = y;
			y = z;
			z = w;
			w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
			return w;
		}

		static void to_string(const Node* a, string &res, int indent) {
			if (a == nullptr) return;
			to_string(a->lef, res, indent + 2);
			for (int i = 0; i < indent; i++) res += ' ';
			a->to_string(res);
			res += '\n';
			to_string(a->rig, res, indent + 2);
		}

		static Node* update(Node* a) {
			if (a == nullptr) return nullptr;
			a->size_ = 1 + size(a->lef) + size(a->rig);

			a->summary = a->v;
			if (a->lef != nullptr) a->summary = comp(a->summary, a->lef->summary);
			if (a->rig != nullptr) a->summary = comp(a->summary, a->rig->summary);
			return a;
		}

		static int size(const Node* x) { return x == nullptr ? 0 : x->size_; }

		static vector<Node*> nodes(Node* a) {
			vector<Node*> ret(size(a));
			nodes(a, ret, 0, size(a));
			return ret;
		}

		static void nodes(Node* a, vector<Node*> &ns, int L, int R) {
			if (a == nullptr) return;
			nodes(a->lef, ns, L, L + size(a->lef));
			ns[L + size(a->lef)] = a;
			nodes(a->rig, ns, R - size(a->rig), R);
		}

		static Node* merge(Node* a, Node* b) {
			if (b == nullptr) return a;
			if (a == nullptr) return b;
			if (a->priority > b->priority) {
				a->rig = merge(a->rig, b);
				return update(a);
			}
			else {
				b->lef = merge(a, b->lef);
				return update(b);
			}
		}

		static pair<Node*, Node*> split(Node* a, int k) {
			if (a == nullptr) return make_pair(nullptr, nullptr);
			if (k <= size(a->lef)) {
				auto s = split(a->lef, k);
				a->lef = s.second;
				s.second = update(a);
				return s;
			}
			else {
				auto s = split(a->rig, k - size(a->lef) - 1);
				a->rig = s.first;
				s.first = update(a);
				return s;
			}
		}
	};

	using node = Node;
	using np = node*;

	static np merge_technically(np a, np b) {
		if (node::size(a) > node::size(b)) swap(a, b);

		for (np cur : node::nodes(a)) {
			cur->lef = cur->rig = nullptr;
			b = insert_b(b, cur);
		}
		return b;
	}

	static pair<np, np> split_less(np a, Val v) {
		if (a == nullptr) return make_pair(nullptr, nullptr);
		if (a->v < v) {
			auto s = split_less(a->rig, v);
			a->rig = s.first;
			s.first = node::update(a);
			return s;
		}
		else {
			auto s = split_less(a->lef, v);
			a->lef = s.second;
			s.second = node::update(a);
			return s;
		}
	}

	static pair<np, np> split_leq(np a, Val v) {
		if (a == nullptr) return make_pair(nullptr, nullptr);
		if (a->v <= v) {
			auto s = split_leq(a->rig, v);
			a->rig = s.first;
			s.first = node::update(a);
			return s;
		}
		else {
			auto s = split_leq(a->lef, v);
			a->lef = s.second;
			s.second = node::update(a);
			return s;
		}
	}

	static np insert_k(np a, int k, const Val v) {
		auto lr = node::split(a, k);
		return node::merge(node::merge(lr.first, new node(v)), lr.second);
	}

	static np insert_b(np a, np v) {
		auto lr = split_less(a, v->v);
		return node::merge(node::merge(lr.first, v), lr.second);
	}

	static np erase_k(np a, int k) {
		auto lr = node::split(a, k);
		auto mr = node::split(lr.second, 1);
		return node::merge(lr.first, mr.second);
	}

	static np erase_b(np a, Val v) {
		auto lr = split_less(a, v);
		auto mr = node::split(lr.second, 1);
		return node::merge(lr.first, mr.second);
	}

	static np get_k(np a, int k) {
		while (a != nullptr) {
			if (k < node::size(a->lef)) {
				a = a->lef;
			}
			else if (k == node::size(a->lef)) {
				break;
			}
			else {
				k = k - node::size(a->lef) - 1;
				a = a->rig;
			}
		}
		return a;
	}

	public:
	np root;

	Treap() : root(nullptr) {}
	Treap(np root) : root(root) {}
	Treap(Treap l, Treap r) : root(node::merge(l.root, r.root)) {}

	string to_string() const { string res; node::to_string(root, res, 0); return res; }

	int size() const { return node::size(root); }

	pair<Treap, Treap> split_k(int k) {
		auto lr = node::split(root, k);
		return make_pair(Treap(lr.first), Treap(lr.second));
	}

	void insert_k(const Val val, int k) {
		root = insert_k(root, k, val);
	}

	void erase_k(int k) {
		root = erase_k(root, k);
	}

	Val operator[](int k) const {
		assert(size() > k);
		return get_k(root, k)->v;
	}

	void insert_b(const Val val) {
		root = insert_b(root, new node(val));
	}

	void erase_b(const Val val) {
		if (contains_b(val)) root = erase_b(root, val);
	}

	int count_less_b(const Val q) const {
		auto a = root;
		int lsize = 0;
		while (a != nullptr) {
			if (a->v < q) {
				lsize += node::size(a->lef) + 1;
				a = a->rig;
			}
			else {
				a = a->lef;
			}
		}
		return lsize;
	}

	int count_leq_b(const Val q) const {
		auto a = root;
		int lsize = 0;
		while (a != nullptr) {
			if (a->v <= q) {
				lsize += node::size(a->lef) + 1;
				a = a->rig;
			}
			else {
				a = a->lef;
			}
		}
		return lsize;
	}

	int count_b(const Val q) const {
		return count_leq_b(q) - count_less_b(q);
	}

	bool contains_b(const Val q) const {
		auto a = root;
		while (a != nullptr) {
			if (a->v == q) return true;
			else if (a->v < q) a = a->rig;
			else a = a->lef;
		}
		return false;
	}

	pair<Treap, Treap> split_less_b(const Val v) {
		auto lr = split_less(root, v);
		return make_pair(Treap(lr.first), Treap(lr.second));
	}

	pair<Treap, Treap> split_leq_b(const Val v) {
		auto lr = split_leq(root, v);
		return make_pair(Treap(lr.first), Treap(lr.second));
	}
};
template<class Val> ostream& operator<<(ostream& os, const Treap<Val>& treap) {
	vi tp;
	rep(i, sz(treap)) tp.emplace_back(treap[i]);
	os << tp; return os;
 }

void Main() {
	int Q = ri();

	vl A;
	ll sum = 0;

	using TP = Treap<ll>;

	TP tp;

	rep(i, Q) {
		ll t = ri(), x = ri();
		out(t, x);

		if (t == 1) {
			ll p = x - sum;
			A.emplace_back(p);
			tp.insert_b(p);
		}
		else if (t == 2) {
			ll p = A[x-1];
			tp.erase_b(p);
		}
		else if (t == 3) {
			sum += x;
		}
		else {
			assert(false);
		}

		out(sum, tp);

		auto check = [&](ll x) -> bool {
			ll p = x - sum;
			return sz(tp) - tp.count_less_b(p) >= x;
		};

		ll ok = 0;
		ll ng = Q+1;
		
		while (abs(ok - ng) > 1) {
			ll mid = (ok + ng) / 2;
			bool c = check(mid);
			if (c) ok = mid;
			else ng = mid;
		}

		cout << ok << '\n';
	}
}

signed main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	Main();
	return 0;
}
0