【Tsinsen】A1499. Theresa与数据结构 cdq分治套树状数组套平衡树

10 篇文章 0 订阅
4 篇文章 0 订阅

传送门:【Tsinsen】A1499. Theresa与数据结构


题目分析:三维平面的统计问题,用cdq分治套cdq分治套树状数组会超时= =。。。最后写了一个cdq套树状数组套treap过的。。。。思想和HDU5126一样。


代码如下:


#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 = 100005 ;

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] , s[MAXN] , s1[MAXN * 2] , s2[MAXN * 2] ;
Node pool[MAXN * 60] ;
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 == 0 ) 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 ;
	cur = pool ;
	null = cur ++ ;
	null->c[0] = null->c[1] = null ;
	null->sum = null->v = null->r = 0 ;
}

void solve () {
	int x1 , y1 , z1 , x2 , y2 , z2 , r ;
	char op[10] ;
	int top = 0 ;
	init () ;
	For ( i , 1 , n ) {
		scanf ( "%d%d%d" , &x1 , &y1 , &z1 ) ;
		opp[++ m] = Query ( x1 , y1 , x1 , y1 , z1 , 1 , 0 ) ;
		a[++ cnt] = x1 ;
	}
	scanf ( "%d" , &q ) ;
	For ( i , 1 , q ) {
		scanf ( "%s" , op ) ;
		if ( op[0] == 'A' ) {
			scanf ( "%d%d%d" , &x1 , &y1 , &z1 ) ;
			opp[++ m] = Query ( x1 , y1 , x1 , y1 , z1 , 1 , 0 ) ;
			a[++ cnt] = x1 ;
			s[top ++] = Query ( x1 , y1 , x1 , y1 , z1 , -1 , 0 ) ;
			ans[i] = -1 ;
		} else if ( op[0] == 'C' ) {
			opp[++ m] = s[-- top] ;
			ans[i] = -1 ;
		} else {
			scanf ( "%d%d%d%d" , &x1 , &y1 , &z1 , &r ) ;
			x2 = x1 + r ;
			y2 = y1 + r ;
			z2 = z1 + r ;
			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 , q ) if ( ~ans[i] ) printf ( "%d\n" , ans[i] ) ;
}

int main () {
	Time = 0 ;
	clr ( vis , 0 ) ;
	while ( ~scanf ( "%d" , &n ) ) solve () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值