제출 #1270232

#제출 시각아이디문제언어결과실행 시간메모리
1270232luattapcodeBirthday gift (IZhO18_treearray)C++20
56 / 100
4098 ms83600 KiB
/*The best way to get something done is to begin*/
#include     <bits/stdc++.h>
#define     ll long long
#define     int long long
#define     fr(i,a,b) for(int i=a;i<=b;i++)
#define     frd(i,a,b) for(int i=a;i>=b;i--)
#define     pb push_back
#define     fi first
#define     se second
#define     el '\n'
using namespace std;
template <class X, class Y> bool minimize(X &x, const Y &y) {if (x > y) {x = y;return true;} else return false;}
template <class X, class Y> bool maximize(X &x, const Y &y) {if (x < y) {x = y;return true;} else return false;}
/* Author: Huynh Quoc Luat */
/*Khai Bao*/
const long long inf=1e18;
const int oo=1e9;
const int LO=21;
const int CH=27;
const int N=2e5+5;
const int sm=1e9+7;
int a[N];
int n,m,q;
vector <int> g[N];
//void add(int &x,const int &y){x+=y;if(x>=sm)x-=sm;}
//void sub(int &x,const int &y){x-=y;if(x<0)x+=sm;}

void read()
{
    cin >> n >> m >> q;
    fr(i,1,n-1){
        int u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    fr(i,1,m) cin >> a[i];
}

namespace sub1
{
    set <pair <int,int>> s[N];
    int P[N][LO];
    int h[N];
    void dfs(int u,int p){
        for(auto v:g[u]){
            if(v==p) continue;
            h[v]=h[u]+1;
            P[v][0]=u;
            dfs(v,u);
        }
    }
    int lca(int u,int v){
        if(h[u]<h[v]) swap(u,v);
        frd(j,__lg(n),0) if(h[P[u][j]]>=h[v]) u=P[u][j];
        frd(j,__lg(n),0) if(P[u][j]!=P[v][j]) u=P[u][j],v=P[v][j];
        if(u==v) return u;
        return P[u][0];
    }
    void prep(){
        dfs(1,0);
        P[1][0]=1;
        fr(j,1,__lg(n)) fr(i,1,n) P[i][j]=P[P[i][j-1]][j-1];
    }
    void query2(int x,int l,int r){
        for(auto v:s[x]){
            if(v.fi>=l and v.se<=r){
                cout << v.fi << " " << v.se << el;
                return;
            }
        }
        cout << -1 << " " << -1 << el;
    }
    void query1(int x,int y){
        int old_lca1=a[x];
        int old_lca2=-1,old_lca3=-1;
        if(x-1!=0) old_lca2=lca(a[x],a[x-1]);
        if(x+1!=n+1) old_lca3=lca(a[x],a[x+1]);
        s[old_lca1].erase({x,x});
        if(old_lca2!=-1) s[old_lca2].erase({x-1,x});
        if(old_lca3!=-1) s[old_lca3].erase({x,x+1});
        a[x]=y;
        int new_lca1=a[x];
        int new_lca2=-1,new_lca3=-1;
        if(x-1!=0) new_lca2=lca(a[x],a[x-1]);
        if(x+1!=n+1) new_lca3=lca(a[x],a[x+1]);
        s[new_lca1].insert({x,x});
        if(new_lca2!=-1) s[new_lca2].insert({x-1,x});
        if(new_lca3!=-1) s[new_lca3].insert({x,x+1});

    }
    void process()
    {
        prep();
        fr(i,1,m){
            s[a[i]].insert({i,i});
            if(i+1!=n+1) s[lca(a[i],a[i+1])].insert({i,i+1});
        }
        // for(auto v:s[1]) cout << v.fi << " " << v.se << el;
        while(q--){
            int type,x,y,z;
            cin >> type;
            if(type==1) cin >> x >> y,query1(x,y);
            else cin >> x >> y >> z,query2(z,x,y);
        }
    }
}
void time()    {cerr<< endl<<"Luattapcode: "<<clock()<<"ms"<<endl;}
main()
{
    ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
    #define TASK "qn"
    if(fopen(TASK".INP","r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    int mtt=0;
    int test=1;
    if(mtt) cin>>test;
    while(test--)
    {
        read();
        sub1::process();
    }
    time();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

treearray.cpp:109:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  109 | main()
      | ^~~~
treearray.cpp: In function 'int main()':
treearray.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...