有一款線上遊戲,共有n名註冊玩家,第i名玩家從第a[i]天開始上線,持續線上b[i]天。現在要統計線上人數為x[i]的天數,其中1<=x[i]<=n
。
1<=n<=2E5; 1<=a[i],b[i]<=1E9;
先按差分更新,然後求字首和還原,再遍歷一次統計答案,時間複雜度O(n)。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a; i<=b; i++)
#define per(i,a,b) for(int i=b; i>=a; i--)
int n, ans[200001];
map<int,int> mp;
void solve() {
cin >> n;
rep(i,1,n) {
int a, b;
cin >> a >> b;
mp[a] += 1;
mp[a+b] -= 1;
}
int cur = 0;
for (auto &[k,v] : mp) {
v += cur;
cur = v;
}
int K = 0, V = 0;
for (auto &[k,v] : mp) {
if (V) ans[V] += k - K;
K = k;
V = v;
}
rep(i,1,n) cout << ans[i] << " ";
cout << "\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}