【模板】线段树(min,add)

const int maxn = 5000010;
int a[maxn];
#define lc(x) x<<1
#define rc(x) x<<1|1
il int min(int a, int b)
{
 return a > b ? b : a;
}
struct tree
{
 int a;
 int l, r;
 int tag;
 tree()
 {
  tag = 0;
  l = 0;
  r = 0;
  a = 0;
 }
}node[maxn];
void build(int x, int l, int r)
{
 node[x].l = l;
 node[x].r = r;
 if (l == r)
 {
  node[x].a = a[l];
  return;
 }
 int mid = (l + r) >> 1;
 build(lc(x), l, mid);
 build(rc(x), mid + 1, r);
 node[x].a = min(node[lc(x)].a, node[rc(x)].a);
}
inline void push_down(int x)
{
 node[lc(x)].tag += node[x].tag;
 node[lc(x)].a += node[x].tag;
 node[rc(x)].a += node[x].tag;
 node[rc(x)].tag += node[x].tag;
 node[x].tag = 0;
}
inline void update(int left, int right, int x, int k)
{
 if (left <= node[x].l && node[x].r <= right)
 {
  node[x].a += k;
  node[x].tag += k;
  return;
 }
 push_down(x);
 int mid = (node[x].l + node[x].r) >> 1;
 if (left <= mid) update(left, right, lc(x), k);
 if (right > mid) update(left, right, rc(x), k);
 node[x].a = min(node[lc(x)].a, node[rc(x)].a);
}
int query(int left, int right, int x)
{
 int res = INFLL;
 if (left <= node[x].l && node[x].r <= right) return node[x].a;
 int mid = (node[x].l + node[x].r) >> 1;
 push_down(x);
 if (left <= mid) res = min(res, query(left, right, lc(x)));
 if (right > mid) res = min(res, query(left, right, rc(x)));
 return res;
}
 

 

posted on 2019-10-11 13:06  thjkhdf12  阅读(160)  评论(0)    收藏  举报