답안 #566627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
566627 2022-05-22T13:18:16 Z zaneyu Regions (IOI09_regions) C++14
100 / 100
5332 ms 47376 KB
/*input
6 3 4
1
1 2
1 3
2 3
2 3
5 1
1 2
1 3
2 3
3 1
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=2e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
    out<<P.f<<' '<<P.s;
    return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
    REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?"\n":"");
    return out;
}
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mypow(ll a,ll b){
    a%=MOD;
    if(a==0) return 0;
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
vector<int> v[maxn];
vector<int> vv[maxn],v2[maxn];
int bs;
int arr[maxn];
ll save[25000][405];
ll save2[405][25000];
int big[maxn];
int cntt[maxn];
void dfs(int u,vector<int> &cnt){
    if(big[arr[u]]!=-1) cnt[big[arr[u]]]++,cntt[big[arr[u]]]++;
    for(int x:v[u]){
        vector<int> asd(bs);
        dfs(x,asd);
        REP(j,bs) cnt[j]+=asd[j];
    }
    REP(j,bs) save[arr[u]][j]+=cnt[j];
    REP(j,bs) save2[j][arr[u]]+=cntt[j];
    if(big[arr[u]]) cntt[big[arr[u]]]--;
}
int in[maxn],out[maxn];
int cur=0;
void dfs2(int u){
    in[u]=cur++;
    for(int x:v[u]) dfs2(x);
    out[u]=cur-1;
}
const int blk=500;
int main(){
    int n,r,q;
    cin>>n>>r>>q;
    REP(i,n){
        int p,x;
        if(i) cin>>p,--p,v[p].pb(i);
        cin>>x;
        --x;
        arr[i]=x;
    }
    int bs=0;
    REP(i,n){
        if(sz(v[i])>=blk){
            big[i]=bs++;
        }
        else big[i]=-1;
    }    
    vector<int> cnt(bs);
    dfs(0,cnt);
    dfs2(0);
    REP(i,n) vv[arr[i]].pb(in[i]),v2[arr[i]].pb(out[i]);
    REP(i,r) sort(ALL(vv[i])),sort(ALL(v2[i]));
    while(q--){
        int a,b;
        cin>>a>>b;
        --a,--b;
        if(big[a]!=-1){
            cout<<save2[big[a]][b]<<endl;
            continue;
        }
        if(big[b]!=-1){
            cout<<save[a][big[b]]<<endl;
            continue;
        }
        int l=0,r=0;
        int ans=0;
        for(int x:v2[a]){
            while(r<sz(vv[b]) and vv[b][r]<=x) ++r;
            ans+=r;
        }
        for(int x:vv[a]){
            while(l<sz(vv[b]) and vv[b][l]<x) ++l;
            ans-=l;
        }
        cout<<ans<<endl;
    }
} 
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14416 KB Output is correct
2 Correct 8 ms 14324 KB Output is correct
3 Correct 9 ms 14416 KB Output is correct
4 Correct 12 ms 14416 KB Output is correct
5 Correct 14 ms 14404 KB Output is correct
6 Correct 28 ms 14544 KB Output is correct
7 Correct 37 ms 14416 KB Output is correct
8 Correct 38 ms 14544 KB Output is correct
9 Correct 39 ms 15236 KB Output is correct
10 Correct 98 ms 14928 KB Output is correct
11 Correct 118 ms 15340 KB Output is correct
12 Correct 172 ms 16008 KB Output is correct
13 Correct 107 ms 15440 KB Output is correct
14 Correct 186 ms 16092 KB Output is correct
15 Correct 258 ms 21156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2668 ms 20288 KB Output is correct
2 Correct 5332 ms 18228 KB Output is correct
3 Correct 4645 ms 23428 KB Output is correct
4 Correct 290 ms 16244 KB Output is correct
5 Correct 338 ms 19308 KB Output is correct
6 Correct 824 ms 17868 KB Output is correct
7 Correct 948 ms 18892 KB Output is correct
8 Correct 818 ms 27968 KB Output is correct
9 Correct 1473 ms 25480 KB Output is correct
10 Correct 2104 ms 34884 KB Output is correct
11 Correct 2549 ms 24580 KB Output is correct
12 Correct 1846 ms 25908 KB Output is correct
13 Correct 2130 ms 27268 KB Output is correct
14 Correct 2621 ms 26316 KB Output is correct
15 Correct 3142 ms 34596 KB Output is correct
16 Correct 3615 ms 47376 KB Output is correct
17 Correct 3737 ms 44664 KB Output is correct