# zkw线段树解决区间rmq

zkw线段树具体内容请百度统计的力量（这是他讲的时候所用的ppt的名字）

*差分是化绝对为相对的重要手段
*标记永久化后就是值，只不过这种值是相对的
*计算答案时可以利用从节点到根部的信息

#include <bits/stdc++.h>

#define up(i, lower, upper) for(int i = lower; i < upper; i++)
#define down(i, lower, upper) for(int i = upper-1; i >= lower; i--)

using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef __int64 ll;
typedef unsigned __int64 ull;

const double pi = acos(-1);
const double eps = 1.0e-9;

template<class T>
T x = 0, tmp = 1; char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return c;
}

template <class T>
inline void write(T n) {
if(n < 0) {
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n) {
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}

//---------------------------------------------------------
struct SegTree {
ll tree[550000];
int m;

void build(int len) {
m = 1;
memset(tree, 0, sizeof tree);
while(m-1 <= len) m*=2;
down(i, 1, m) tree[i] = min(tree[(i<<1)], tree[(i<<1)+1]);
down(i, 1, len+m+1) tree[i] = tree[i] - tree[i>>1];
}

void update(int l, int r, int val) {
ll tmp;
for(l += m-1, r += m+1; l^r^1; l>>=1, r>>=1) {
if(~l&1) tree[l^1]+=val;
if(r&1) tree[r^1]+=val;
tmp = min(tree[l], tree[l^1]), tree[l] -= tmp, tree[l^1] -= tmp, tree[l>>1] += tmp;
tmp = min(tree[r], tree[r^1]), tree[r] -= tmp, tree[r^1] -= tmp, tree[r>>1] += tmp;
}
for (;l!=1;l>>=1)
tmp = min(tree[l],tree[l^1]), tree[l] -= tmp, tree[l^1] -= tmp, tree[l>>1] += tmp;
}

ll query(int l, int r) {
ll lAns = 0, rAns = 0;
l+=m, r+=m;
if(l != r) {
for(; l^r^1; l>>=1, r>>=1) {
lAns += tree[l], rAns += tree[r];
if(~l&1) lAns = min(lAns, tree[l^1]);
if(r&1) rAns = min(rAns, tree[r^1]);
}
}
ll ans = min(lAns+tree[l], rAns+tree[r]);
while(l > 1) ans += tree[l>>=1];
return ans;
}
};

SegTree a;

int main() {
int len, n, m, l, r;
a.build(len);
while(n--) {
if(l > r) a.update(l+1, len, m), a.update(1, r+1, m);
else a.update(l+1, r+1, m);
}
else {
if(l > r) write(min(a.query(l+1, len), a.query(1, r+1)));
else write(a.query(l+1, r+1));
puts("");
}
}
return 0;
}