提交时间:2024-03-31 09:26:44

运行 ID: 27822

#include <bits/stdc++.h> using namespace std; int n,m,B,belong[1000006],ans,l[1000006],r[1000006]; inline void init (int n,int I) { B = I; ans = 0; for (int i = 1;i <= n;i++) { belong[i] = (i - 1) / B + 1; } } void query (int l,int r) { if (belong[l] == belong[r]) { ans += r - l + 1; return; } if (belong[r] > belong[l] + 1) { ans += belong[r] - belong[l] - 1; } int R = belong[l] * B,L = (belong[r] - 1) * B + 1; if (r >= L) { ans += r - L + 1; } if (R >= l) { ans += R - l + 1; } } int main () { scanf("%d%d",&n,&m); for(int i = 1;i <= m;i++) { scanf("%d%d",&l[i],&r[i]); } for (int I = 1;I <= n;I++) { init(n,I); for(int i = 1;i <= m;i++) { query(l[i],r[i]); } printf("%d\n",ans); } }