Submission #1022417

# Submission time Handle Problem Language Result Execution time Memory
1022417 2024-07-13T13:22:10 Z hasan2006 Birthday gift (IZhO18_treearray) C++17
0 / 100
73 ms 119688 KB
#include <bits/stdc++.h>

using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , p[N][25]  , us[N] , us1[N] , timer = 0;
vector<int>v[N];
void dfs(int n , int par = 0){
    p[n][0] = par;
    us[n] = ++timer;
    for(int i = 1; i <= 20; i++)
        p[n][i] = p[p[n][i - 1]][i - 1];
    for(auto to : v[n])
        if(to != par)
            dfs(to , n);
    us1[n] = ++timer;
}
bool ok(int x , int y){
    return (us[x] <= us[y] && us1[x] >= us1[y]);
}
ll lca(ll x , ll y){
    if(ok(x , y))
        return x;
    if(ok(y , x))
        return y;
    for(int i = 20; i >= 0; i--)
        if(p[x][i] && !ok(p[x][i] , y))
            x = p[x][i];
    return p[x][0];
}
set<int>st[N] , ss[N];
void solve()
{
    ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
    cin>>n>>m>>q;
    for(i = 1; i < n; i++){
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    dfs(1);
    for(i = 1; i <= m; i++){
        cin>>a[i];
        ss[a[i]].insert(i);
        b[i] = lca(a[i] , a[i - 1]);
        if(i != 1)
        st[b[i]].insert(i);
    }
    while(q--){
        cin>>k>>x>>y;
        if(k == 1){
            ss[a[x]].erase(x);
            a[x] = y;
            ss[a[x]].insert(x);
            if(x != m){
                st[b[x + 1]].erase(x + 1);
                b[x + 1] = lca(a[x + 1], a[x]);
                st[b[x + 1]].insert(x + 1);
            }
            if(x != 1){
                st[b[x]].erase(x);
                b[x] = lca(a[x - 1], a[x]);
                st[b[x]].insert(x);
            }
        }else {
            cin>>k;
            if(st[k].size()){
            auto it = st[k].upper_bound(x);
                if(it != st[k].end() && *it <= y){
                    cout<<*it - 1 << " "<<*it<<"\n";
                    continue;
                }
            }
            if(ss[k].size()){
                auto it1 = ss[k].upper_bound(x - 1);
                if(it1 != ss[k].end() && *it1 <= y){
                    cout<<*it1 << " "<<*it1<<"\n";
                    continue;
                }
            }
            cout<<-1<<" "<<-1<<"\n";
        }
    }
}

int main(){
    TL;

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
   // cin>>t;
    while(t--)
     {
        solve();
     }
}
// Author : حسن

Compilation message

treearray.cpp: In function 'void solve()':
treearray.cpp:50:20: warning: unused variable 'j' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                    ^
treearray.cpp:50:38: warning: unused variable 's' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                      ^
treearray.cpp:50:46: warning: unused variable 'f' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                              ^
treearray.cpp:50:58: warning: unused variable 'mn' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                          ^~
treearray.cpp:50:69: warning: unused variable 'mx' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                                     ^~
treearray.cpp: In function 'int main()':
treearray.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 73 ms 119688 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 73 ms 119688 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 73 ms 119688 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 73 ms 119688 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -