2018 UESTC Training for Data Structures 一棵像样的线段树

一棵像样的线段树

设 last[x] 表示使 bi = x 的最大的 i,即 bi 中 x 最后出现的位置.

这样问题就转换为 bi = min last[x]

以上引用自UESTC 傅大爷 的题解

//389MS   31804KB
#include 
#include 
#include 
using namespace std;
const int MAX=1e6+5;
const int INF=0x3f3f3f3f;
int c[MAX];
int tree[MAX*4];
void read(int &x)
{
    int f=1;x=0;char s=getchar();
    while (s<'0'||s>'9'){if (s=='-')f=-1;s=getchar();}
    while (s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
void update(int r,int L,int R,int pos,int val)
{
    if (L==R)
    {
        tree[r]=val;
        return ;
    }
    int mid=(L+R)>>1;
    if (pos<=mid) update (r*2,L,mid,pos,val);
    else update (r*2+1,mid+1,R,pos,val);
    tree[r]=tree[r*2]>tree[r*2+1]?tree[r*2+1]:tree[r*2];
}
int query(int r,int L,int R,int top)
{
    if (L==R) return L;
    int mid=(L+R)>>1,ans;
    if (tree[r*2]

你可能感兴趣的:(线段树)