您的位置:首页 > 其它

hdu 4288 线段树

2014-08-11 13:44 176 查看

Coder

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2884 Accepted Submission(s): 1155



[align=left]Problem Description[/align]
  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
[align=left]Input[/align]
  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).

[align=left]Output[/align]
  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.

[align=left]Sample Input[/align]

9
add 1
add 2
add 3
add 4
add 5
sum
add 6
del 3
sum
6
add 1
add 3
add 5
add 7
add 9
sum


[align=left]Sample Output[/align]

3
4
5
HintC++ maybe run faster than G++ in this problem.


[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Chengdu Online

/*题目大意,有一个集合,集合内部是排好序的(升序),有三种操作:
1:add x,添加数 x ;(保证此操作前没有存在x而且1<=x<=1e9)
2:del x,删除数 x ;(保证此操作之前存在x)
3:sum,求当前集合里面,位置处于 i % 5==3 上面的那些数的和。
位置分五种情况(分别是除以5余1 2 3 4 0)在每个结点,保存该区间这五种位置
的数的和,每次添加,删除一个数,就更新这五个位置的和。然后为了保证线段树的
底部元素是有序的,我就考虑能不能把全部操作都读进来,把要添加的数进行一个排序,
去一下重,这样就知道有多少个数要添加到线段树了,线段树的大小于是也确定了。
同时因为添加的数x本身比较大, 但是数目比较少,所以进行离散化的话,就可以建起
树来了。同时我们可以发现,我们是预先给每个要添加的数预留了一个位置的,所以添加
数x表现为x在线段树中存在,删除x就表现为不存在x。最后难点就在于怎样更新每个节点
的五个位置的和。对于每一个节点,我们用一个数组sum[5]保存那5个和。那么对于底层节点,
如果数x存在,sum[0]=x,否则,sum[0]=0。那么对于中间的节点,我们怎么根据左右子
区间保存的和,更新自己的和?首先我们要弄清楚,节点保存的那个和,位置是以节点对应
区间的左端点为起点的。所以对于左子区间,其起点跟当前节点的起点是一样的,所以就直接
加到当前节点上,不用位置转移,也就是 leftson.sum[ i ] 直接加到 cur.sum[ i ]。
但是对于右子区间,它的起点是向右偏移了左子区间数目个数(leftson.num)那么多的长度,
所以右子区间要进行位置转移,rightson.sum[i]要加到 cur.sum[( i + leftson.num)%5]
上去。这样就更新了当前节点了。然后就可以轻松解决问题了*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<map>
#define N 100100
using namespace std;
struct node
{
int x,y,p,len;
long long sum[5];
} a[N*4];
int val
,b
;
map<int,int >rank;//避免了开数组
void build(int x,int y,int t)
{
int i;
a[t].x=x;
a[t].y=y;
a[t].p=0;
a[t].len=0;
for(i=0; i<5; i++)
a[t].sum[i]=0;
if(x==y)  return;
int mid=(x+y)>>1,temp=t<<1;
build(x,mid,temp);
build(mid+1,y,temp+1);
}
void update(int x,int val,int t)
{
int i;
if(a[t].x==a[t].y)
{
if(a[t].p)
{
a[t].p=0;
a[t].len=0;
a[t].sum[0]=0;
}
else
{
a[t].p=1;
a[t].len=1;
a[t].sum[0]=val;
}
return ;
}
int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
if(x<=mid)
update(x,val,temp);
else
update(x,val,temp+1);
a[t].len=a[temp].len+a[temp+1].len;
for(i=0; i<5; i++)
a[t].sum[i]=a[temp].sum[i];
int len=a[temp].len;
for(i=0; i<5; i++)
a[t].sum[(len+i)%5]+=a[temp+1].sum[i];
}
int main()
{
int n,i,k,j;
char str[100];
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
k=0;  rank.clear();
for(i=1; i<=n; i++)
{
scanf("%s",str);
if(strcmp(str,"add")==0)
{
scanf("%d",&val[i]);
b[k++]=val[i];
}
else if(strcmp(str,"del")==0)
{
scanf("%d",&val[i]);
val[i]=-val[i];
}
else
{
val[i]=0;
}
}
sort(b,b+k);
j=1;
rank[b[0]]=1;
for(i=1; i<k; i++)
{
if(b[i]!=b[i-1])
rank[b[i]]=++j;
}
build(1,j,1);
for(i=1; i<=n; i++)
{
if(val[i]==0)
printf("%I64d\n",a[1].sum[2]);
else  if(val[i]<0)
update(rank[-val[i]],val[i],1);
else
update(rank[val[i]],val[i],1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: