Advertisement
Guest User

D.cpp

a guest
Mar 23rd, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<memory.h>
  4.  
  5. using namespace std;
  6. typedef long long ll;
  7. const int N = 100000 + 10;
  8. const int sq = 333;
  9.  
  10. int res[N], a[N], b[N], l[N], r[N];
  11. ll x[N], y[N], xx[N], yy[N], need[N];
  12.  
  13. int main()
  14. {
  15.     ios_base::sync_with_stdio(true);
  16.     freopen("jam.in", "r", stdin);
  17.     freopen("jam.out", "w", stdout);
  18.     int n;
  19.     cin >> n;
  20.     for (int i = 1; i <= n; i++) cin >> a[i];
  21.     for (int i = 1; i <= n; i++) cin >> b[i];
  22.     for (int i = 1; i <= n; i++) res[i] = -1;
  23.     for (int i = 1; i <= n; i++)
  24.     {
  25.         need[i] = b[i] - a[i];
  26.         if (need[i] <= 0) res[i] = 0;
  27.     }
  28.     int m;
  29.     cin >> m;
  30.     for (int i = 1; i <= m; i++) cin >> l[i] >> r[i] >> x[i] >> y[i];
  31.     for (int lf = 1; lf <= m; lf += sq)
  32.     {
  33.         int rh = std::min(lf + sq - 1, m);
  34.         memset(xx, 0, sizeof(xx));
  35.         memset(yy, 0, sizeof(yy));
  36.         for (int i = lf; i <= rh; i++)
  37.         {
  38.             xx[l[i]] += x[i] - y[i];
  39.             yy[l[i]] += y[i];
  40.             xx[r[i] + 1] -= x[i] + y[i] * (r[i] - l[i]);
  41.             yy[r[i] + 1] -= y[i];
  42.         }
  43.         ll sum = 0, coef = 0;
  44.         for (int i = 1; i <= n; i++)
  45.         {
  46.             coef += yy[i];
  47.             sum += xx[i] + coef;
  48.             if (res[i] != -1) continue;
  49.             if (need[i] <= sum)
  50.             for (int j = lf; j <= rh; j++)
  51.             {
  52.                 if (!(l[j] <= i && i <= r[j])) continue;
  53.                 need[i] -= x[j] + y[j] * (i - l[j]);
  54.                 if (need[i] <= 0) { res[i] = j; break; }
  55.             }
  56.             else need[i] -= sum;
  57.         }
  58.     }
  59.     for (int i = 1; i <= n; i++) cout << res[i] << " ";
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement