Advertisement
Guest User

Juanzhang

a guest
Jun 5th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define nc getchar()
  5. typedef long long ll;
  6. const int maxn = 3e5 + 10;
  7. int n, m, k, a[maxn], ans[maxn];
  8. int q[maxn], q1[maxn], q2[maxn];
  9.  
  10. struct Query {
  11.   int l, r, x;
  12. } Q[maxn];
  13.  
  14. struct BIT {
  15.   ll c[maxn];
  16.   int T, t[maxn];
  17.   void clr() { T++; }
  18.   void add(int pos, int x) {
  19.     for (; pos <= m; pos += pos & -pos) {
  20.       t[pos] < T ? t[pos] = T, c[pos] = x : c[pos] += x;
  21.     }
  22.   }
  23.   ll query(int pos) {
  24.     ll res = 0;
  25.     for (; pos; pos &= pos - 1) {
  26.       res += t[pos] < T ? t[pos] = T, c[pos] = 0 : c[pos];
  27.     }
  28.     return res;
  29.   }
  30.   void upd(int pos) {
  31.     int l = Q[pos].l, r = Q[pos].r, x = Q[pos].x;
  32.     add(l, x), add(r + 1, -x);
  33.     if (l > r) add(1, x);
  34.   }
  35. } bit;
  36.  
  37. vector <int> tid[maxn];
  38.  
  39. int read() {
  40.   int x = 0;
  41.   char c = nc;
  42.   while (c < 48) c = nc;
  43.   while (c > 47) x = x * 10 + c - 48, c = nc;
  44.   return x;
  45. }
  46.  
  47. void divide(int l, int r, int ql, int qr) {
  48.   if (l > r) return;
  49.   if (ql == qr) {
  50.     for (int i = l; i <= r; i++) {
  51.       ans[q[i]] = ql;
  52.     }
  53.     return;
  54.   }
  55.   int mid = (ql + qr) >> 1;
  56.   for (int i = ql; i <= mid; i++) {
  57.     bit.upd(i);
  58.   }
  59.   int s1 = 0, s2 = 0;
  60.   for (int i = l; i <= r; i++) {
  61.     ll sum = 0;
  62.     for (int pos : tid[q[i]]) {
  63.       sum += bit.query(pos);
  64.       if (sum > a[q[i]]) break;
  65.     }
  66.     if (a[q[i]] <= sum) {
  67.       q1[++s1] = q[i];
  68.     } else {
  69.       q2[++s2] = q[i], a[q[i]] -= sum;
  70.     }
  71.   }
  72.   for (int i = 1; i <= s1; i++) {
  73.     q[l + i - 1] = q1[i];
  74.   }
  75.   for (int i = 1; i <= s2; i++) {
  76.     q[l + s1 + i - 1] = q2[i];
  77.   }
  78.   bit.clr();
  79.   divide(l, l + s1 - 1, ql, mid);
  80.   divide(l + s1, r, mid + 1, qr);
  81. }
  82.  
  83. int main() {
  84.   n = read(), m = read();
  85.   for (int i = 1; i <= m; i++) {
  86.     tid[read()].push_back(i);
  87.   }
  88.   for (int i = 1; i <= n; i++) {
  89.     a[i] = read(), q[i] = i;
  90.   }
  91.   k = read();
  92.   for (int i = 1; i <= k; i++) {
  93.     Q[i].l = read();
  94.     Q[i].r = read();
  95.     Q[i].x = read();
  96.   }
  97.   Q[++k] = Query{1, m, 1 << 30};
  98.   divide(1, n, 1, k);
  99.   for (int i = 1; i <= n; i++) {
  100.     if (ans[i] < k) {
  101.       printf("%d\n", ans[i]);
  102.     } else {
  103.       puts("NIE");
  104.     }
  105.   }
  106.   return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement