Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.file;
- import std.algorithm;
- import std.typecons;
- import std.math;
- import std.random;
- import std.container;
- import std.string;
- import std.conv;
- struct OrderedTree
- {
- alias treap = node*;
- alias pair = Tuple!(treap, "l", treap, "r");
- struct node
- {
- node* l = null, r = null;
- int y, s, s_all;
- long sum, x;
- this(long x)
- {
- this.x = this.sum = x;
- this.y = uniform(0, 2000000000);
- this.s = this.s_all = 1;
- }
- }
- int _s(treap t)
- {
- return (t ? t.s_all : 0);
- }
- long _sum(treap t)
- {
- return (t ? t.sum : 0);
- }
- treap _mend(treap t)
- {
- if (t) t.s_all = t.s + _s(t.l) + _s(t.r), t.sum = t.s * t.x + _sum(t.l) + _sum(t.r);
- return t;
- }
- treap root = null;
- long total()
- {
- return _sum(root);
- }
- void insert(long x)
- {
- auto p = _split(root, x - 1);
- auto q = _split(p.r, x);
- if (!q.l)
- q.l = new node(x);
- else
- q.l.s++, q.l = _mend(q.l);
- root = _merge(p.l, _merge(q.l, q.r));
- }
- int get(long sum)
- {
- return _get(root, sum);
- }
- int _get(treap t, long sum)
- {
- if (_sum(t.r) >= sum)
- return _get(t.r, sum);
- if (_sum(t.r) + t.s * t.x >= sum)
- return _s(t.r) + cast(int)((sum - _sum(t.r) - 1) / t.x) + 1;
- return _get(t.l, sum - t.s * t.x - _sum(t.r)) + t.s + _s(t.r);
- }
- int size()
- {
- return _s(root);
- }
- treap _merge(treap u, treap v)
- {
- if (!u) return v;
- if (!v) return u;
- if (u.y > v.y)
- {
- u.r = _merge(u.r, v);
- return _mend(u);
- } else
- {
- v.l = _merge(u, v.l);
- return _mend(v);
- }
- }
- pair _split(treap t, long x)
- {
- if (!t)
- return pair(t, t);
- if (x < t.x)
- {
- auto p = _split(t.l, x);
- t.l = p.r;
- return pair(p.l, _mend(t));
- } else
- {
- auto p = _split(t.r, x);
- t.r = p.l;
- return pair(_mend(t), p.r);
- }
- }
- }
- alias pair = Tuple!(long, "l", long, "r");
- void main()
- {
- File inf = File("artifact.in", "r");
- File ouf = File("artifact.out", "w");
- int n = std.conv.to!int(inf.readln()[0..$ - 1]);
- long x, y;
- auto a = new pair[n];
- foreach(i; 0..n)
- {
- auto s = inf.readln()[0..$ - 1].split();
- a[i] = pair(std.conv.to!long(s[0]), std.conv.to!long(s[1]));
- }
- sort!((x, y) => ((x.l + x.r < y.l + y.r) || ((x.l + x.r == y.l + y.r) && x.l > y.l)))(a);
- OrderedTree tree;
- int ans = cast(int)1e9;
- foreach(i; 0..n)
- {
- if (tree.total() >= a[i].r)
- ans = min(ans, tree.get(a[i].r) + 1);
- tree.insert(a[i].l);
- }
- ouf.writeln((ans < cast(int)1e9) ? ans : -1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement