// triple-bfs version // (with input validators) #include #include #include #include using namespace std; int main() { int T; cin >> T; assert( 1<=T && T<=20 ); for( int caseNo=1; caseNo<=T; ++caseNo ) { // Input vector C; int N; cin >> N; assert( 1<=N && N<=300000 ); while(N--) {int c; cin>>c; assert( 0<=c && c<=100 ); C.push_back(c);} // Solve // # NOTE: 20M * sizeof int * 4 = 320MB memory required. // # If doubles are used for representing ranges, 480MB. // # It is possible to cut it down to 160MB, but such a detail // # should not bother contestants. So we (judges) have to allow // # 480MB memory consumption. int nACM = accumulate(C.begin(), C.end(), 1); assert( 1<=nACM && nACM<=2000000 ); vector parent(nACM, -1); for(int a=0,id=1; a subtree_size(nACM, 1); for(int a=nACM-1; a>=1; --a) subtree_size[parent[a]] += subtree_size[a]; vector range_l(nACM,0), range_r(nACM,nACM); for(int a=0,id=1; a> M; assert( 1<=M && M<=500000 ); while(M--) { int a, b; cin >> a >> b; assert( a!=b ); if( range_l[a]<=range_l[b] && range_r[b]<=range_r[a] ) count ++; } // Output cout << count << endl; } }