hdu4288(线段树)
来源:互联网 发布:JAVA对象哈希值 编辑:程序博客网 时间:2024/06/13 12:41
Coder
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2096 Accepted Submission(s): 858
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
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).
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
345HintC++ maybe run faster than G++ in this problem.本题即将区间内的元素按升序排列,然后求下标对5取摸余3的数的和,由于需要升序排列,当来一个数的时候就把他插入到适当位置,这就要求实现读入所有数据,然后离散化处理建立区间,当加操作时就把元素插到适当区间,当修改元素时就把适当位置置零,关键是求和时候的根据下标对5取摸余3,由于是递归,先左子树后右子树,所以左边的不变,右边的需(i-tree[id*2].num%5+5)%5,具体数学道理有待想想,本题关键是先离散化处,防止MLT#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int MAX=100000+10;char cmd[MAX][5];__int64 a[MAX];__int64 ta[MAX];int da[MAX];struct node {int left,right,num;__int64 sum[5];}tree[MAX*3];void build( int id, int left, int right ){ tree[id].left = left; tree[id].right = right;tree[id].num=0;memset(tree[id].sum,0,sizeof(tree[id].sum)); if( left == right ) { return ; } else { int mid = ( left + right )/2; build( id * 2, left, mid ); build( id * 2 + 1, mid + 1, right ); }}void pushup(int id){int i;for(i=0;i<5;i++)tree[id].sum[i]=tree[id*2].sum[i]+tree[id*2+1].sum[(i-tree[id*2].num%5+5)%5];//tree[id*2+1].sum[(1+i+tree[id*2].num%5)%5];}void updata(int id,int pos,bool flag){if(flag)tree[id].num++;else tree[id].num--; if(tree[id].left==tree[id].right ) {if(flag)tree[id].sum[1]=ta[pos];else tree[id].sum[1]=0; return ; } int mid = (tree[id].left+tree[id].right) /2; if(pos<=mid) updata( id * 2,pos,flag); else updata( id * 2 + 1,pos,flag);pushup(id);}int main(){int n,i,num,cnt;while(~scanf("%d",&n)){num=0;for(i=0;i<n;i++){scanf("%s",cmd[i]);if(cmd[i][0]!='s'){scanf("%d",&a[i]);ta[num]=a[i];num++;}}sort(ta,ta+num);int cnt=unique(ta,ta+num)-ta;build(1,1,cnt);//cout<<cnt<<endl;for(i=0;i<n;i++){int ind=lower_bound(ta,ta+cnt,a[i])-ta;//printf(" %I64d pos=%d\n",a[i],ind);if(cmd[i][0]=='a'){updata(1,ind,true);}else if(cmd[i][0]=='d'){updata(1,ind,false);}else{ printf("%I64d\n",tree[1].sum[3]);}}}return 0;}
- hdu4288线段树
- hdu4288(线段树)
- hdu4288 Coder 线段树
- hdu4288 离线处理线段树
- HDU4288线段树+离散化
- HDU4288线段树+离散化
- hdu4288 线段树之点更新
- hdu4288之线段树单点更新
- hdu4288 线段树+离线化+离散化
- 线段树点更新(好)hdu4288
- HDU4288-Coder(线段树+离线+离散化)
- hdu4288 Coder(线段树+离散化)
- hdu4288(线段树维护多个sum)
- HDU4288 Coder(离线+线段树)
- hdu4288--Coder--线段树--离线处理+离散化+想法!
- HDU4288:Coder(线段树单点更新版 && 暴力版)
- HDU4288
- 多颗线段树+间隔点组成的区间求和 hdu4288 coder
- The Architecture of Open Source Applications---VTK
- [unity3D基础教程]Unity3D材质与着色器 Materials and Shaders
- 改写整数
- 刚子扯谈 活着 没那么简单
- 第三章数程序设计初步--控制结构综合项目2-2输出千以内的素数
- hdu4288(线段树)
- Android中ListView的各种显示效果
- 刚子扯个蛋 说下增、删、改、查
- android 开发环境配置
- object-c的静态变量(static)
- 10个步骤让你成为高效的Web开发者
- Hdoj1863畅通工程
- sqlserver数据库连接池配置和运用总结
- UESTC 1711 Divide 解题报告