This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
/*
const int MOD = ;
inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}
inline void add(int &x, int y) {
    x += y;
    mod(x);
}
inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}
inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}
*/
using namespace std;
const ll INF = 1e9;
const int MAXN = (int) 3e5;
ll aint[4 * MAXN + 1];
void update(int nod, int left, int right, int l, int r, int val) {
    if(l <= left && right <= r) {
        aint[nod] += val;
    }
    else {
        int mid = (left + right) / 2;
        if(l <= mid) update(2 * nod, left, mid, l, r, val);
        if(mid < r) update(2 * nod + 1, mid + 1, right, l, r, val);
    }
}
ll query(int nod, int left, int right, int pos) {
    if(left == right) {
        return aint[nod];
    }
    else {
        int mid = (left + right) / 2;
        ll ans = aint[nod];
        if(pos <= mid) ans += query(2 * nod, left, mid, pos);
        else ans += query(2 * nod + 1, mid + 1, right, pos);
        return ans;
    }
}
vector <int> pos[MAXN + 1];
int q[MAXN + 1];
int l[MAXN + 1], r[MAXN + 1], a[MAXN + 1];
int n, m, k;
pair <int, int> res[MAXN + 1];
inline void check(int step) {
    for(int i = 1; i <= 4 * m; i++) {
        aint[i] = 0;
    }
    sort(res + 1, res + n + 1);
    int p = 1;
    for(int i = 1; i <= n; i++) {
        while(p <= res[i].first + step && p <= k) {
            if(l[p] <= r[p]) {
                update(1, 1, m, l[p], r[p], a[p]);
            }
            else {
                update(1, 1, m, 1, r[p], a[p]);
                update(1, 1, m, l[p], m, a[p]);
            }
            p++;
        }
        ll cur = 0;
        for(auto it : pos[res[i].second]) {
            cur += query(1, 1, m, it);
            cur = min(cur, INF);
        }
        if(cur < q[res[i].second]) {
            res[i].first += step;
        }
    }
}
int sol[MAXN + 1];
int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(i = 1; i <= m; i++) {
        int o;
        cin >> o;
        pos[o].push_back(i);
    }
    for(i = 1; i <= n; i++) {
        cin >> q[i];
    }
    cin >> k;
    for(i = 1; i <= k; i++) {
        cin >> l[i] >> r[i] >> a[i];
    }
    for(i = 1; i <= n; i++) {
        res[i] = {-1, i};
    }
    for(int step = 1 << 18; step; step >>= 1) {
        check(step);
    }
    for(i = 1; i <= n; i++) {
        sol[res[i].second] = res[i].first + 1;
    }
    for(i = 1; i <= n; i++) {
        if(sol[i] == (1 << 19) - 1) {
            cout << "NIE\n";
        }
        else {
            cout << sol[i] << "\n";
        }
    }
    //cin.close();
    //cout.close();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |