QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#685831#9528. New Energy Vehiclewzl19371#TL 0ms3768kbC++142.1kb2024-10-28 21:30:192024-10-28 21:30:26

Judging History

This is the latest submission verdict.

  • [2024-10-28 21:30:26]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3768kb
  • [2024-10-28 21:30:19]
  • Submitted

answer

#include <bits/stdc++.h>
#define endl '\n'

#define rep(i, o, p) for(int i = o; i <= p; i ++ )

using namespace std;
typedef long long ll;

const ll INF = 1e17;

void sol(){
    int n, m; 
    cin >> n >> m;
    vector <ll> a(n + 5), pos(m + 5), c(m + 5), t(n + 5);
    map <ll, ll> mp, list;
    vector<ll> tmp(n + 5);
    set <ll> s;
    rep(i, 1, n) {
        cin >> a[i];
        t[i] = a[i];
    }
    rep(i, 1, m) {
        cin >> pos[i] >> c[i];
        mp[pos[i]] = c[i]; // pos x has y
        if(tmp[c[i]]) list[tmp[c[i]]] = pos[i];
        else s.insert(pos[i]);
        tmp[c[i]] = pos[i];
    }

    rep(i, 1, n) {
        if(!tmp[i]) s.insert(INF + i);
        mp[INF + i] = i;
    }

    ll ans = 0;
    rep(i, 1, m){
        while(*s.begin() < pos[i])  {
            ll a = *s.begin();
            s.erase(s.begin());
            if(list[a])
                s.insert(list[a]);
        }
        ll l = pos[i] - pos[i - 1];
        ll cnt = 0;
        auto it = *s.begin();
        bool flag = 1;
        while(1){
            //cout << 73;
           if(t[mp[it]] >= l ) {
      //      cout << mp[it] << endl;
         //   cout << "73";
            t[mp[it]] -= l;
            t[c[i]] = a[c[i]];
            if(!t[mp[it]]) {
                s.erase(it);
                if(list[it]) {
                    s.insert(list[it]);
                }
            }
            cnt += l;
             break;
           }
           else {
          //  cout << 2;
            l -= t[mp[it]];
            cnt += t[mp[it]];
            t[mp[it]] = 0;
            s.erase(it);
            if(list[it]) s.insert(list[it]);
            if(s.size()) it = *s.begin();
            else {
                flag = 0;
                break;
            }
           }
        }
        ans += cnt;
        if(flag == 0) break;
    }
    //cout << ans << t[1] << t[2] << endl;
    rep(i, 1, n) ans += t[i];
    cout << ans << '\n'; 
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while(T--) sol();
    return 0;
}
/*
2
3 1
3 3 3
8 1
2 2
5 2
1 2
2 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3768kb

input:

2
3 1
3 3 3
8 1
2 2
5 2
1 2
2 1

output:

12
9

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

6
3 2
2 2 2
6 1
7 1
2 2
3 3
2 1
6 2
2 3
2 2
5 1
7 2
9 1
2 2
3 3
2 1
6 2
1 1
999999999
1000000000 1
1 1
1000000000
1000000000 1

output:


result: