CTSC2016 Time Travel

Time Travel

Problems face a little longer, put links.
LOJ face problems .

answer

Obviously y, z useless. Then the requirement is minimized (X 0 the -X- I ) 2 + C I .

We saw a quadratic function in the form of convex hull maintenance should think. Consider the problem of how to maintain the convex hull,Obviously durable dynamic convex hull is feasible

Since the operation of the sub-tree for each node to take effect, and the sub-tree of DFS order it is continuous, so you can do the tree line DFS order to divide and conquer. Modify operations can range while DFS settlement, mainly recursion and backtracking before the operation, see the specific code.

Since the modified then independent of each other, it is possible to build a tree node segment of the convex hull. Ask first into the tree line in the outer query sort after, so do not write a half.

Time complexity of O (log n- 2 n-).

#include<bits/stdc++.h>
using namespace std;
template<class T> T read(){
    T x=0,w=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T> T read(T&x){
    return x=read<T>();
}
#define co const
#define il inline
typedef long long LL;

struct Vector {LL x,y;};
il bool operator<(co Vector&a,co Vector&b){
    return a.x!=b.x?a.x<b.x:a.y<b.y;
}
il Vector operator-(co Vector&a,co Vector&b){
    return (Vector){a.x-b.x,a.y-b.y};
}
il LL cross(co Vector&a,co Vector&b){
    return a.x*b.y-a.y*b.x;
}
il LL calc(co Vector&a,int x){
    return (LL)x*x-a.x*x+a.y;
}

co int N=500000+10;
int n,m;
LL X[N],C[N];
int id[N],to[N],next[N];
int pos[N],dfn,last[N];

vector<Vector> tree[N<<2];
int pl[N<<2]; // edit 3: N<<2
#define lc (x<<1)
#define rc (x<<1|1)
void insert(int x,int l,int r,int ql,int qr,co Vector&v){
//  if(x==1) cerr<<"ins "<<ql<<" "<<qr<<" "<<v.x<<" "<<v.y<<endl;
    if(ql<=l and r<=qr)
        return tree[x].push_back(v);
    int mid=(l+r)>>1;
    if(ql<=mid) insert(lc,l,mid,ql,qr,v);
    if(qr>mid) insert(rc,mid+1,r,ql,qr,v);
}
void dfs(int x){
    pos[x]=++dfn;
    if(id[x]>0) last[id[x]]=dfn;
    else{
        int p=-id[x];
        if(last[p]<=dfn-1)
            insert(1,1,n,last[p],dfn-1,(Vector){2*X[p],X[p]*X[p]+C[p]});
        last[p]=0;
    }
    for(int y=to[x];y;y=next[y]) dfs(y);
    if(id[x]<0) last[-id[x]]=dfn+1;
    else{
        int p=id[x];
        if(last[p]<=dfn)
            insert(1,1,n,last[p],dfn,(Vector){2*X[p],X[p]*X[p]+C[p]});
        last[p]=0;
    }
}
void build(int x,int l,int r){
    sort(tree[x].begin(),tree[x].end());
    static Vector st[N];
    int top=0;
    for(int i=0;i<(int)tree[x].size();++i){
        while(top>=2 and cross(tree[x][i]-st[top-1],st[top]-st[top-1])>=0) --top;
        st[++top]=tree[x][i];
    }
    tree[x].assign(st+1,st+top+1);
    if(l==r) return;
    int mid=(l+r)>>1;
    build(lc,l,mid),build(rc,mid+1,r);
}

struct ask {int s,x,id;}q[N];
il bool operator<(co ask&a,co ask&b){
    return a.x<b.x;
}
LL ans[N];

void query(int x,int l,int r,co ask&q){
//  if(x==1) cerr<<"qry "<<q.s<<" "<<q.x<<" "<<q.id<<endl;
    if(tree[x].size()){
        while(pl[x]<(int)tree[x].size()-1 and calc(tree[x][pl[x]+1],q.x)<=calc(tree[x][pl[x]],q.x)) ++pl[x]; // edit 1:<=
        ans[q.id]=min(ans[q.id],calc(tree[x][pl[x]],q.x));
    }
    if(l==r) return;
    int mid=(l+r)>>1;
    if(q.s<=mid) query(lc,l,mid,q);
    else query(rc,mid+1,r,q);
}

int main(){
    freopen("travel.in","r",stdin),freopen("travel.out","w",stdout); // travel
    read(n),read(m),read(C[1]);
    id[1]=1;
    for(int i=2;i<=n;++i){
        int opt=read<int>(),fa=read<int>()+1;
        next[i]=to[fa],to[fa]=i,id[i]=read<int>()+1;
        if(!opt) read(X[id[i]]),read<int>(),read<int>(),read(C[id[i]]); // edit 2:id
        else id[i]=-id[i];
    }
    dfs(1),build(1,1,n);
    for(int i=1;i<=m;++i){
        int s=read<int>()+1,x=read<int>();
        q[i]=(ask){pos[s],x,i};
    }
    sort(q+1,q+m+1);
    for(int i=1;i<=m;++i) ans[q[i].id]=1e18,query(1,1,n,q[i]);
    for(int i=1;i<=m;++i) printf("%lld\n",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/11495318.html