답안 #476997

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476997 2021-09-29T17:16:39 Z urosk Regions (IOI09_regions) C++14
0 / 100
1876 ms 131076 KB
// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x) long long
#define here cerr<<"---------------------------\n"
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

void setIO(string inoutname)
{
	freopen((inoutname+".in").c_str(),"r",stdin);
    	freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
   if(b==0) return a;
   if(a==0) return b;
   if(a>=b)  return gcd(a%b,b);
   return  gcd(a,b%a);
}
ll lcm(ll a,ll b){
   return (a/gcd(a,b))*b;
}
ll add(ll a,ll b){
	a+=b;
	a+=mod;
	if(a>=mod) a%=mod;
	return a;
}
ll mul(ll a,ll b){return(a*b)%mod;}
#define maxn 200005
ll n,r,q;
ll a[maxn];
ll b[maxn];
ll in[maxn],out[maxn];
ll cnt[maxn];
vector<ll> g[maxn];
ll ti = 1;
void dfs(ll u){
    in[u] = ti;
    b[ti] = a[u];
    ti++;
    for(ll v : g[u]){
        dfs(v);
    }
    out[u] = ti-1;
}
bool in_t(ll v,ll u){
    return in[u]<=in[v]&&out[u]>=out[v];
}
bool cmp(ll x,ll y){return in[x]<in[y];}
map<pll,ll> pre;
vector<ll> w[maxn];
vector<ll> wt[maxn];
vector<pll> f;
ll bsr(ll x,ll i){
    ll l = 0,r = sz(wt[x])-1,mid,rez = -1;
    while(l<=r){
        mid = (l+r)/2;
        if(i>=wt[x][mid]){
            rez = mid;
            l = mid+1;
        }else r = mid-1;
    }
    return rez;
}
ll bsl(ll x,ll i){
    ll l = 0,r = sz(wt[x])-1,mid,rez = -1;
    while(l<=r){
        mid = (l+r)/2;
        if(i<=wt[x][mid]){
            rez = mid;
            r = mid-1;
        }else l = mid+1;;
    }
    return rez;
}
#define endl '\n'
void tc(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> r >> q;
    cin >> a[1];
    w[a[1]].pb(1);
    for(ll i = 2;i<=n;i++){
        ll x;
        cin >> x >> a[i];
        w[a[i]].pb(i);
        g[x].pb(i);
    }
    dfs(1);
    for(ll i = 1;i<=r;i++){
        for(ll j : w[i]){
            wt[i].pb(in[j]);
        }
        sort(all(wt[i]));
    }
    ll siz = 1;
    for(ll i = 1;i*i<=n;i++) siz = max(0LL,i-30);
    for(ll i = 1;i<=r;i++){
        if(sz(w[i])<siz) continue;
        fill(cnt,cnt+n+2,0);
        for(ll j : w[i]){
            cnt[in[j]]++;
            cnt[out[j]+1]--;
        }
        for(ll j = 1;j<ti;j++){
            cnt[j]+=cnt[j-1];
            pre[{i,b[j]}]+=cnt[j];
        }
    }
    /*here;
    for(ll j = 1;j<=r;j++){
            for(ll i : wt[j]) cerr<<i<< " ";
            cerr<<endl;
    }
    here;*/
    while(q--){
        ll r1,r2;
        cin >> r1 >> r2;
        if(sz(w[r1])>=siz||pre.count({r1,r2})) cout<<pre[{r1,r2}]<<endl;
        else{
            ll ans = 0;
            //cerr<<"R2: "<<r2<<endl;
            for(ll i : w[r1]){
                ll l = in[i],r = out[i];
                ll br = bsr(r2,r);
                ll bl = bsl(r2,l);
                //cerr<<"AAA"<<endl;
                //cerr<<l<< " "<<r<<endl;
                //cerr<<bl<< " "<<br<<endl;
                if(bl==-1) continue;
                if(br==-1) continue;
                if(bl>br) continue;
                //cerr<<bl<< " "<<br<<" "<<br-bl<<endl;
                ans+=br-bl+1;
            }
            pre[{r1,r2}] = ans;
            cout<<ans<<endl;
        }
    }
}
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
	//setIO("lol");
	int t; t = 1;
	while(t--){
		tc();
	}
	return 0;
}

Compilation message

regions.cpp: In function 'void setIO(std::string)':
regions.cpp:31:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |  freopen((inoutname+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:32:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |      freopen((inoutname+".out").c_str(),"w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10 ms 14408 KB Time limit exceeded (wall clock)
2 Execution timed out 9 ms 14408 KB Time limit exceeded (wall clock)
3 Execution timed out 7 ms 14408 KB Time limit exceeded (wall clock)
4 Execution timed out 8 ms 14436 KB Time limit exceeded (wall clock)
5 Execution timed out 11 ms 14536 KB Time limit exceeded (wall clock)
6 Execution timed out 29 ms 18240 KB Time limit exceeded (wall clock)
7 Execution timed out 21 ms 15568 KB Time limit exceeded (wall clock)
8 Execution timed out 15 ms 15048 KB Time limit exceeded (wall clock)
9 Execution timed out 9 ms 14920 KB Time limit exceeded (wall clock)
10 Execution timed out 10 ms 15176 KB Time limit exceeded (wall clock)
11 Execution timed out 13 ms 15688 KB Time limit exceeded (wall clock)
12 Execution timed out 14 ms 16200 KB Time limit exceeded (wall clock)
13 Execution timed out 16 ms 16200 KB Time limit exceeded (wall clock)
14 Execution timed out 330 ms 18928 KB Time limit exceeded (wall clock)
15 Execution timed out 345 ms 20932 KB Time limit exceeded (wall clock)
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 125 ms 21736 KB Time limit exceeded (wall clock)
2 Execution timed out 450 ms 21992 KB Time limit exceeded (wall clock)
3 Execution timed out 95 ms 23576 KB Time limit exceeded (wall clock)
4 Execution timed out 272 ms 32320 KB Time limit exceeded (wall clock)
5 Execution timed out 23 ms 18368 KB Time limit exceeded (wall clock)
6 Execution timed out 431 ms 41980 KB Time limit exceeded (wall clock)
7 Execution timed out 601 ms 52188 KB Time limit exceeded (wall clock)
8 Execution timed out 1415 ms 88428 KB Time limit exceeded (wall clock)
9 Execution timed out 76 ms 28188 KB Time limit exceeded (wall clock)
10 Runtime error 1876 ms 131076 KB Execution killed with signal 9
11 Execution timed out 97 ms 29764 KB Time limit exceeded (wall clock)
12 Execution timed out 162 ms 33640 KB Time limit exceeded (wall clock)
13 Execution timed out 135 ms 33644 KB Time limit exceeded (wall clock)
14 Execution timed out 736 ms 55840 KB Time limit exceeded (wall clock)
15 Execution timed out 180 ms 38328 KB Time limit exceeded (wall clock)
16 Execution timed out 174 ms 41632 KB Time limit exceeded (wall clock)
17 Execution timed out 751 ms 63384 KB Time limit exceeded (wall clock)