蒟蒻求助此题
查看原帖
蒟蒻求助此题
281668
FOX_konata楼主2021/11/27 11:35
#include <bits/stdc++.h>
#define For( i , j , k ) for( ll i = ( j ) ; i <= ( k ) ; ++ i )
#define For__( i , j , k ) for( ll i = ( j ) ; i >= ( k ) ; -- i )
#define Fore( i , j , k ) for( ll i = j ; i ; i = k )
#define ll long long
#define lf double
#define llf long double
#define random( l , r ) ( ( 1ll * rand() * rand() % ( r - l + 1 ) + rand() ) % ( r - l + 1 ) + l )
using namespace std;
//-------------------------Fastio-------------------------//
/*
namespace Fread {
	const int SIZE = 1 << 16;
	char buf[ SIZE ], *S, *T;
	inline char getchar() {
		if ( S == T ) {
			T = ( S = buf ) + fread( buf , 1 , SIZE , stdin );
			if ( S == T ) return '\n';
		}
		return *S ++;
	}
}
namespace Fwrite {
	const int SIZE = 1 << 16;
	char buf[ SIZE ], *S = buf, *T = buf + SIZE;
	inline void flush() {
		fwrite( buf , 1 , S - buf , stdout );
		S = buf;
	}
	inline void putchar(char c) {
		*S ++ = c;
		if ( S == T ) flush();
	}
	struct NTR {
		~ NTR() {
			flush();
		}
	} ztr;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread :: getchar
	#define putchar Fwrite :: putchar
#endif
*/
ll trash;
template < typename T >
inline T read( T &num ){
	num = 0 ; T f = 1 ; char c = ' ';
	while( c < '0' || c > '9' ) if( ( c = getchar() ) == '-' ) f = -1;
	while( c >= '0' && c <= '9' ) num = ( num << 1 ) + ( num << 3 ) + ( c ^ 48 ) , c = getchar();
	num *= f;
	return num;
}
//-------------------------Fastio end-------------------------//
const ll maxn = 2e5 + 5;
const ll maxlg = 25;
ll n , m;
ll a[ maxn ];
namespace Line_Tree{
	#define ls ( p << 1 )
	#define rs ( p << 1 | 1 )
	struct Tree{
		short two[ maxlg ] , tag[ maxlg ];
		Tree( ll x_ = 0 ){
			memset( two , 0 , sizeof( two ) );
			memset( tag , 0 , sizeof( tag ) );
			ll cnt = 0;
			while( x_ ){
				two[ ++ cnt ] = x_ & 1;
				x_ >>= 1;
			}
		}
	}trees[ maxn << 2 ];
	inline void push_up( ll p ){//上传值 
		memset( trees[ p ].two , 0 , sizeof( trees[ p ].two ) );
		For( i , 1 , 20 ){
			trees[ p ].two[ i ] += trees[ ls ].two[ i ] + trees[ rs ].two[ i ];
			trees[ p ].two[ i + 1 ] += trees[ p ].two[ i ] / 2;//二进制进位 
			trees[ p ].two[ i ] %= 2;
		}
		return ;
	}
	inline void f( ll l , ll r , ll k , ll p ){
		ll cnt = 0;
		while( k ){//把x变成tag,一一修改 
			trees[ p ].tag[ ++ cnt ] ^= k & 1;
			trees[ p ].two[ cnt ] ^= k & 1;
			k >>= 1;
		}
		return ;
	}
	inline void push_down( ll l , ll r , ll p ){//下传 
		ll x = 0;
		For__( i , 20 , 1 ){//先把tag变成x 
			x <<= 1;
			x |= trees[ p ].tag[ i ];
		}
		if( !x ) return ;
		ll mid = l + r >> 1;
		f( l , mid , x , ls );//把x递归 
		f( mid + 1 , r , x , rs );
		memset( trees[ p ].tag , 0 , sizeof( trees[ p ].tag ) );//记得清空 
		return ;
	}
	inline void build( ll l , ll r , ll p ){
		if( l == r ){
			ll t = a[ l ] , cnt = 0;
			while( t ){//把a[l]二进制分解后存到two里 
				trees[ p ].two[ ++ cnt ] = t & 1;
				t >>= 1;
			}
			return ;
		}
		ll mid = l + r >> 1;
		build( l , mid , ls );
		build( mid + 1 , r , rs );
		push_up( p );
		return ;
	}
	inline void update( ll l , ll r , ll x , ll y , ll k , ll p ){
		if( x <= l & r <= y ){
			f( l , r , k , p );
			return ;
		}
		push_down( l , r , p );
		ll mid = l + r >> 1;
		if( x <= mid ) update( l , mid , x , y , k , ls );
		if( mid < y ) update( mid + 1 , r , x , y , k , rs );
		push_up( p );
		return ;
	}
	inline ll query( ll l , ll r , ll x , ll y , ll p ){
		if( x <= l && r <= y ){
			ll t = 0;
			For__( i , 20 , 1 ){//把二进制转换成原来的数 
				t <<= 1;
				t |= trees[ p ].two[ i ];
			}
			return t;
		}
		push_down( l , r , p );
		ll mid = l + r >> 1;
		ll sum = 0;
		if( x <= mid ) sum += query( l , mid , x , y , ls );
		if( mid < y ) sum += query( mid + 1 , r , x , y , rs );
		return sum;
	}
	#undef ls
	#undef rs
};
int main(){
	read( n );
	For( i , 1 , n )
		read( a[ i ] );
	Line_Tree :: build( 1 , n , 1 );
	read( m );
	ll opt , l_ , r_ , k_;
	For( i , 1 , m ){
		read( opt ) , read( l_ ) , read( r_ );
		if( opt == 1 )
			printf( "%lld\n" , Line_Tree :: query( 1 , n , l_ , r_ , 1 ) );
		if( opt == 2 ){
			read( k_ );
			Line_Tree :: update( 1 , n , l_ , r_ , k_ , 1 );
		}
	}
	return 0;
}

求助,样例1的第4个询问一直不对

2021/11/27 11:35
加载中...