萌新求助,RE#10
查看原帖
萌新求助,RE#10
68882
灵华楼主2022/5/4 16:32

能有哪位路过的大佬帮忙看看么/kel

#include <iostream>
#include <vector>
using namespace std ;

const int N = 250005 , NN = 30000005 ;
int n , m , tot , p[N] , fa[N] , siz[N] , son[N] , top[N] , id[N] , wd[N] , dt ;
int val[N] , rt[N] , ls[NN] , rs[NN] , lz[NN] ;
long long dis[N] , sud[N] , ts[N<<2] , tr[NN] , lans ;
vector < pair < int , int > > hd[N] ;

inline int read ( ) {
	char ch = getchar ( ) ;
	int x = 0 , f = 1 ;
	while ( ch < '0' || ch > '9' ) {
		if ( ch == '-' ) f = -1 ;
		ch = getchar ( ) ;
	}
	while ( ch >= '0' && ch <= '9' )
		x = x * 10 + ch - 48 , ch = getchar ( ) ;
	return x * f ;
}

inline void insert ( int u , int v , int w ) {
	hd [ u ] .push_back ( { v , w } ) ;
	hd [ v ] .push_back ( { u , w } ) ;
}

void dfs1 ( int x , int la ) {
	fa [ x ] = la ;
	siz [ x ] = 1 ;
	int axx = 0 ;
	for ( auto i : hd [ x ] ) {
		int y = i .first ;
		if ( y == la ) continue ;
		val [ y ] = i .second ;
		dis [ y ] = dis [ x ] + val [ y ] ;
		dfs1 ( y , x ) ;
		siz [ x ] += siz [ y ] ;
		if ( siz [ y ] > axx )
			axx = siz [ y ] , son [ x ] = y ;
	}
}

void dfs2 ( int x , int topf ){
	top [ x ] = topf ;
	id [ x ] = ++ dt ;
	wd [ dt ] = x ;
	if ( ! son [ x ] )
		return ;
	dfs2 ( son [ x ] , topf ) ;
	for ( auto i : hd [ x ] ) {
		int y = i .first ;
		if ( y == fa [ x ] || y == son [ x ] )
			continue ;
		dfs2 ( y , y ) ;
	}
}

void build ( int k , int l , int r ) {
	if ( l == r ) {
		ts [ k ] = val [ wd [ l ] ] ;
		return ;
	}
	int mid = ( l + r ) >> 1 ;
	build ( k << 1 , l , mid ) ;
	build ( k << 1 | 1 , mid + 1 , r ) ;
	ts [ k ] = ts [ k << 1 ] + ts [ k << 1 | 1 ] ;
}

/*
int newnode ( ) {
	if ( de .empty ( ) )
		return ++ tot ;
	int x = de .top ( ) ; de .pop ( ) ;
	return x ;
}

void del ( int x ) {
	de .push ( x ) ;
}
*/

int lim = N ;
int change ( int k , int kb , int l , int r , int x , int y ) {
	int kt ;
	if ( k <= lim ) {
		kt = ++ tot ;
		ls [ kt ] = ls [ k ] ;
		rs [ kt ] = rs [ k ] ;
		lz [ kt ] = lz [ k ] ;
		tr [ kt ] = tr [ k ] ;
	}
	else kt = k ;
	if ( x <= l && r <= y ) {
		++ lz [ kt ] ;
		tr [ kt ] += ts [ kb ] ;
		return kt ;
	}
	int mid = ( l + r ) >> 1 ;
	if ( x <= mid )
		ls [ kt ] = change ( ls [ kt ] , kb << 1 , l , mid , x , y ) ;
	if ( y > mid )
		rs [ kt ] = change ( rs [ kt ] , kb << 1 | 1 , mid + 1 , r , x , y ) ;
	tr [ kt ] = tr [ ls [ kt ] ] + tr [ rs [ kt ] ] + lz [ kt ] * 1ll * ts [ kb ] ;
	return kt ;
}

long long query ( int u , int v , int kb , int l , int r , int x , int y ) {
	if ( x <= l && r <= y ) {
//		cout << " query : " << l << " - " << r << "\n" ;
//		cout << u << " , " << v << " : " << tr [ v ] << " - " << tr [ u ] << " : " << ts [ kb ] << "\n" ;
		lans += tr [ v ] - tr [ u ] ;
		return ts [ kb ] ;
	}
	int mid = ( l + r ) >> 1 ;
	long long res = 0 ;
	if ( x <= mid )
		res = query ( ls [ u ] , ls [ v ] , kb << 1 , l , mid , x , y ) ;
	if ( y > mid )
		res += query ( rs [ u ] , rs [ v ] , kb << 1 | 1 , mid + 1 , r , x , y ) ;
	lans += ( lz [ v ] - lz [ u ] ) * 1ll * res ;
	return res ;
}

void modify ( int k , int x ) {
	lim = tot ;
	while ( top [ x ] != 1 ) {
		rt [ k ] = change ( rt [ k ] , 1 , 1 , n , id [ top [ x ] ] , id [ x ] ) ;
		x = fa [ top [ x ] ] ;
	}
	if ( x != 1 )
		rt [ k ] = change ( rt [ k ] , 1 , 1 , n , 2 , id [ x ] ) ;
}

