求好心人帮忙卡常
查看原帖
求好心人帮忙卡常
804607
rainygame楼主2024/8/11 19:48
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200001

int n, m, q;
set<pair<int, int>> st[MAXN];
vector<int> T[MAXN], S[MAXN];

int gans(int x, int y){
    int res(0);
    for (int i(0), j(-1); i<T[x].size(); ++i){
        while (j+1 < (int)T[y].size() && T[y][j+1] <= T[x][i]) ++j;
        if (j == -1) continue;
        res += (i & 1 ? 1 : -1)*(S[y][j]+(j & 1 ? 0 : T[x][i]-T[y][j]));
    }
    return res;
}

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

    cin >> n >> m; for (int i(1), t, x; i<=m; ++i) cin >> t >> x, T[x].push_back(t);
    for (int i(1); i<=n; ++i){
        if (T[i].empty()) continue;
        S[i].resize(T[i].size()); S[i][0] = 0;
        for (int j(1); j<T[i].size(); ++j){
            S[i][j] = S[i][j-1]+(j & 1 ? T[i][j]-T[i][j-1] : 0);
        }
    }
    cin >> q;
    for (int a, b; q; --q){
        cin >> a >> b; auto it(st[a].lower_bound({b, 0}));
        if (it->first == b){cout << it->second << '\n'; continue;}
        int k(gans(a, b)); st[a].insert({b, k}); cout << k << '\n';
    }

    return 0;
}

复杂度应该是对的,但是 TLE 了 4 个点。链接

2024/8/11 19:48
加载中...