Submission #1344387

#TimeUsernameProblemLanguageResultExecution timeMemory
1344387thesen새로운 문제 (POI11_met)C++20
74 / 100
1222 ms50268 KiB
#include <bits/stdc++.h>
#define pb push_back
#define ll long
#define vll vector<ll>
#define vbool vector<bool>
#define pairll pair<ll,ll>
#define fi first
#define sc second
#define rever greater<ll>()
using namespace std;

void add(vll &tree, ll x, ll v){
    for(; x < tree.size(); x += (x&(-x))){
        tree[x] += v;
        if(tree[x] >= 1e9)break;
    }
}

ll q(vll &tree, ll x){
    ll sum = 0;
    for(; x > 0; x -= (x&(-x))){
        sum += tree[x];
        if(sum >= 1e9)break;
    }return sum;
}

void solve(){
    ll n, m; cin >> n >> m;

    vector<vll> station(n+1);
    for(ll i = 1; i <= m; i++){
        ll x; cin >> x;
        station[x].pb(i);
    }

    vll p(n+1);
    for(ll i = 1; i <= n; i++) cin >> p[i];

    ll k; cin >> k;
    vll l(k+2), r(k+2), a(k+2);
    for(ll i = 1; i <= k; i++){
        cin >> l[i] >> r[i] >> a[i];
    }
    l[k+1] = 1; r[k+1] = m; a[k+1] = 1e9;

    
    vector<vector<pairll>> mid(1);
    vll lef(n+1, 1), rig(n+1, k+1);
    for(ll i = 1; i <= n; i++){
        mid[0].pb({(lef[i]+rig[i])/2, i});
    }

    vll tree(m+2), res(n+1);
    ll lay = 0;
    while(true){
        if(mid[lay].size() == 0)break;
        sort(mid[lay].begin(), mid[lay].end());
        // cout << lay << endl;
        mid.pb({});

        for(ll i = 0; i <= m+1; i++)tree[i] = 0;
        
        ll cur = 0;
        for(ll i = 1; i <= k+1; i++){
            if(l[i] <= r[i]){
                add(tree, l[i], a[i]); add(tree, r[i]+1, -a[i]);
            }else{
                add(tree, 1, a[i]); add(tree, r[i]+1, -a[i]); add(tree, l[i], a[i]);
            }

            // cout << i << endl;
            // for(ll i = 1; i <= m; i++){
            //     cout<< q(tree, i) << ' ';
            // }cout << endl;

            // cout << ' ' << i << endl;

            while(cur < mid[lay].size()){
                ll md = mid[lay][cur].fi, ind = mid[lay][cur].sc;
                if(md > i)break;

                ll tot = 0;
                for(ll j : station[ind]){
                    tot += q(tree, j);
                    if(tot >= p[ind])break;
                    // cout << ind << ' ' << j << endl;
                }
                // cout << ' ' << ind << ' ' << tot << ' ' << p[ind] << endl;
                if(tot >= p[ind]){
                    rig[ind] = md;
                }else{
                    lef[ind] = md+1;
                }
                // cout << ' ' << lef[ind] << ' ' << rig[ind] << endl;
                if(lef[ind] == rig[ind])res[ind] = lef[ind];
                else{
                    mid[lay+1].pb({(lef[ind]+rig[ind])/2, ind});
                }
                cur++;
            }
            if(cur == mid[lay].size())break;
        }
        lay++;
    }

    for(ll i = 1; i <= n; i++){
        if(res[i] == k+1){
            cout << "NIE" << endl;
        }else{
            cout << res[i] << endl;
        }   
    }

}

int main(){
    // ios_base::sync_with_stdio(0); cin.tie(0);
    ll t = 1; //cin >> t;
    while(t--)solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...