TLE求调
查看原帖
TLE求调
802557
pyiming楼主2024/3/6 19:02
#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
class node{
    public:
        int v;
        node *p1,*p2;
        node(int v,node *p1,node *p2){
            this->v=v;
            this->p1=p1;
            this->p2=p2;
        }
};
class tree{
    public:
        int *a;
        node *s;
        node **rt;
        int tot;
        int n;
        tree(int n,int *a){
            tot=0;
            this->a=a;
            this->n=n;
            rt=new node*[n];
            rt[tot++]=build(1,n);
        }
        ~tree(){
            delete[] s;
            delete[] rt;
        }
        node *build(int l,int r){
            if(l==r){
                return s=new node(a[l],0,0);;
            }
            int mid=(l+r)>>1;
            node *t1=build(l,mid);
            node *t2=build(mid+1,r);
            return s=new node(0,t1,t2);
        }
        node *add(node *p,int l,int r,int loc,int val){
            if(l==r){
                return s=new node(val,p->p1,p->p2);
            }
            int mid=(l+r)>>1;
            return s=new node(0,loc<=mid?add(p->p1,l,mid,loc,val):p->p1,loc>mid?add(p->p2,mid+1,r,loc,val):p->p2);
        }
        int find(node *p,int l,int r,int loc){
            if(l==r){
                return p->v;
            }
            int mid=(l+r)>>1;
            if(loc<=mid) return find(p->p1,l,mid,loc);
            else return find(p->p2,mid+1,r,loc);
        }
        int solve(int v,int t,int loc,int val){
            int ans=0;
            if(t==1){
                rt[tot++]=add(rt[v],1,n,loc,val);
            }
            else{
                ans=find(rt[v],1,n,loc);
                rt[tot++]=s=new node(0,rt[v]->p1,rt[v]->p2);
            }
            return ans;
        }
};
int main(){
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    int *a=new int[n+1];
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    tree *s;
    s=new tree(n,a);
    for(int i=1;i<=m;i++){
        int v,op,loc,val;
        scanf("%d%d%d",&v,&op,&loc);
        if(op==1){
            scanf("%d",&val);
            s->solve(v,op,loc,val);
        }
        else{
            printf("%d\n",s->solve(v,op,loc,val));
        }
    }
    delete s;
    return 0;
}
2024/3/6 19:02
加载中...