void find ( int u , int v , int x ) {
	while ( top [ x ] != 1 ) {
//		cout << " Query : " << id [ top [ x ] ] << " - " << id [ x ] << "\n" ;
		query ( u , v , 1 , 1 , n , id [ top [ x ] ] , id [ x ] ) ;
		x = fa [ top [ x ] ] ;
	}
	if ( x != 1 ) {
//		cout << " Query : " << 2 << " - " << id [ x ] << "\n" ;
		query ( u , v , 1 , 1 , n , 2 , id [ x ] ) ;
	}
}

void dft ( int k , int l , int r ) {
	cout << " dft : " << l << " - " << r << " : " << ts [ k ] << "\n" ;
	if ( l == r )
		return ;
	int mid = ( l + r ) >> 1 ;
	dft ( k << 1 , l , mid ) ;
	dft ( k << 1 | 1 , mid + 1 , r ) ;
}

void dfs ( int k , int l , int r ) {
	cout << " dfs : " << l << " - " << r << " : " << k << " : " << tr [ k ] << " , " << lz [ k ] << "\n" ;
	if ( l == r )
		return ;
	int mid = ( l + r ) >> 1 ;
	dfs ( ls [ k ] , l , mid ) ;
	dfs ( rs [ k ] , mid + 1 , r ) ;
}

signed main ( ) {
	n = read ( ) , m = read ( ) ;
	for ( int i = 1 ; i <= n ; ++ i )
		p [ i ] = read ( ) ;
	for ( int i = 1 ; i < n ; ++ i ) {
		int u = read ( ) , v = read ( ) , w = read ( ) ;
		insert ( u , v , w ) ;
	}
	dfs1 ( 1 , 0 ) ;
	dfs2 ( 1 , 1 ) ;
	build ( 1 , 1 , n ) ;
	
//	cout << "  fa : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << fa [ i ] << "  " ;
//	cout << "\n" ;
//	cout << "  id : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << id [ i ] << "  " ;
//	cout << "\n" ;
//	cout << "  wd : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << wd [ i ] << "  " ;
//	cout << "\n" ;
//	cout << " val : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << val [ i ] << "  " ;
//	cout << "\n" ;
//	cout << " vwd : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << val [ wd [ i ] ] << "  " ;
//	cout << "\n" ;
//	cout << " dis : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << dis [ i ] << "  " ;
//	cout << "\n" ;
//	cout << " DFT :\n" ;
//	dft ( 1 , 1 , n ) ;
//	cout << "\n" ;
	
	for ( int i = 1 ; i <= n ; ++ i ) {
		rt [ i ] = rt [ i - 1 ] ;
		modify ( i , p [ i ] ) ;
		sud [ i ] = sud [ i - 1 ] + dis [ p [ i ] ] ;
	}
	
//	cout << " sud : " ;
//	for ( int i = 1 ; i <= n ; ++ i )
//		cout << sud [ i ] << "  " ;
//	cout << "\n" ;
//	cout << " ans tree :\n" ;
//	for ( int i = 1 ; i <= n ; ++ i ) {
//		cout << "DFS : " << i << "\n" ;
//		dfs ( rt [ i ] , 1 , n ) ;
//		cout << "\n\n" ;
//	}
	
	for ( int i = 1 ; i <= m ; ++ i ) {
//		if ( i == m ) lans = 10350 ;
//		int op = read ( ) , x = read ( ) ;
		int op = read ( ) , x = read ( ) ^ lans ;
		
		if ( op == 1 ) {
//			int y = read ( ) , z = read ( ) ;
			int y = read ( ) ^ lans , z = read ( ) ^ lans ;
			
//			cout << " OP = 1 : " << x << " - " << y << " : " << z << "\n" ;
			lans = 0 ;
			find ( rt [ x - 1 ] , rt [ y ] , z ) ;
//			cout << " Find : " << lans << "\n" ;
			lans = sud [ y ] - sud [ x - 1 ] + dis [ z ] * ( y - x + 1 ) - lans * 2 ;
//			cout << " ********************** ans : " ;
			cout << lans << "\n" ;
			lans %= ( 1ll << 30 ) ;
		}
		else {
//			cout << " OP = 2 : " << x << "\n" ;
			rt [ x ] = rt [ x - 1 ] ;
			modify ( x , p [ x + 1 ] ) ;
			swap ( p [ x ] , p [ x + 1 ] ) ;
			sud [ x ] = sud [ x - 1 ] + dis [ p [ x ] ] ;
			
//			cout << "\n" ;
//			cout << " ans tree :\n" ;
//			for ( int j = 1 ; j <= n ; ++ j ) {
//				cout << "DFS : " << j << "\n" ;
//				dfs ( rt [ j ] , 1 , n ) ;
//				cout << "\n\n" ;
//			}
//			cout << "\n\n" ;
		}
	}
	return 0 ;
}
2022/5/4 16:32
加载中...