Submission #409502

# Submission time Handle Problem Language Result Execution time Memory
409502 2021-05-21T00:49:19 Z 534351 Dungeon 3 (JOI21_ho_t5) C++17
0 / 100
4000 ms 3432 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

const int MAXN = 2e5 + 13;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, Q;
ll A[MAXN], B[MAXN];

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> Q;
    FOR(i, 0, N)
    {
        cin >> A[i];
    }
    FOR(i, 0, N)
    {
        cin >> B[i];
    }
    while(Q--)
    {
        int l, r; ll k, ans = 0;
        cin >> l >> r >> k;
        l--; r -= 2;
        ll c = 0;
        //cost, how much you have of it.
        set<pll> s; ll tot = 0;
        FORD(i, r + 1, l)
        {
            ll cnt = A[i], cost = B[i];
            if (cnt > k)
            {
                ans = -1;
                break;
            }
            s.insert({cost, k});
            tot += k;
            //find out minimum # of money it takes to raise cnt.
            while(cnt > 0)
            {
                auto p = *s.begin(); s.erase(s.begin());
                ll num = min(p.se, cnt);
                cnt -= num;
                tot -= num;
                p.se -= num;
                if (p.se != 0)
                {
                    s.insert(p);
                }
                // cerr << "+= " << p.fi << '*' << num << endl;
                ans += p.fi * num;
            }
            cnt = k - A[i];
            while(tot > cnt)
            {
                auto p = *prev(s.end()); s.erase(prev(s.end()));
                ll num = min(p.se, tot - cnt);
                tot -= num;
                p.se -= num;
                if (p.se != 0)
                {
                    s.insert(p);
                }
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:61:12: warning: unused variable 'c' [-Wunused-variable]
   61 |         ll c = 0;
      |            ^
# Verdict Execution time Memory Grader output
1 Incorrect 656 ms 388 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4075 ms 1232 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4097 ms 3432 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 656 ms 388 KB Output isn't correct
2 Halted 0 ms 0 KB -