Welcome, Guest! Sign Up RSS

Clever Space

Friday, 11.22.2024
Main » 2013 » October » 5 » [甚是厉害]我的线段树已经写到一种境界了
4:35 PM
[甚是厉害]我的线段树已经写到一种境界了

PR是双关键字(pair<int,int>)

struct node{int l,r;PR m;};
PR min(PR a,PR b)
{
  if (a<b) return a;
  return b;
}
struct segtree
{
  node tree[4*N];
  void update(int x)
  {tree[x].m=min(tree[x<<1].m,tree[(x<<1)+1].m);}
  void build(int x,int L,int R)
  {
    tree[x].l=L;tree[x].r=R;int M=(L+R)>>1;
    if (L==R) {tree[x].m=mp(a[L],L);return;}
    build(x<<1,L,M);build((x<<1)+1,M+1,R);
    update(x);
  }
  PR query(int x,int L,int R)
  {
    if (tree[x].l==L&&tree[x].r==R) return tree[x].m;
    int M=(tree[x].l+tree[x].r)>>1;
    if (R<=M) return query(x<<1,L,R);
    if (L>M) return query((x<<1)+1,L,R);
    return min(query(x<<1,L,M),query((x<<1)+1,M+1,R));
  }
}Tree;
Views: 403 | Added by: dhy0077 | Rating: 5.0/1
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]