Advertisement
Guest User

problem

a guest
Jul 10th, 2014
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8.  
  9. const int N = 100000;
  10.  
  11. struct Query {
  12.     ll x, y;
  13.     int l, r;
  14. };
  15.  
  16. Query queries[N];
  17.  
  18. vector<int> queriesByL[N + 1];
  19. vector<int> queriesByR[N + 1];
  20.  
  21. int a[N];
  22.  
  23. pll tree[1 << 18];
  24.  
  25. #define root 0, m, 0
  26. #define lx 2*x+1
  27. #define rx 2*x+2
  28. #define lc l, m, lx
  29. #define rc m, r, rx
  30.  
  31. void treeAdd(int dest, ll first, ll second, int l, int r, int x) {
  32.     tree[x].first += first;
  33.     tree[x].second += second;
  34.     if (l + 1 != r) {
  35.         int m = (l + r) / 2;
  36.         if (dest < m) {
  37.             treeAdd(dest, first, second, lc);
  38.         } else {
  39.             treeAdd(dest, first, second, rc);
  40.         }
  41.     }
  42. }
  43.  
  44. int treeFind(ll goal, ll mult, int l, int r, int x) {
  45.     if (l + 1 == r) {
  46.         return l;
  47.     }
  48.     int m = (l + r) / 2;
  49.     if (tree[lx].first + tree[lx].second * mult >= goal) {
  50.         return treeFind(goal, mult, lc);
  51.     } else {
  52.         return treeFind(goal - tree[lx].first - tree[lx].second * mult, mult, rc);
  53.     }
  54. }
  55.  
  56. int main() {
  57.     // freopen("in.txt", "r", stdin);
  58.  
  59.     int n;
  60.     cin >> n;
  61.  
  62.     for (int i = 0; i < n; ++i) {
  63.         cin >> a[i];
  64.     }
  65.  
  66.     int m;
  67.     cin >> m;
  68.  
  69.     for (int i = 0; i < m; ++i) {
  70.         Query& q = queries[i];
  71.         cin >> q.l >> q.r >> q.x >> q.y;
  72.         q.l -= 1;
  73.         q.x -= (q.l - 1) * q.y;
  74.         queriesByL[q.l].push_back(i);
  75.         queriesByR[q.r].push_back(i);
  76.     }
  77.  
  78.     for (int i = 0; i < n; ++i) {
  79.         for (int index : queriesByR[i]) {
  80.             treeAdd(index, -queries[index].x, -queries[index].y, root);
  81.         }
  82.         for (int index : queriesByL[i]) {
  83.             treeAdd(index, +queries[index].x, +queries[index].y, root);
  84.         }
  85.  
  86.         if (a[i] >= 0) {
  87.             cout << 0 << ' ';
  88.             continue;
  89.         }
  90.         if (a[i] + tree[0].first + tree[0].second * i < 0) {
  91.             cout << -1 << ' ';
  92.             continue;
  93.         }
  94.         cout << treeFind(-a[i], i, root) + 1 << ' ';
  95.     }
  96.     cout << endl;
  97.  
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement