有一款在线游戏,共有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;
}
标签:cur,int,rep,游戏,abc221D,mp,ans,在线
From: https://www.cnblogs.com/chenfy27/p/18069312