提交时间:2020-11-16 18:53:43
运行 ID: 1887
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1000010; int n, m, sum, ans[N], s[N]; inline void calc(int x, int op){ for(int l = 1, r;l <= x;l = r + 1){ r = x / (x / l); // [l, r]所有数i, x/i == l/i ans[l] += op * x / l; ans[r + 1] -= op * x / l; //printf("(%lld,%lld)%c=%lld\n",l,r,op==1?'+':'-',x/l); } } struct node{ int num, L, R; }q1[N], q2[N]; int cnt1, cnt2; inline void Minus(int x, int y){ cnt1 = cnt2 = 0; for(int l = 1, r;l <= x;l = r + 1){ r = x / (x / l); q1[++cnt1] = (node){x / l, l, r}; } for(int l = 1, r;l <= x;l = r + 1){ r = y / (y / l); q2[++cnt2] = (node){y / l, l, r}; } int now = 0; for(int i = 1;i <= cnt1;i++){ while(now < cnt2 && q2[now + 1].num <= q1[i].num) ++now; if(q2[now].num == q1[i].num){ int l = max(q1[i].L, q2[now].L), r = min(q1[i].R, q2[now].R); if(r >= l) ++s[l], --s[r + 1]; } } ++s[max(x, y) + 1]; } signed main() { ios::sync_with_stdio(false); cin >> n >> m; for(int i = 1;i <= m;i++){ int l, r; cin >> l >> r; calc(r - 1, 1); calc(l - 1, -1); Minus(l - 1, r - 1); sum += r - l; } for(int i = 1;i <= n;i++) ans[i] += ans[i - 1], s[i] += s[i - 1]; for(int i = 1;i <= n;i++){ ans[i] = ans[i] * (1 - i) + sum + i * m; s[i] *= (1 - i); } for(int i = 1;i <= n;i++) cout << ans[i] + s[i] << " "; return 0; }