QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#73290 | #4387. Static Query on Tree | poi | AC ✓ | 237ms | 66516kb | C++ | 2.8kb | 2023-01-23 14:59:18 | 2023-01-23 14:59:21 |
Judging History
answer
#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "queue"
#include "vector"
#include "queue"
#include "stack"
#include "ctime"
#include "set"
#include "map"
#include "cmath"
using namespace std;
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define mp make_pair
#define rep( i , a , b ) for( int i = (a) , i##end = b ; i <= i##end ; ++ i )
#define per( i , a , b ) for( int i = (a) , i##end = b ; i >= i##end ; -- i )
#define mem( a ) memset( a , 0 , sizeof (a) )
#define all( x ) x.begin() , x.end()
//#define int long long
typedef long long ll;
const int MAXN = 4e5 + 10;
int n , q;
vi G[MAXN];
int dep[MAXN] , dfn[MAXN] , clo , R[MAXN];
int g[MAXN][20];
void dfs( int u ) {
dfn[u] = ++ clo;
for( int v : G[u] ) {
dep[v] = dep[u] + 1;
g[v][0] = u;
rep( k , 1 , 17 ) g[v][k] = g[g[v][k - 1]][k - 1];
dfs( v );
}
R[u] = clo;
}
int lca( int u , int v ) {
if( dep[u] < dep[v] ) swap( u , v );
per( k , 17 , 0 ) if( dep[g[u][k]] >= dep[v] ) u = g[u][k];
if( u == v ) return u;
per( k , 17 , 0 ) if( g[u][k] != g[v][k] ) u = g[u][k] , v = g[v][k];
return g[u][0];
}
vi V[MAXN];
int A[MAXN] , B[MAXN] , C[MAXN];
int oc , sa[MAXN] , sb[MAXN] , res;
void afs( int u , int f ) {
if( C[u] ) ++ oc;
sa[u] = A[u] , sb[u] = B[u];
for( int v : V[u] ) afs( v , u ) , sa[u] += sa[v] , sb[u] += sb[v];
if( sa[u] && sb[u] && oc ) ++ res;
if( C[u] ) -- oc;
if( oc && sa[u] && sb[u] ) res += dep[u] - dep[f] - 1;
}
void solve() {
cin >> n >> q;
rep( i , 2 , n ) {
int f;
scanf("%d",&f);
G[f].pb( i );
}
dep[1] = 1 , dfs( 1 );
rep( i , 1 , q ) {
int a , b , c , u;
scanf("%d%d%d",&a,&b,&c);
vi vec;
rep( k , 1 , a ) scanf("%d",&u) , A[u] = 1 , vec.pb( u );
rep( k , 1 , b ) scanf("%d",&u) , B[u] = 1 , vec.pb( u );
rep( k , 1 , c ) scanf("%d",&u) , C[u] = 1 , vec.pb( u );
vec.pb( 1 );
sort( all( vec ) , [&]( int u , int v ) { return dfn[u] < dfn[v]; } );
vec.erase( unique( all( vec ) ) , vec.end() );
rep( t , 1 , vec.size() - 1 ) vec.pb( lca( vec[t] , vec[t - 1] ) );
sort( all( vec ) , [&]( int u , int v ) { return dfn[u] < dfn[v]; } );
vec.erase( unique( all( vec ) ) , vec.end() );
vi stk;
rep( t , 0 , vec.size() - 1 ) {
while( stk.size() && R[stk.back()] < dfn[vec[t]] ) stk.pop_back();
if( stk.size() ) V[stk.back()].pb( vec[t] );// , cerr << stk.back() << ' ' << vec[t] << endl;
stk.pb( vec[t] );
}
oc = res = 0;
afs( 1 , 1 );
printf("%d\n",res);
for( int x : vec ) A[x] = B[x] = C[x] = sa[x] = sb[x] = 0 , V[x].clear();
}
clo = 0;
rep( i , 1 , n ) mem( g[i] ) , G[i].clear();
}
signed main() {
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int T;cin >> T;while( T-- ) solve();
// solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 237ms
memory: 66516kb
input:
1 200000 18309 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...
output:
102147 62590 87270 88880 7654 61542 62953 85022 55135 54125 70500 64356 25824 88300 42278 15336 18132 28734 90282 42889 28099 31311 96842 19959 34366 60205 78358 91142 56048 74688 86091 51979 94750 11989 89544 86860 56720 29534 52343 90031 79002 90293 94554 48340 65015 9181 15016 19884 49445 14181 6...
result:
ok 18309 numbers