Submission #1209016

#TimeUsernameProblemLanguageResultExecution timeMemory
1209016og_matveychick1Long Mansion (JOI17_long_mansion)C++20
100 / 100
2380 ms230568 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// /*
//                                                //////////**DEFINES - START**//////////

#define ret return
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define be(x) x.begin()
#define en(x) x.end()
#define sz(x) ll(x.size())
#define for0(i, n) for (ll   i = 0; i < (n); ++i)
#define for1(i, n) for (ll   i = 1; i < (n); ++i)
#define rfor0(i, n) for (ll   i = (n) - 1; i >= 0; --i)
#define rfor1(i, n) for (ll   i = (n) - 1; i >= 1; --i)
#define rep(i, a, n) for (ll   i = a; i < ll(n); ++i)
#define rrep(i, a, n) for (ll   i = a - 1; i >= ll(n); --i)
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define fastIO() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define con continue
#define pb push_back
#define pob pop_back
#define deb(x) cout << (#x) << " is " << (x) << endl
#define ins insert
#define len(s) (s).length()
#define gi greater<int>()
#define gll greater<ll  >()
#define gstr greater<string>()
#define gpll greater<pair<ll  , ll  >>()
#define rast(x1, y1, x2, y2) sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
#define rev reverse
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define rs resize
#define last(a) a.back()
#define co count
#define ba(a) a.back()
#define um unordered_map
#define rsun(a) a.resize(unique(a.begin(), a.end())-a.begin())
#define endl '\n'
#ifdef OG_Matveychick1
bool local = true;
#else
bool local = false;
#endif

//                                                \\\\\\\\\\**DEFINES - END**\\\\\\\\\\
        // */

// /*
//                                                //////////**TYPEDEFS - START**//////////

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ull> vull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef pair<double, double> pdd;
typedef double ld;
typedef double D;
typedef vector<ld> vld;
typedef vector<pair<ld, ld>>
        vpld;
typedef string str;
typedef set<ll> sll;
typedef set<int> si;
typedef set<str> ss;
typedef set<pii> spii;
typedef multiset<int> msi;
typedef multiset<ll> msll;
typedef multiset<str> mss;
typedef multiset<pii> mspii;
typedef multiset<pll> mspll;
typedef map<str, str> mps;
typedef map<int, int> mpi;
typedef map<ll, ll> mpll;
typedef map<int, vi> mpvi;
typedef map<int, vll> mpvll;
typedef map<char, int> mpci;
typedef multimap<ll, ll> mmpll;
typedef multimap<str, str> mmps;
typedef multimap<int, int> mmpi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<long double>> vvld;
typedef vector<vvi> vvvi;
typedef vector<vector<char>> vvc;
typedef vector<vs> vvs;
typedef vector<D> vD;
typedef set<pair<ll, ll>>
        spll;
typedef pair<ull, ull> pull;
typedef vector<pull> vpull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<char> sc;
typedef queue<int> qi;
typedef queue<ll> qll;
typedef queue<bool> qb;
typedef vector<sll> vsll;
typedef queue<pair<ll, ll>>
        qpll;
typedef vector<vector<pair<int, int>>>
        vvpii;
typedef vector<vector<pair<ll, ll>>>
        vvpll;
typedef vector<spll> vspll;
typedef multiset<char> msc;
typedef queue<str> qs;
typedef vector<set<int>> vsi;
typedef priority_queue<ll> pqll;
typedef vector<vsll> vvsll;
typedef pair<ld, ld> pld;
typedef vector<vvll> vvvll;
typedef set<ld> sld;
typedef vector<vpld> vvpld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
        ordered_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
        ordered_multiset;

//                                                \\\\\\\\\\**TYPEDEFS - END**\\\\\\\\\\
        // */


// /*
//                                                //////////**CONSTANTS - START**//////////

const ld pi = acosl(-1);
const ll mod1 = 1e9 + 7;
const ll mod2 = 998244353;
const ll MAXLL = 9223372036854775807;
//const ll MAXINT = 2147483647;
const ld eps = 1e-6;

//                                                \\\\\\\\\\**CONSTANTS - END**\\\\\\\\\\
        // */


// /*
//                                                //////////**TEMPLATES - START**//////////


template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for (T &i: a) in >> i;
    return in;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
    in >> a.fi >> a.se;
    return in;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &a) {
    out << a.fi << " " << a.se;
    return out;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, vector<pair<T1, T2>>

&a) {
    for (
        pair<T1, T2> &i
            : a)
        in >> i.fi >> i.
                se;
    return
            in;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for (auto i: a) {
        out << i << " ";
    }
    return out;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &out, vector<pair<T1, T2>>

&a) {
    for (
        pair<T1, T2> i
            : a)
        out << i.fi << " " << i.se <<
            endl;
    return
            out;
}

template<typename T1>
ostream &operator<<(ostream &out, vector<vector<T1>> &a) {
    for (vector<T1> i: a) {
        for (T1 j: i) out << j << " ";
        out << endl;
    }
    return out;
}

template<typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
    b = (T1) b;
    return a > b ? b : a;
}

template<typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
    b = (T1) b;
    return a > b ? a : b;
}

template<typename T1, typename T2>
inline void amin(T1 &a, T2 b) {
    a = min(a, b);
}

template<typename T1, typename T2>
inline void amax(T1 &a, T2 b) {
    a = max(a, b);
}


//                                                \\\\\\\\\\**TEMPLATES - END**\\\\\\\\\\
        // */


// This bear is a good alternative to duck!!!
/*
    ????      ??????
  ???????????????????
 ????????????????  ???
???   ???????????  ???
???  ???????????? ??
 ??????????????????
   ??????????????? ?
   ?????????????????
   ???????   ???    ??
    ????  ??????????
     ????     ?? ???
   ???????????? ?????
  ????????????????????
 ????????   ??  ???????
 ???????         ?????
 */


ld getTime() {
    return (ld) clock() / (ld) CLOCKS_PER_SEC;
}


mt19937_64 rn(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rn(4);

ll rnd(ll l, ll r) {
    ll a = rn() % (r - l + 1) + l;
    return a;
}

void solve();

ll T = 1;

signed main(int argc, char **argv) {
    //    setlocale(LC_ALL, "RUS");
    fastIO()
    cout.precision(12);
    cout << fixed;
    if (local && argc == 1) {
        freopen("input.txt", "r", stdin);
        //        freopen("output.txt", "w", stdout);
    }
//    cin >> T;
    while (T--) {
        solve();
    }
    if (local && argc == 1) {
        cout << endl << fixed << "time = " << getTime();
    }
    return 0;
}


/*
    ___        __              __   ______          __        _____ __             __          __  __
   /   | _____/ /___  ______ _/ /  / ____/___  ____/ /__     / ___// /_____ ______/ /______   / / / /__  ________
  / /| |/ ___/ __/ / / / __ `/ /  / /   / __ \/ __  / _ \    \__ \/ __/ __ `/ ___/ __/ ___/  / /_/ / _ \/ ___/ _ \
 / ___ / /__/ /_/ /_/ / /_/ / /  / /___/ /_/ / /_/ /  __/   ___/ / /_/ /_/ / /  / /_(__  )  / __  /  __/ /  /  __/
/_/  |_\___/\__/\__,_/\__,_/_/   \____/\____/\__,_/\___/   /____/\__/\__,_/_/   \__/____/  /_/ /_/\___/_/   \___/
*/


struct ST {
    struct node;
    ll n;
    vector<node> tree;
    vector<node> lazy;

    struct node {
        ll x;

        node() {}

        node(ll x) : x(x) {}
    };

    node neutral = 0;

    friend node operator+(node a, node b) {
        return max(a.x, b.x);
    }

    friend void operator%=(node &a, node b) {
        a.x += b.x;
    }

    ST() {}

    ST(ll n1) {
        n = n1;
        tree.resize(4 * n, neutral);
        lazy.resize(4 * n, neutral);
    }

    void update(ll l, ll r, ll val) {
        update(1, 0, n, l, r, val);
    }

    void update(ll pos, ll val) {
        update(1, 0, n, pos, val);
    }

    node query(ll l, ll r) {
        return query(1, 0, n, l, r);
    }

    void update(ll v, ll l, ll r, ll pos, ll val) {
        tree[v] %= lazy[v / 2];
        lazy[v] %= lazy[v / 2];
        if (l > pos || r <= pos) {
            return;
        }
        if (l == r - 1) {
            tree[v] %= val;
            return;
        }
        ll m = (l + r) / 2;
        update(v * 2, l, m, pos, val);
        update(v * 2 + 1, m, r, pos, val);
        tree[v] = tree[v * 2] + tree[v * 2 + 1];
        lazy[v] = neutral;
    }

    void update(ll v, ll l, ll r, ll L, ll R, ll val) {
        tree[v] %= lazy[v / 2];
        lazy[v] %= lazy[v / 2];
        if (l >= R || r <= L) {
            return;
        }
        if (l >= L && r <= R) {
            tree[v] %= val;
            lazy[v] %= val;
            return;
        }
        ll m = (l + r) / 2;
        update(v * 2, l, m, L, R, val);
        update(v * 2 + 1, m, r, L, R, val);
        tree[v] = tree[v * 2] + tree[v * 2 + 1];
        lazy[v] = neutral;
    }

    node query(ll v, ll l, ll r, ll L, ll R) {
        tree[v] %= lazy[v / 2];
        lazy[v] %= lazy[v / 2];
        if (l >= L && r <= R) {
            return tree[v];
        }
        if (l >= R || r <= L) {
            return -1e18;
        }
        ll m = (l + r) / 2;
        node re = query(v * 2, l, m, L, R) + query(v * 2 + 1, m, r, L, R);
        tree[v] = tree[v * 2] + tree[v * 2 + 1];
        lazy[v] = neutral;
        return re;
    }
};

const ll N = 5e5 + 5;

ll n, c[N], q, l[N], r[N];
vll a[N];
vpll bad;
pll z[N];
str ans[N];
ST t;
vpll d[N];
sll sl[N], sr[N];

void prec() {
    bad.clear();
    for0(i, N) sl[i].clear(), sr[i].clear();
    for0(i, n) for (auto x: a[i]) sr[x].ins(i);
    for0(i, n - 1) {
        for (auto x: a[i]) sr[x].erase(i), sl[x].ins(i);
        r[i] = n - 1;
        l[i] = -N;
        if (sz(sr[c[i]])) r[i] = *be(sr[c[i]]);
        if (sz(sl[c[i]])) l[i] = *--en(sl[c[i]]);
    }
    t = ST(n - 1);
    for0(i, n - 1) t.update(i, -1 - l[i]);
    set<array<ll, 3>> s;
    s.ins({0, n - 2, -1});
    for0(i, n) {
        for (auto [id, v]: d[i]) ans[id] = t.query(i, v).x >= 0 ? "NO" : "YES";
        d[i].clear();

        array<ll, 3> nt = {i, r[i] - 1, i};
        auto it = s.lb({i, -5, -5});
        if (it == en(s) || (*it)[0] != i) {
            it--;
            auto ntt = *it;
            ntt[0] = i;
            s.erase(it);
            s.ins(ntt);
        }
        while (1) {
            it = s.lb({i, -5, -5});
            if (it == en(s) || (*it)[0] > nt[1]) break;
            if ((*it)[1] <= nt[1]) {
                t.update((*it)[0], (*it)[1] + 1, -((*it)[2]));
                s.erase(it);
            } else {
                t.update((*it)[0], nt[1] + 1, -((*it)[2]));
                auto ntt = *it;
                ntt[0] = nt[1] + 1;
                s.erase(it);
                s.ins(ntt);
            }
        }
        t.update(nt[0], nt[1] + 1, i);
        s.ins(nt);
    }
}

void solve() {
    cin >> n;
    for0(i, n - 1) cin >> c[i], c[i]--;
    for0(i, n) {
        ll s;
        cin >> s;
        a[i].rs(s);
        cin >> a[i];
        for (auto &x: a[i]) x--;
        sort(all(a[i]));
    }
    cin >> q;
    for0(i, q) cin >> z[i], z[i].fi--, z[i].se--;

    for0(i, q) {
        auto [u, v] = z[i];
        if (u < v) d[u].pb({i, v});
    }
    prec();

    rev(a, a + n);
    rev(c, c + n - 1);
    for0(i, q) {
        auto [u, v] = z[i];
        if (u > v) d[n - 1 - u].pb({i, n - 1 - v});
    }
    prec();

    for0(i, q) cout << ans[i] << endl;
}

Compilation message (stderr)

long_mansion.cpp: In function 'int main(int, char**)':
long_mansion.cpp:292:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  292 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...