思路:
线段树玄学剪枝, 俗称吉司机线段树。
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r //#define mp make_pair #define pb push_back #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define pdi pair<double, int> #define pdd pair<double, double> #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << " "; #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //head const int N = 5e5 + 5; LL sum[N<<2]; int mn[N<<2]; int n, m, l, r; void push_up(int rt) { mn[rt] = min(mn[rt<<1], mn[rt<<1|1]); sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void build(int rt, int l, int r) { if(l == r) { sum[rt] = 0; mn[rt] = l; return ; } int m = l+r >> 1; build(ls); build(rs); push_up(rt); } LL query(int L, int R, int rt, int l, int r) { LL ans = 0; if(mn[rt] >= R) return 0; if(l == r) { ans = R-mn[rt]; mn[rt] = R; return ans; } int m = l+r >> 1; if(L <= m) ans += query(L, R, ls); if(R > m) ans += query(L, R, rs); push_up(rt); return ans; } int main() { while(~scanf("%d %d", &n, &m)) { build(1, 1, n); for (int i = 1; i <= m; ++i) { scanf("%d %d", &l, &r); printf("%lld ", query(l, r, 1, 1, n)); } } return 0; }