HDU 6521 Party

时间:2021-08-29 02:42:25

6521

思路:

线段树玄学剪枝, 俗称吉司机线段树。

代码:

#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 << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head const int N = 5e5 + ;
LL sum[N<<];
int mn[N<<];
int n, m, l, r;
void push_up(int rt) {
mn[rt] = min(mn[rt<<], mn[rt<<|]);
sum[rt] = sum[rt<<] + sum[rt<<|];
}
void build(int rt, int l, int r) {
if(l == r) {
sum[rt] = ;
mn[rt] = l;
return ;
}
int m = l+r >> ;
build(ls);
build(rs);
push_up(rt);
}
LL query(int L, int R, int rt, int l, int r) {
LL ans = ;
if(mn[rt] >= R) return ;
if(l == r) {
ans = R-mn[rt];
mn[rt] = R;
return ans;
}
int m = l+r >> ;
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(, , n);
for (int i = ; i <= m; ++i) {
scanf("%d %d", &l, &r);
printf("%lld\n", query(l, r, , , n));
}
}
return ;
}