# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
884710 | parlimoos | Uzastopni (COCI15_uzastopni) | C++17 | 6 ms | 3676 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define cl clear
#define bg begin
#define lb lower_bound
#define ub upper_bound
#define arr(x) array<int , x>
#define endl '\n'
int n;
int vs[10000];
vector<arr(2)> tr[10000];
bool ls[10000][100];
bool rs[10000][100];
void dfs(int v = 0){
// cout << v << ">>\n" << flush;
for(auto el : tr[v]) dfs(el[1]);
ls[v][vs[v]] = 1 , rs[v][vs[v]] = 1;
arr(2) d = {vs[v] , -1};
int inx1 = int(lb(tr[v].bg() , tr[v].end() , d) - tr[v].bg());
while(inx1 > 0){
inx1--;
bool flg = 0;
for(int i = 99 ; i >= 1 ; i--){
if(ls[v][i] and rs[tr[v][inx1][1]][i - 1]){
flg = 1;
break;
}
}
if(flg) for(int i = 99 ; i >= 0 ; i--) if(ls[tr[v][inx1][1]][i]) ls[v][i] = 1;
}
d = {vs[v] , int(1e9)};
int inx2 = int(ub(tr[v].bg() , tr[v].end() , d) - tr[v].bg());
while(inx2 < (int)tr[v].size()){
bool flg = 0;
for(int i = 0 ; i < 99 ; i++){
if(rs[v][i] and rs[tr[v][inx2][1]][i + 1]){
flg = 1;
break;
}
}
if(flg) for(int i = 0 ; i <= 99 ; i++) if(rs[tr[v][inx2][1]][i]) rs[v][i] = 1;
inx2++;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0 ; i < n ; i++){
cin >> vs[i];
vs[i]--;
}
for(int i = 1 ; i < n ; i++){
int a , b;
cin >> a >> b;
tr[a - 1].pb({vs[b - 1] , b - 1});
}
for(int i = 0 ; i < n ; i++) sort(tr[i].bg() , tr[i].end());
dfs();
int o1 = 0 , o2 = 0;
for(int i = 0 ; i < 100 ; i++){
if(ls[0][i] and i <= vs[0]) o1++;
if(rs[0][i] and i >= vs[0]) o2++;
}
cout << 1ll * o1 * o2;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |