QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#718575 | #7903. Computational Intelligence | retired_midlights | TL | 0ms | 0kb | C++14 | 1.6kb | 2024-11-06 20:53:30 | 2024-11-06 20:53:31 |
answer
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)a; i <= (int)b; i ++)
#define per(i, a, b) for(int i = (int)a; i >= (int)b; i --)
#define ll long long
using namespace std;
const int maxn = 100010;
int n, m, a[maxn], b[maxn], t[maxn], nxt[maxn];
ll x[maxn];
void solve() {
cin >> n >> m;
rep(i, 1, n) cin >> a[i], b[i] = a[i];
priority_queue < pair < int, int > > pq;
map < int, int > mp;
rep(i, 1, m) {
cin >> x[i] >> t[i];
pq.push(make_pair(-i, t[i]));
}
per(i, m, 1) {
if(mp.find(x[i]) != mp.end()) nxt[i] = mp[x[i]], mp[x[i]] = i;
else mp[x[i]] = i, nxt[i] = m + 1;
}
rep(i, 1, n) pq.push(make_pair(-(m + 1), i));
ll cur = 0, num = 1;
x[m + 1] = 1e18;
while(!pq.empty()) {
if(pq.top().first > -num) pq.pop();
auto u = pq.top();
while(cur + b[u.second] < x[num]) {
cur += b[u.second];
b[u.second] = 0;
pq.pop();
if(pq.empty()) {
cout << cur << '\n';
return;
}
u = pq.top();
}
b[u.second] -= x[num] - cur;
cur = x[num];
b[t[num]] = a[t[num]];
// rep(i, 1, n) cerr << b[i] << " \n"[i == n];
if(nxt[num] < -u.first) {
// cerr << "add" << '\n';
pq.push(make_pair(-nxt[num], t[num]));
}
num ++;
}
}
int main() {
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
ios :: sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while(T --) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 1 1 1