提交时间:2020-11-16 10:32:13

运行 ID: 1822

#include <bits/stdc++.h> using namespace std; int B, n, m, ans, belong[5008]; void init() { 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]) { for(int i = l; i <= r; i++) ++ans; return; } for(int i = belong[l] + 1; i < belong[r]; i++) ++ans; int R = belong[l] * B, L = (belong[r] - 1) * B + 1; for(int i = L; i <= r; i++) ++ans; for(int i = l; i <= R; i++) ++ans; } int ll[5008], rr[5008]; int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= m; i++) { scanf("%d%d", &ll[i], &rr[i]); } for(B = 1; B <= n; B++) { init(); for(int i = 1; i <= m; i++) { query(ll[i], rr[i]); } cout << ans << " "; } return 0; }