2

100 阅读1分钟

#include <bits/stdc++.h> #define int long long

using namespace std;

const int N = 200010; int no[N], ye[N]; map<int, int> id; int now[N]; vector cf[N], df[N]; map<int, int> bf[N]; int ans = 0, hh = 0;

priority_queue<pair<int, int>> pq[N];

int idx(int x) { if (!id[x]) id[x] = ++ hh; return id[x]; }

signed main() { ios::sync_with_stdio(false); cin.tie(0);

int g; cin >> g;
for (int i = 0; i < g; i ++)
{
    int t, u;
    cin >> t >> u;
    t = idx(t);
    now[t] += u;
}

int n; cin >> n;
queue<int> q;
for (int i = 0; i < n; i ++)
{
    cin >> no[i];
    int m = no[i];
    for (int j = 0; j < m; j ++)
    {
        int a, b; cin >> a >> b;
        a = idx(a);
        if (now[a] >= b) no[i] --;
        else
        {
            bf[i][a] = b;
            pq[a].push({b, i});
        }
    }
    if (!no[i]) q.push(i);

    cin >> ye[i];
    for (int j = 0; j < ye[i]; j ++)
    {
        int c, d; cin >> c >> d;
        c = idx(c);
        cf[i].push_back(c);
        df[i].push_back(d);
    }
}

while (!q.empty())
{
    int t = q.front(); q.pop();
    ans ++;
    for (int i = 0; i < ye[t]; i ++)
    {
        int c = cf[t][i];
        int d = df[t][i];
        now[c] += d;
        while (!pq[c].empty())
        {
            auto [x, y] = pq[c].top();
            if (x <= now[c])
            {
                pq[c].pop();
                no[y] --;
                if (!no[y]) q.push(y);
            }
            else break;
        }
    }
}
cout << ans;
return 0;

}