Submission #427965

# Submission time Handle Problem Language Result Execution time Memory
427965 2021-06-15T06:14:08 Z 조영욱(#7659) Corporate life (after hostile takover) (CPSPC17_corpo) C++17
30 / 100
1000 ms 74424 KB
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> son1[200000];
vector<int> son2[200000];
int ind1[262144];
int sz1[200000];
int ind2[262144];
int rev2[200000];
int sz2[200000];
int f1=0;
int f2=0;

void dfs1(int v,int prev) {
    ind1[v]=f1++;
    sz1[v]=1;
    for(int nt:son1[v]) {
        dfs1(nt,v);
        sz1[v]+=sz1[nt];
    }
}

void dfs2(int v,int prev) {
    ind2[v]=f2++;
    rev2[ind2[v]]=v;
    sz2[v]=1;
    for(int nt:son2[v]) {
        dfs2(nt,v);
        sz2[v]+=sz2[nt];
    }
}

const int sz=262144;
vector<int> v[sz*2];

void init(int node=1,int nodel=0,int noder=sz-1) {
    if (nodel==noder) {
        if (nodel<n) {
            v[node].push_back(ind1[rev2[nodel]]);
        }
        return;
    }
    int mid=(nodel+noder)/2;
    init(node*2,nodel,mid);
    init(node*2+1,mid+1,noder);
    int i1=0;
    int i2=0;
    while (i1<v[node*2].size()||i2<v[node*2+1].size()) {
        if (i2==v[node*2+1].size()||(i1<v[node*2].size()&&v[node*2][i1]<v[node*2+1][i2])) {
            v[node].push_back(v[node*2][i1++]);
        }
        else {
            v[node].push_back(v[node*2+1][i2++]);
        }
    }
}

int query(int l,int r,int d,int u) {
    int ret=0;
    for(int pl=sz;pl>0;pl/=2) {
        if (l>r) {
            break;
        }
        if (l%2==1) {
            ret+=upper_bound(v[l+pl].begin(),v[l+pl].end(),u)-lower_bound(v[l+pl].begin(),v[l+pl].end(),d);
            //printf("%d ",l+pl);
            l++;
        }
        if (r%2==0) {
            ret+=upper_bound(v[r+pl].begin(),v[r+pl].end(),u)-lower_bound(v[r+pl].begin(),v[r+pl].end(),d);
            //printf("%d ",r+pl);
            r--;
        }
        if (l>r) {
            break;
        }
        l/=2;
        r/=2;
    }
    //printf("\n");
    return ret;
}

int main(void) {
    scanf("%d",&n);
    for(int i=1;i<n;i++) {
        int p;
        scanf("%d",&p);
        p--;
        son1[p].push_back(i);
    }
    for(int i=1;i<n;i++) {
        int p;
        scanf("%d",&p);
        p--;
        son2[p].push_back(i);
    }
    dfs1(0,-1);
    dfs2(0,-1);
    init();
    for(int i=0;i<n;i++) {
        printf("%d ",query(ind2[i],ind2[i]+sz2[i]-1,ind1[i],ind1[i]+sz1[i]-1)-1);
    }
}

Compilation message

Main.cpp: In function 'void init(int, int, int)':
Main.cpp:49:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     while (i1<v[node*2].size()||i2<v[node*2+1].size()) {
      |            ~~^~~~~~~~~~~~~~~~~
Main.cpp:49:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     while (i1<v[node*2].size()||i2<v[node*2+1].size()) {
      |                                 ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:50:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         if (i2==v[node*2+1].size()||(i1<v[node*2].size()&&v[node*2][i1]<v[node*2+1][i2])) {
      |             ~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:50:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         if (i2==v[node*2+1].size()||(i1<v[node*2].size()&&v[node*2][i1]<v[node*2+1][i2])) {
      |                                      ~~^~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Main.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         scanf("%d",&p);
      |         ~~~~~^~~~~~~~~
Main.cpp:95:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         scanf("%d",&p);
      |         ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 21964 KB Output is correct
2 Correct 17 ms 22044 KB Output is correct
3 Correct 16 ms 21936 KB Output is correct
4 Correct 18 ms 22056 KB Output is correct
5 Correct 17 ms 21940 KB Output is correct
6 Correct 17 ms 22052 KB Output is correct
7 Correct 18 ms 21964 KB Output is correct
8 Correct 18 ms 22112 KB Output is correct
9 Correct 18 ms 22172 KB Output is correct
10 Correct 17 ms 22208 KB Output is correct
11 Correct 18 ms 22220 KB Output is correct
12 Correct 20 ms 22208 KB Output is correct
13 Correct 20 ms 22268 KB Output is correct
14 Correct 18 ms 22220 KB Output is correct
15 Correct 19 ms 22420 KB Output is correct
16 Correct 20 ms 22452 KB Output is correct
17 Correct 24 ms 22444 KB Output is correct
18 Correct 20 ms 22424 KB Output is correct
19 Correct 20 ms 22496 KB Output is correct
20 Correct 20 ms 22544 KB Output is correct
21 Correct 23 ms 22476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 21964 KB Output is correct
2 Correct 17 ms 22044 KB Output is correct
3 Correct 16 ms 21936 KB Output is correct
4 Correct 18 ms 22056 KB Output is correct
5 Correct 17 ms 21940 KB Output is correct
6 Correct 17 ms 22052 KB Output is correct
7 Correct 18 ms 21964 KB Output is correct
8 Correct 18 ms 22112 KB Output is correct
9 Correct 18 ms 22172 KB Output is correct
10 Correct 17 ms 22208 KB Output is correct
11 Correct 18 ms 22220 KB Output is correct
12 Correct 20 ms 22208 KB Output is correct
13 Correct 20 ms 22268 KB Output is correct
14 Correct 18 ms 22220 KB Output is correct
15 Correct 19 ms 22420 KB Output is correct
16 Correct 20 ms 22452 KB Output is correct
17 Correct 24 ms 22444 KB Output is correct
18 Correct 20 ms 22424 KB Output is correct
19 Correct 20 ms 22496 KB Output is correct
20 Correct 20 ms 22544 KB Output is correct
21 Correct 23 ms 22476 KB Output is correct
22 Correct 296 ms 42668 KB Output is correct
23 Correct 389 ms 44624 KB Output is correct
24 Correct 440 ms 46816 KB Output is correct
25 Correct 240 ms 42876 KB Output is correct
26 Correct 273 ms 49372 KB Output is correct
27 Correct 261 ms 49244 KB Output is correct
28 Correct 283 ms 49348 KB Output is correct
29 Correct 708 ms 61532 KB Output is correct
30 Correct 835 ms 65324 KB Output is correct
31 Correct 923 ms 69448 KB Output is correct
32 Correct 558 ms 61928 KB Output is correct
33 Correct 608 ms 74296 KB Output is correct
34 Correct 603 ms 74424 KB Output is correct
35 Correct 729 ms 74284 KB Output is correct
36 Correct 747 ms 63128 KB Output is correct
37 Correct 970 ms 66980 KB Output is correct
38 Execution timed out 1090 ms 71432 KB Time limit exceeded
39 Halted 0 ms 0 KB -