HDU 4288 Coder 线段树

来源:互联网 发布:管理文件的软件 编辑:程序博客网 时间:2024/05/16 06:40

Coder

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2747    Accepted Submission(s): 1096


Problem Description
  In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done.1
  You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
  By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
  1. add x – add the element x to the set;
  2. del x – remove the element x from the set;
  3. sum – find the digest sum of the set. The digest sum should be understood by

  where the set S is written as {a1, a2, ... , ak} satisfying a1 < a2 < a3 < ... < ak
  Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
 

Input
  There’re several test cases.
  In each test case, the first line contains one integer N ( 1 <= N <= 105 ), the number of operations to process.
  Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
  You may assume that 1 <= x <= 109.
  Please see the sample for detailed format.
  For any “add x” it is guaranteed that x is not currently in the set just before this operation.
  For any “del x” it is guaranteed that x must currently be in the set just before this operation.
  Please process until EOF (End Of File).
 

Output
  For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.
 

Sample Input
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
 

Sample Output
345
Hint
C++ maybe run faster than G++ in this problem.
 

Source
2012 ACM/ICPC Asia Regional Chengdu Online

传送门:HDU 4288 Coder

题目大意:
m次操作(m<=10^5),三种类型。
1.add x 向集合中添加元素x。
2.del x 从集合中删除元素x。
3.sum 询问集合中元素从小到大排列时的下标i % 5 == 3的数之和。
题目保证每个元素唯一。且元素大小不超过10^9。

题目分析:
由于x太大,数组开不下,所以我们将之离散化,因为每个元素唯一,则元素最多的时候就是add操作的次数n。那么区间范围就是【1,n】。
然后我们定义num[ o ],表示区间 o 内存在num[ o ]个元素。假设每个区间对于自己都是从下标为1开始,那么自身包含元素的下标 % 5 == 3 的和就是sum3。则父区间元素的下标%5==3的和就是:
sum = 左子区间下标 % 5 == 3 的元素和 + 右子区间下标 ==(( 5 - 左子区间元素个数 % 5 )+ 3 ) % 5 的元素和。
因此我们需要同时保存下标%5 == 0 ~ 4 的所有的元素和。
将sum[ ]变成sum[5][ ]就好了。

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;typedef long long Int ;#define rt l , r , o#define ls ( o << 1 )#define rs ( o << 1 | 1 )#define lson l , m , ls#define rson m + 1 , r , rs#define mid ( ( l + r ) >> 1 )#define root 1 , n , 1#define clear( A , X ) memset ( A , X , sizeof A )const int maxN = 100005 ;char ch[10] ;int q[maxN] ;//0:add,1:del,2:sumint a[maxN] ;int b[maxN] ;int num[maxN << 2] ;Int sum[5][maxN << 2] ;void PushUp ( int o ) {num[o] = num[ls] + num[rs] ;int tmp = 5 - num[ls] % 5 ;for ( int i = 0 ; i < 5 ; ++ i ) sum[i][o] = sum[i][ls] + sum[( tmp + i ) % 5][rs] ;}void Update ( int key , int x , int l , int r , int o ) {if ( l == r ) {sum[1][o] = x ? 0 : b[key] ;num[o] = !x;return ;}int m = mid ;if ( key <= m ) Update ( key , x , lson ) ;else Update ( key , x , rson ) ;PushUp ( o ) ;}int Init ( int m ) {int top = 0 ;for ( int i = 1 ; i <= m ; ++ i ) {scanf ( "%s" , ch ) ;if ( ch[0] == 'a' ) {q[i] = 0 ;scanf ( "%d" , &a[i] ) ;b[++ top] = a[i] ;}if ( ch[0] == 'd' ) {q[i] = 1 ;scanf ( "%d" , &a[i] ) ;}if ( ch[0] == 's' ) {q[i] = 2 ;}}sort ( b + 1 , b + top + 1 ) ;clear ( num , 0 ) ;clear ( sum , 0 ) ;return top ;}int Bin_Search ( int x , int l , int r ) {while ( l < r ) {int m = mid ;if ( b[m] >= x ) r = m ;else l = m + 1 ;}return l ;}void work () {int n , m , key ;while ( ~scanf ( "%d" , &m ) ) {n = Init ( m ) ;for ( int i = 1 ; i <= m ; ++ i ) {if ( 2 != q[i] ) {key = Bin_Search ( a[i] , 1 , n + 1 ) ;Update ( key , q[i] , root ) ;}else printf ( "%I64d\n" , sum[3][1] ) ;}}}int main () {work () ;return 0 ;}


0 0
原创粉丝点击