Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.52 KB | None | 0 0
  1. import std.stdio;
  2. import std.file;
  3. import std.algorithm;
  4. import std.typecons;
  5. import std.math;
  6. import std.random;
  7. import std.container;
  8. import std.string;
  9. import std.conv;
  10.  
  11. struct OrderedTree
  12. {
  13.     alias treap = node*;
  14.     alias pair = Tuple!(treap, "l", treap, "r");
  15.  
  16.     struct node
  17.     {
  18.         node* l = null, r = null;
  19.         int y, s, s_all;
  20.         long sum, x;
  21.  
  22.         this(long x)
  23.         {
  24.             this.x = this.sum = x;
  25.             this.y = uniform(0, 2000000000);
  26.             this.s = this.s_all = 1;
  27.         }
  28.     }
  29.  
  30.     int _s(treap t)
  31.     {
  32.         return (t ? t.s_all : 0);
  33.     }
  34.  
  35.     long _sum(treap t)
  36.     {
  37.         return (t ? t.sum : 0);
  38.     }
  39.  
  40.     treap _mend(treap t)
  41.     {
  42.         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);
  43.         return t;
  44.     }
  45.  
  46.     treap root = null;
  47.  
  48.     long total()
  49.     {
  50.         return _sum(root);
  51.     }
  52.  
  53.     void insert(long x)
  54.     {
  55.         auto p = _split(root, x - 1);
  56.         auto q = _split(p.r, x);
  57.         if (!q.l)
  58.             q.l = new node(x);
  59.         else
  60.             q.l.s++, q.l = _mend(q.l);
  61.  
  62.         root = _merge(p.l, _merge(q.l, q.r));
  63.     }
  64.  
  65.     int get(long sum)
  66.     {
  67.         return _get(root, sum);
  68.     }
  69.  
  70.     int _get(treap t, long sum)
  71.     {
  72.         if (_sum(t.r) >= sum)
  73.             return _get(t.r, sum);
  74.  
  75.         if (_sum(t.r) + t.s * t.x >= sum)
  76.             return _s(t.r) + cast(int)((sum - _sum(t.r) - 1) / t.x) + 1;
  77.  
  78.         return _get(t.l, sum - t.s * t.x - _sum(t.r)) + t.s + _s(t.r);
  79.     }
  80.  
  81.     int size()
  82.     {
  83.         return _s(root);
  84.     }
  85.  
  86.     treap _merge(treap u, treap v)
  87.     {
  88.         if (!u) return v;
  89.         if (!v) return u;
  90.  
  91.         if (u.y > v.y)
  92.         {
  93.             u.r = _merge(u.r, v);
  94.             return _mend(u);
  95.         } else
  96.         {
  97.             v.l = _merge(u, v.l);
  98.             return _mend(v);
  99.         }
  100.     }
  101.  
  102.     pair _split(treap t, long x)
  103.     {
  104.         if (!t)
  105.             return pair(t, t);
  106.  
  107.         if (x < t.x)
  108.         {
  109.             auto p = _split(t.l, x);
  110.             t.l = p.r;
  111.             return pair(p.l, _mend(t));
  112.         } else
  113.         {
  114.             auto p = _split(t.r, x);
  115.             t.r = p.l;
  116.             return pair(_mend(t), p.r);
  117.         }
  118.     }
  119. }
  120.  
  121. alias pair = Tuple!(long, "l", long, "r");
  122.  
  123. void main()
  124. {
  125.     File inf = File("artifact.in", "r");
  126.     File ouf = File("artifact.out", "w");
  127.  
  128.     int n = std.conv.to!int(inf.readln()[0..$ - 1]);
  129.  
  130.     long x, y;
  131.  
  132.     auto a = new pair[n];
  133.  
  134.     foreach(i; 0..n)
  135.     {
  136.         auto s = inf.readln()[0..$ - 1].split();
  137.  
  138.         a[i] = pair(std.conv.to!long(s[0]), std.conv.to!long(s[1]));
  139.     }
  140.  
  141.     sort!((x, y) => ((x.l + x.r < y.l + y.r) || ((x.l + x.r == y.l + y.r) && x.l > y.l)))(a);
  142.  
  143.     OrderedTree tree;
  144.  
  145.     int ans = cast(int)1e9;
  146.     foreach(i; 0..n)
  147.     {
  148.         if (tree.total() >= a[i].r)
  149.             ans = min(ans, tree.get(a[i].r) + 1);
  150.  
  151.         tree.insert(a[i].l);
  152.     }
  153.  
  154.     ouf.writeln((ans < cast(int)1e9) ? ans : -1);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement