結果

問題 No.2325 Skill Tree
ユーザー Luke02561Luke02561
提出日時 2023-05-28 17:34:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 722 ms / 3,000 ms
コード長 7,126 bytes
コンパイル時間 5,061 ms
コンパイル使用メモリ 287,504 KB
実行使用メモリ 25,000 KB
最終ジャッジ日時 2023-08-27 14:41:05
合計ジャッジ時間 26,634 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 272 ms
6,224 KB
testcase_08 AC 190 ms
12,168 KB
testcase_09 AC 310 ms
8,868 KB
testcase_10 AC 253 ms
19,404 KB
testcase_11 AC 321 ms
13,440 KB
testcase_12 AC 602 ms
23,884 KB
testcase_13 AC 607 ms
23,892 KB
testcase_14 AC 604 ms
24,056 KB
testcase_15 AC 590 ms
24,072 KB
testcase_16 AC 601 ms
24,096 KB
testcase_17 AC 575 ms
24,004 KB
testcase_18 AC 579 ms
23,948 KB
testcase_19 AC 572 ms
24,048 KB
testcase_20 AC 573 ms
23,872 KB
testcase_21 AC 575 ms
23,924 KB
testcase_22 AC 616 ms
24,072 KB
testcase_23 AC 614 ms
23,892 KB
testcase_24 AC 612 ms
23,884 KB
testcase_25 AC 621 ms
23,996 KB
testcase_26 AC 618 ms
24,056 KB
testcase_27 AC 703 ms
24,964 KB
testcase_28 AC 705 ms
24,920 KB
testcase_29 AC 701 ms
24,888 KB
testcase_30 AC 703 ms
24,752 KB
testcase_31 AC 708 ms
24,676 KB
testcase_32 AC 640 ms
24,736 KB
testcase_33 AC 666 ms
25,000 KB
testcase_34 AC 658 ms
24,944 KB
testcase_35 AC 702 ms
24,544 KB
testcase_36 AC 689 ms
24,848 KB
testcase_37 AC 722 ms
24,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define ALL(x) x.begin(),x.end()
#define BACK(x) x.rbegin(),x.rend()
#define MOD1 1000000007
#define MOD2 998244353
#define MOD1_BASE 131
#define INF (LLONG_MAX / 2)
#define FLOAT_ANS setprecision(30)
#define TORAD(x) (x*acos(-1)/180.0)
#define TODEG(x) (x*180/acos(-1))

using namespace std;
using ll = long long;
using ull = unsigned long long;

template<typename T> // T:重み
using p_que = priority_queue<T,vector<T>,greater<T>>;

template<typename T>
bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}

template<typename T>
bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}

ll modpow(ll a, ll n, ll mod) {ll res=1;while (n>0) {if(n&1)res=(res*(a%mod))%mod;a=((a%mod)*(a%mod))%mod;n>>=1;}return res;}

template<typename T>
void RotateVec2(vector<vector<T>>&v){ll h=v.size();ll w=v[0].size();vector<vector<T>>t(w,vector<T>(h));rep(i,h){rep(j,w){t[j][h-i-1]=v[i][j];}}v=t;}

template<class T>
bool InRange(T x, T mn, T mx){return (mn <= x && x <= mx);}

template<typename T>
vector<T>&merged(vector<T>&a,vector<T>&b) {vector<T>res;merge(a.begin(),a.end(),b.begin(),b.end(),back_inserter(res));return res;}

struct UnionFind{
    vector<ll>tree;
    UnionFind(ll x):tree(x, -1){}
    ll root(ll x){if(tree[x]<0) return x;return tree[x]=root(tree[x]);}
    bool same(ll x,ll y){return root(x)==root(y);}
    ll size(ll x){return -tree[root(x)];}
    void unite(ll x,ll y){x=root(x),y=root(y);if(x==y)return;if(size(x)<size(y))swap(x,y);tree[x]+=tree[y];tree[y]=x;}
};

template<typename T>
struct SegTree{
    ll n;T e;vector<T>tree;function<T(T,T)>f,add;
    SegTree(ll n_,function<T(T,T)>f_,T e_=0,function<T(T,T)>add_=[](T next,T old){return next;}):e(e_),f(f_),add(add_){ll x=1;while(x<n_)x*=2;n=x;tree.assign(n*2,e);}
    void update(ll idx,T x){idx+=n-1;tree[idx]=add(x,tree[idx]);while(idx){idx=(idx-1)/2;tree[idx]=f(tree[idx*2+1],tree[idx*2+2]);}}
    T query(ll x,ll y){return query_sub(x,y,0,n,0);}
    T query_sub(ll x,ll y,ll l,ll r,ll k){if(r<=x||y<=l)return e;if(x<=l&&r<=y)return tree[k];T c1=query_sub(x,y,l,(l+r)/2,k*2+1);T c2=query_sub(x,y,(l+r)/2,r,k*2+2);return f(c1,c2);}
    T get(ll idx){return tree[idx+n-1];}
};

template<std::uint_fast64_t Modulus> class modint {
    using u64 = std::uint_fast64_t;
public:
    u64 a;
    constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus)+Modulus)%Modulus) {}
    constexpr u64 &value() noexcept { return a; }
    constexpr const u64 &value() const noexcept { return a; }
    constexpr modint operator+(const modint rhs) const noexcept {return modint(*this) += rhs;}
    constexpr modint operator-(const modint rhs) const noexcept {return modint(*this) -= rhs;}
    constexpr modint operator*(const modint rhs) const noexcept {return modint(*this) *= rhs;}
    constexpr modint operator/(const modint rhs) const noexcept {return modint(*this) /= rhs;}
    constexpr modint &operator+=(const modint rhs) noexcept {a += rhs.a;if (a >= Modulus) {a -= Modulus;}return *this;}
    constexpr modint &operator-=(const modint rhs) noexcept {if (a < rhs.a) {a += Modulus;}a -= rhs.a;return *this;}
    constexpr modint &operator*=(const modint rhs) noexcept {a = a * rhs.a % Modulus;return *this;}
    constexpr modint &operator/=(modint rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
            if (exp % 2) {
                *this *= rhs;
            }
            rhs *= rhs;
            exp /= 2;
        }
        return *this;
    }
    constexpr modint &operator=(u64 x){ a = x % Modulus; return *this; }
};

template<class T=ll>
struct Vector2D {
    T x, y;
    Vector2D():x(0),y(0) {}
    Vector2D(T x_, T y_):x(x_),y(y_) {}

    double length() const { return sqrt((double)x*x+y*y); };
    bool inrange(const Vector2D a, const Vector2D b) { return (InRange(x, a.x, b.x) and InRange(y, a.y, b.y)); }
    Vector2D operator-(const Vector2D a) const { return Vector2D(*this) -= a; }
    Vector2D operator+(const Vector2D a) const { return Vector2D(*this) += a; }
    T operator*(const Vector2D a) const { return x*a.y+y*a.x; }
    Vector2D operator*(const T a) const { return Vector2D(*this) *= a; }
    Vector2D operator/(const T a) const { return Vector2D(*this) /= a; }
    Vector2D &operator+=(const Vector2D a) { x += a.x; y += a.y; return *this; }
    Vector2D &operator-=(const Vector2D a) { x -= a.x; y -= a.y; return *this; }
    Vector2D &operator-=(const T a) { x -= a; y -= a; return *this; }
    Vector2D &operator*=(const T a) { x *= a; y *= a; return *this; }
    Vector2D &operator/=(const T a) { x /= a; y /= a; return *this; }
    friend ostream& operator<< (ostream& stream, const Vector2D<>& x);
    bool operator==(const Vector2D a) const { return (x==a.x and y==a.y); }
    bool operator!=(const Vector2D a) const { return not (x==a.x and y==a.y); }
    bool operator>(const Vector2D a) const { return a < *this; }
    bool operator<(const Vector2D a) const 
    {
        return make_pair(x,y) < make_pair(a.x, a.y);
        // return x*a.y < y*a.x;
    }
};

ostream& operator<< (ostream& stream, const Vector2D<ll>& x) {
    string s = "(" + to_string(x.x) + ", " + to_string(x.y) + ")";
    stream << s;
    return stream;
}

ll popcount(ll x) { ll res = 0; while(x) {res+=x%2;x>>=1;} return res; }

// MAIN PROGRAM ------------

using mint = modint<MOD2>;
using Vec2 = Vector2D<ll>;
const Vec2 Angle[] = {{0,1}, {0,-1}, {-1,0}, {1,0}};

int main() {
    ll n;
    cin >> n;
    vector<Vec2>a(n);
    vector<vector<Vec2>>G(n);

    rep(i, n) {
        if (!i) continue;
        cin >> a[i].x >> a[i].y;
        --a[i].y;
        G[a[i].y].push_back({a[i].x, i});
    }
    vector<ll>cost(n, INF);
    cost[0] = 0;

    p_que<Vec2>que;
    que.push({0, 0});

    while(que.size()) {
        auto [c, now] = que.top(); que.pop();
        if (c!=cost[now]) continue;

        for (auto [i,j] : G[now]) {
            if (chmin(cost[j], max(i, cost[now]))) {
                que.push({cost[j], j});
            }
        }
    }

    vector<ll>levs;
    map<ll,ll>linv;
    SegTree<ll>seg(n, [](ll a,ll b){return a+b;}, 0ll, [](ll nxt,ll old){return nxt+old;});
    {
        vector<ll>tmp;
        rep(i, n) {
            tmp.push_back(cost[i]);
        }
        sort(ALL(tmp));
        tmp.erase(unique(ALL(tmp)), tmp.end());
        levs = tmp;
        rep(i, levs.size()) {
            linv[levs[i]] = i;
        }

        rep(i, n) {
            seg.update(linv[cost[i]], 1);
        }
    }

    // for (auto i : levs) {
    //     cout << i << " ";
    // }
    // cout << endl;
    // rep(i, n) cout << seg.get(i) << " ";
    // cout << endl;

    ll q;
    cin >> q;
    rep(idx, q) {
        ll t,x;
        cin >> t >> x;
        if (t == 1) {
            auto itr = lower_bound(ALL(levs), x);
            if (*itr!=x) --itr;
            ll i = itr-levs.begin();
            // cout << i << " ";
            cout << seg.query(0, i+1) << endl;
        }
        else {
            if (cost[x-1]==INF) cout << -1 << endl;
            else cout << cost[x-1] << endl;
        }
    }

}
0