【COGS】577 蝗灾 cdq分治
2014-09-09 15:14
302 查看
传送门:【COGS】577 蝗灾
题目分析:cdq分治入门题= =。。。。用差分思想将矩阵分成四块来计算。。排序一维,另一维用树状数组解决。
代码如下:
题目分析:cdq分治入门题= =。。。。用差分思想将矩阵分成四块来计算。。排序一维,另一维用树状数组解决。
代码如下:
#include <cstdio> #include <vector> #include <cstring> #include <algorithm> using namespace std ; #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 CPY( a , x ) memcpy ( a , x , sizeof a ) #define mid ( ( l + r ) >> 1 ) const int MAXN = 200005 ; const int MAXM = 500005 ; struct Node { int x1 , y1 , x2 , y2 ; int v ; int op ; } L[MAXN] , S[MAXN << 1] ; int n , m ; int ans[MAXN] ; int T[MAXM] ; void scanf ( int& x , char c = 0 ) { while ( ( c = getchar () ) < '0' || c > '9' ) ; x = c - '0' ; while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ; } void modify ( int x , int v ) { while ( x <= m ) { T[x] += v ; x += x & -x ; } } int sum ( int x , int ans = 0 ) { while ( x ) { ans += T[x] ; x -= x & -x ; } return ans ; } int cmp ( const Node& a , const Node& b ) { return a.x1 != b.x1 ? a.x1 < b.x1 : a.op < b.op ; } void cdq_solve ( int l , int r ) { if ( l == r ) return ; int m = mid , top = 0 ; cdq_solve ( l , m ) ; cdq_solve ( m + 1 , r ) ; FOR ( i , l , m ) if ( L[i].op == 1 ) S[top ++] = L[i] ; FOR ( i , m + 1 , r ) if ( L[i].op == 2 ) { S[top ++] = L[i] ; S[top - 1].x1 -- ; S[top ++] = L[i] ; S[top - 1].op = 3 ; S[top - 1].x1 = S[top - 1].x2 ; } sort ( S , S + top , cmp ) ; REP ( i , 0 , top ) { if ( S[i].op == 1 ) modify ( S[i].y1 , S[i].v ) ; else if ( S[i].op == 2 ) ans[S[i].v] -= sum ( S[i].y2 ) - sum ( S[i].y1 - 1 ) ; else ans[S[i].v] += sum ( S[i].y2 ) - sum ( S[i].y1 - 1 ) ; } REP ( i , l , m ) if ( L[i].op == 1 ) modify ( L[i].y1 , -L[i].v ) ; } void solve () { int ans_cnt = 0 ; CLR ( T , 0 ) ; CLR ( ans , 0 ) ; scanf ( m ) ; scanf ( n ) ; FOR ( i , 1 , n ) { scanf ( L[i].op ) ; if ( L[i].op == 1 ) scanf ( L[i].x1 ) , scanf ( L[i].y1 ) , scanf ( L[i].v ) ; else { scanf ( L[i].x1 ) , scanf ( L[i].y1 ) , scanf ( L[i].x2 ) , scanf ( L[i].y2 ) ; L[i].v = ans_cnt ++ ; } } cdq_solve ( 1 , n ) ; REP ( i , 0 , ans_cnt ) printf ( "%d\n" , ans[i] ) ; } int main () { freopen ( "locust.in" , "r" , stdin ) ; freopen ( "locust.out" , "w" , stdout ) ; solve () ; return 0 ; }
相关文章推荐
- COGS 577 蝗灾 [CDQ分治入门题]
- [COGS2479 && COGS2639]高维偏序(CDQ分治,bitset)
- COGS 577 蝗灾
- cogs 577 . 蝗灾 cdq分治
- COGS 2479. [HZOI 2016]偏序 [CDQ分治套CDQ分治 四维偏序]
- COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)
- cogs1752[boi2007]mokia 摩基亚 (cdq分治)
- COGS 577 蝗灾
- cogs577. 蝗灾(CDQ)
- cogs 577 蝗灾 CDQ分治
- cogs 577 蝗灾
- [COGS 577] 蝗灾
- COGS 577 蝗灾 cdq分治+树状数组
- CDQ分治入门---蝗灾、Mokia
- cdq分治入门学习 cogs 1752 Mokia nwerc 2015-2016 G 二维偏序
- COGS 577 蝗灾 线段树+CDQ分治
- COGS 577 蝗灾 线段树+CDQ分治
- BZOJ1492: [NOI2007]货币兑换Cash 【dp + CDQ分治】
- CDQ分治【分治(真得头疼)
- BZOJ1176 [Balkan2007]Mokia 【CDQ分治】