传送门:【HDU】5126 stars
题目分析:将一个立方体查询拆成八个查询,然后对所有的操作+询问进行cdq分治。
由于是三维的,所以用cdq套cdq解决两维,最后一维用树状数组维护。
具体写法看代码好了,简单易懂。
cdq分治就是神,每套一层cdq分治,就可以将一维变成一个log。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) #define clr( a , x ) memset ( a , x , sizeof a ) #define ls ( o << 1 ) #define rs ( o << 1 | 1 ) #define lson ls , l , m #define rson rs , m + 1 , r #define root 1 , 1 , cnt #define mid ( ( l + r ) >> 1 ) const int MAXN = 50005 ; struct Node { int x , y , z , f , idx ; Node () {} Node ( int x , int y , int z , int f , int idx ) : x ( x ) , y ( y ) , z ( z ) , f ( f ) , idx ( idx ) {} } ; Node q[MAXN * 8] , s1[MAXN * 8] , s2[MAXN * 8] , s[MAXN * 8] ; int top1 , top2 , top ; int m ; int a[MAXN << 1] , cnt ; int T[MAXN << 1] ; int ans[MAXN] ; int n ; int unique ( int n ) { int cnt = 1 ; sort ( a + 1 , a + n + 1 ) ; For ( i , 2 , n ) if ( a[i] != a[cnt] ) a[++ cnt] = a[i] ; return cnt ; } int hash ( int x , int l = 1 , int r = cnt ) { while ( l < r ) { int m = mid ; if ( a[m] >= x ) r = m ; else l = m + 1 ; } return l ; } bool cmpz ( const Node& a , const Node& b ) { if( a.z != b.z ) return a.z < b.z ; return a.idx < b.idx ; } bool cmpy ( const Node& a , const Node& b ) { if ( a.y != b.y ) return a.y < b.y ; return a.idx < b.idx ; } void add ( int x , int v ) { for ( int i = x ; i <= cnt ; i += i & -i ) T[i] += v ; } int sum ( int x , int ans = 0 ) { for ( int i = x ; i > 0 ; i -= i & -i ) ans += T[i] ; return ans ; } void cdq_fz2 ( int l , int r ) { if ( r <= l ) return ; int m = mid ; cdq_fz2 ( l , m ) ; cdq_fz2 ( m + 1 , r ) ; top1 = top2 = 0 ; For ( i , l , m ) if ( !s[i].f ) s1[top1 ++] = s[i] ; For ( i , m + 1 , r ) if ( s[i].f ) s2[top2 ++] = s[i] ; sort ( s1 , s1 + top1 , cmpy ) ; sort ( s2 , s2 + top2 , cmpy ) ; int j = 0 ; rep ( i , 0 , top2 ) { while ( j < top1 && s1[j].y <= s2[i].y ) add ( s1[j ++].x , 1 ) ; ans[s2[i].idx] += s2[i].f * sum ( s2[i].x ) ; } rep ( i , 0 , j ) add ( s1[i].x , -1 ) ;; } void cdq_fz ( int l , int r ) { if ( r <= l ) return ; int m = mid ; cdq_fz ( l , m ) ; cdq_fz ( m + 1 , r ) ; int top = 0 ; For ( i , l , m ) if ( !q[i].f ) s[top ++] = q[i] ; For ( i , m + 1 , r ) if ( q[i].f ) s[top ++] = q[i] ; sort ( s , s + top , cmpz ) ; cdq_fz2 ( 0 , top - 1 ) ; } void solve () { int op ; int x1 , y1 , z1 ; int x2 , y2 , z2 ; clr ( T , 0 ) ; clr ( ans , 0 ) ; cnt = 0 ; m = 0 ; scanf ( "%d" , &n ) ; For ( i , 1 , n ) { scanf ( "%d" , &op ) ; if ( op == 1 ) { scanf ( "%d%d%d" , &x1 , &y1 , &z1 ) ; a[++ cnt] = x1 ; q[++ m] = Node ( x1 , y1 , z1 , 0 , 0 ) ; ans[i] = -1 ; } else { scanf ( "%d%d%d%d%d%d" , &x1 , &y1 , &z1 , &x2 , &y2 , &z2 ) ; q[++ m] = Node ( x2 , y2 , z2 , 1 , i ) ; q[++ m] = Node ( x2 , y2 , z1 - 1 , -1 , i ) ; q[++ m] = Node ( x1 - 1 , y2 , z2 , -1 , i ) ; q[++ m] = Node ( x2 , y1 - 1 , z2 , -1 , i ) ; q[++ m] = Node ( x2 , y1 - 1 , z1 - 1 , 1 , i ) ; q[++ m] = Node ( x1 - 1 , y2 , z1 - 1 , 1 , i ) ; q[++ m] = Node ( x1 - 1 , y1 - 1 , z2 , 1 , i ) ; q[++ m] = Node ( x1 - 1 , y1 - 1 , z1 - 1 , -1 , i ) ; a[++ cnt] = x2 ; a[++ cnt] = x1 - 1 ; } } cnt = unique ( cnt ) ; For ( i , 1 , m ) q[i].x = hash ( q[i].x ) ; cdq_fz ( 1 , m ) ; For ( i , 1 , n ) if ( ~ans[i] ) printf ( "%d\n" , ans[i] ) ; } int main () { int T ; scanf ( "%d" , &T ) ; while ( T -- ) solve () ; return 0 ; }
--------------------------------update--------------------------------
给一个CDQ套BIT套Treap的代码吧。常数小的不得了。
代码如下:
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) #define clr( a , x ) memset ( a , x , sizeof a ) #define lson l , m #define rson m + 1 , r #define mid ( ( l + r ) >> 1 ) const int MAXN = 50005 ; struct Query { int x1 , y1 ; int x2 , y2 ; int z , f , idx ; Query () {} Query ( int x1 , int y1 , int x2 , int y2 , int z , int f , int idx ) : x1 ( x1 ) , y1 ( y1 ) , x2 ( x2 ) , y2 ( y2 ) , z ( z ) , f ( f ) , idx ( idx ) {} } ; struct Node* null ; struct Node { Node* c[2] ; int r ;//greater root int sum ; int v ; int key ; void newnode ( int x , int value ) { r = rand () ; key = x ; sum = v = value ; c[0] = c[1] = null ; } void push_up () { sum = c[0]->sum + v + c[1]->sum ; } } ; Query opp[MAXN * 2] , s1[MAXN * 2] , s2[MAXN * 2] ; Node pool[MAXN * 20] ; Node* cur ; Node* T[MAXN << 1] ; int vis[MAXN << 1] , Time ; int a[MAXN << 1] , cnt ; int ans[MAXN] ; int n , m , q ; void rot ( Node* &o , int d ) { Node* ch = o->c[d ^ 1] ; o->c[d ^ 1] = ch->c[d] ; ch->c[d] = o ; o->push_up () ; ch->push_up () ; o = ch ; } void insert ( Node* &o , int x , int v ) { if ( o == null ) { o = cur ++ ; o->newnode ( x , v ) ; } else if ( o->key == x ) { o->v += v ; } else { int d = ( o->key < x ) ; insert ( o->c[d] , x , v ) ; if ( o->c[d]->r > o->r ) rot ( o , d ^ 1 ) ; } o->push_up () ; } int search ( Node* o , int x , int ans = 0 ) { while ( o != null ) { if ( x < o->key ) { o = o->c[0] ; } else { ans += o->c[0]->sum + o->v ; o = o->c[1] ; } } return ans ; } int unique ( int n ) { int cnt = 1 ; sort ( a + 1 , a + n + 1 ) ; For ( i , 2 , n ) if ( a[i] != a[cnt] ) a[++ cnt] = a[i] ; return cnt ; } int hash ( int x , int l = 1 , int r = cnt ) { while ( l < r ) { int m = mid ; if ( a[m] >= x ) r = m ; else l = m + 1 ; } return l ; } int cmpz ( const Query& a , const Query& b ) { if ( a.z != b.z ) return a.z < b.z ; return a.idx < b.idx ; } void add ( int x , int y , int v ) { for ( int i = x ; i <= cnt ; i += i & -i ) { if ( vis[i] != Time ) { T[i] = null ; vis[i] = Time ; } insert ( T[i] , y , v ) ; } } int sum ( int x , int y , int ans = 0 ) { for ( int i = x ; i > 0 ; i -= i & -i ) if ( vis[i] == Time ) ans += search ( T[i] , y ) ; return ans ; } void cdq_fz ( int l , int r ) { if ( r <= l ) return ; int m = mid , top1 = 0 , top2 = 0 , j = 0 ; cdq_fz ( lson ) ; cdq_fz ( rson ) ; For ( i , m + 1 , r ) if ( opp[i].idx ) s1[top1 ++] = opp[i] ; For ( i , l , m ) if ( !opp[i].idx ) s2[top2 ++] = opp[i] ; sort ( s1 , s1 + top1 , cmpz ) ; sort ( s2 , s2 + top2 , cmpz ) ; ++ Time ; cur = pool + 1 ; rep ( i , 0 , top1 ) { while ( j < top2 && s2[j].z <= s1[i].z ) { add ( s2[j].x2 , s2[j].y2 , s2[j].f ) ; ++ j ; } ans[s1[i].idx] += s1[i].f * sum ( s1[i].x2 , s1[i].y2 ) ; ans[s1[i].idx] -= s1[i].f * sum ( s1[i].x1 - 1 , s1[i].y2 ) ; ans[s1[i].idx] -= s1[i].f * sum ( s1[i].x2 , s1[i].y1 - 1 ) ; ans[s1[i].idx] += s1[i].f * sum ( s1[i].x1 - 1 , s1[i].y1 - 1 ) ; } } void init () { m = 0 ; cnt = 0 ; //srand ( 233333333 ) ; cur = pool ; null = cur ++ ; null->c[0] = null->c[1] = null ; null->sum = null->v = null->r = 0 ; } void scanf ( int& x , char c = 0 ) { while ( ( c = getchar () ) < '0' ) ; x = c - '0' ; while ( ( c = getchar () ) >= '0' ) x = x * 10 + c - '0' ; } void solve () { int op , x1 , y1 , z1 , x2 , y2 , z2 ; init () ; scanf ( n ) ; For ( i , 1 , n ) { scanf ( "%d" , &op ) ; if ( op == 1 ) { scanf ( x1 ) ; scanf ( y1 ) ; scanf ( z1 ) ; opp[++ m] = Query ( x1 , y1 , x1 , y1 , z1 , 1 , 0 ) ; a[++ cnt] = x1 ; ans[i] = -1 ; } else { scanf ( x1 ) ; scanf ( y1 ) ; scanf ( z1 ) ; scanf ( x2 ) ; scanf ( y2 ) ; scanf ( z2 ) ; opp[++ m] = Query ( x1 , y1 , x2 , y2 , z2 , 1 , i ) ; opp[++ m] = Query ( x1 , y1 , x2 , y2 , z1 - 1 , -1 , i ) ; a[++ cnt] = x2 ; a[++ cnt] = x1 - 1 ; ans[i] = 0 ; } } cnt = unique ( cnt ) ; For ( i , 1 , m ) { if ( !opp[i].idx ) opp[i].x1 = opp[i].x2 = hash ( opp[i].x1 ) ; else { opp[i].x1 = hash ( opp[i].x1 - 1 ) + 1 ; opp[i].x2 = hash ( opp[i].x2 ) ; } } cdq_fz ( 1 , m ) ; For ( i , 1 , n ) if ( ~ans[i] ) printf ( "%d\n" , ans[i] ) ; } int main () { int T ; Time = 0 ; clr ( vis , 0 ) ; scanf ( T ) ; while ( T -- ) solve () ; return 0 ; }