跳到正文

个人记录

lljiua

cn_miller约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年8月18日
收录
2026年8月18日
更新
2026年8月18日
篇幅
约 3 分钟

版本

  1. v1当前

#include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int MAXN = 1000005; ll a[MAXN * 2];
ll points[MAXN * 2]; ll diff[MAXN * 2];
ll pref[MAXN * 2];
int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < 2 * n; ++i)cin >> a[i]; int q; cin >> q; ll query_x[MAXN], query_y[MAXN]; for (int i = 0; i < q; ++i)cin >> query_x[i] >> query_y[i]; int tot = 0; for (int i = 0; i < n; ++i) { points[tot++] = a[2 * i]; points[tot++] = a[2 * i + 1]; } for (int i = 0; i < q; ++i) { points[tot++] = query_x[i]; points[tot++] = query_y[i]; } sort(points, points + tot); int m = unique(points, points + tot) - points; fill(diff, diff + m + 1, 0); for (int i = 0; i < n; ++i) { ll L = a[2 * i]; ll R = a[2 * i + 1]; int L_idx = lower_bound(points, points + m, L) - points; int R_idx = lower_bound(points, points + m, R) - points; diff[L_idx] += 1; diff[R_idx] -= 1; } pref[0] = 0; ll cur_state = 0; for (int i = 0; i < m; ++i) { cur_state += diff[i]; ll len = (cur_state > 0) ? (points[i + 1] - points[i]) : 0; pref[i + 1] = pref[i] + len; } for (int i = 0; i < q; ++i) { ll x = query_x[i]; ll y = query_y[i]; int lx = lower_bound(points, points + m, x) - points; int ly = lower_bound(points, points + m, y) - points; ll ans = pref[ly] - pref[lx]; cout << ans << “\n”; } return 0; }

原文