Submission #1133255

#TimeUsernameProblemLanguageResultExecution timeMemory
1133255Graypopa (BOI18_popa)C++20
100 / 100
25 ms556 KiB
#include <bits/stdc++.h>
#include "popa.h"
using namespace std;
#define ll int
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair

#define INF 2e18
#define MOD 1e9+7

struct Node{
    Node * left, * right; ll ind;
    Node(ll _ind):ind(_ind){
        left=right=nullptr;
    }
};
// ll cnt; vector<ll> a;
// bool query(ll l1, ll r1, ll l2, ll r2){
//     cnt++;
//     ll gcd1 = 0, gcd2=0;
//     for (ll i=l1; i<=r1; i++) gcd1=__gcd(gcd1, a[i]);
//     for (ll i=l2; i<=r2; i++) gcd2=__gcd(gcd2, a[i]);
//     return (gcd1==gcd2);
// }

void dfs(Node * cur, int *Left, int *Right){
    Left[cur->ind] = (cur->left?cur->left->ind:-1);
    Right[cur->ind] = (cur->right?cur->right->ind:-1);
    if (cur->left) dfs(cur->left, Left, Right);
    if (cur->right) dfs(cur->right, Left, Right);
}

int solve(ll n, int *Left, int *Right){
    vector<Node*> chain;
    Node * temp; Node * buffer;
    for (ll i=0; i<n; i++){
        temp = new Node(i); buffer = nullptr;
        while (!chain.empty() and query(i, i, chain.back()->ind, i)){
            buffer = chain.back();
            chain.pop_back();
        }
        temp->left = buffer;
        if (!chain.empty()) chain.back()->right=temp;
        chain.push_back(temp);
    }
    dfs(chain[0], Left, Right);
    return chain[0]->ind;
}

// void solve(){
//     cnt=0; ll n; cin >> n; a.resize(n);
//     for (ll i=0; i<n; i++) cin >> a[i];
//     int Left[n], Right[n];
//     cout << solve(n, Left, Right) << ln;
//     for (ll i=0; i<n; i++){
//         cout << Left[i] << " ";
//     }
//     cout << ln;
//     for (ll i=0; i<n; i++){
//         cout << Right[i] << " ";
//     }
//     cout << ln;
// }
// int main(){
//     ios_base::sync_with_stdio(false); cin.tie(nullptr);
//     auto start = chrono::high_resolution_clock::now();
//     ll t=1;
//     // cin >> t;
//     while (t--) solve();
//     #ifdef LOCAL
//     auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
//     cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
//     #endif
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...