Advertisement
HellKitsune

Untitled

Jul 10th, 2014
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <string>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <map>
  10. #include <set>
  11. #include <list>
  12. #include <queue>
  13. #include <stack>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. #define pb push_back
  19. #define mp make_pair
  20. #define REP(i, n) for (int i = 0; i < (int)(n); i++)
  21. #define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
  22. typedef long long LL;
  23. typedef pair<int, int> PII;
  24.  
  25. struct Vertex {
  26.     Vertex *left, *right;
  27.     LL x, y;
  28.  
  29.     Vertex(LL x = 0, LL y = 0) : left(NULL), right(NULL), x(x), y(y) {}
  30.     Vertex(Vertex *l, Vertex *r, LL x = 0, LL y = 0) : left(l), right(r), x(x), y(y) {}
  31. };
  32.  
  33. const LL INF = 1e10;
  34.  
  35. int n, m;
  36. Vertex *t[100001];
  37. int a[100000];
  38.  
  39. Vertex *build_st(int L, int R) {
  40.     if (L == R) return new Vertex(a[L]);
  41.     int mid = (L + R) >> 1;
  42.     return new Vertex(build_st(L, mid), build_st(mid + 1, R));
  43. }
  44.  
  45. Vertex *update_st(Vertex *t, int L, int R, int l, int r, LL x, LL y) {
  46.     if (l == L && r == R)
  47.         return new Vertex(t->left, t->right, min(t->x + x, INF), t->y + y);
  48.     int mid = (L + R) >> 1;
  49.     Vertex *newl = t->left, *newr = t->right;
  50.     if (l <= mid)
  51.         newl = update_st(t->left, L, mid, l, min(r, mid), x, y);
  52.     if (r >= mid + 1)
  53.         newr = update_st(t->right, mid + 1, R, max(l, mid + 1), r, x + max(0, mid - l + 1) * y, y);
  54.     return new Vertex(newl, newr, t->x, t->y);
  55. }
  56.  
  57. LL query_st(Vertex *t, int L, int R, int pos, LL x, LL y) {
  58.     if (L == R) return t->x + x;
  59.     int mid = (L + R) >> 1;
  60.     if (pos <= mid) return query_st(t->left, L, mid, pos, x + t->x, y + t->y);
  61.     return query_st(t->right, mid + 1, R, pos, min(x + t->x + (R - mid) * (y + t->y), INF), y + t->y);
  62. }
  63.  
  64. int main() {
  65.     scanf("%d", &n);
  66.     REP(i, n) scanf("%d", a + i);
  67.     int R = 1;
  68.     while (R < n) R <<= 1;
  69.     --R;
  70.     t[0] = build_st(0, R);
  71.     scanf("%d", &m);
  72.     for (int query = 1; query <= m; ++query) {
  73.         int l, r, x, y;
  74.         scanf("%d%d%d%d", &l, &r, &x, &y), --l, --r;
  75.         t[query] = update_st(t[query - 1], 0, R, l, r, x, y);
  76.     }
  77.     REP(i, n) {
  78.         int l = 0, r = m + 1, mid;
  79.         while (l < r) {
  80.             mid = (l + r) >> 1;
  81.             if (query_st(t[mid], 0, R, i, 0, 0) < 0)
  82.                 l = mid + 1;
  83.             else
  84.                 r = mid;
  85.         }
  86.         if (l == m + 1) printf("-1 ");
  87.         else printf("%d ", l);
  88.     }
  89.     printf("\n");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement