首页 > ACM题库 > HDU-杭电 > HDU 4288-Coder-线段树-[解题报告]HOJ
2015
05-23

HDU 4288-Coder-线段树-[解题报告]HOJ

Coder

问题描述 :

  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 �C add the element x to the set;
  2. del x �C remove the element x from the set;
  3. sum �C find the digest sum of the set. The digest sum should be understood by
Intelligent IME

  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

输入:

  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).

输出:

  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).

样例输入:

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

样例输出:

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

线段树学的远远不够啊,网络赛的时候一直不知道怎么做,后来看了大神报告,才学了一下。

线段树每个节点 记录 sum[0...5],即位置mod 5 的5种结果的数对应的和。cnt记录区间有多少个数。

更新的时候 只要把对应子区间的sum[i]加起来就可以了

#include<iostream>
#include<algorithm>
using namespace std;

#define MAXN 100010
#define lson u<<1
#define rson u<<1|1
typedef long long LL;

int dat[MAXN],tmp[MAXN];
char cmd[MAXN][5];

struct Node{
	int lef,rig;
	LL sum[5];
	int cnt;
}T[MAXN<<2];

void Build(int u,int l,int r){
	T[u].lef=l;
	T[u].rig=r;
	T[u].cnt=0;
	for(int i=0;i<5;i++)T[u].sum[i]=0;
	if(l==r)return;
	int mid=(l+r)>>1;
	Build(lson,l,mid);
	Build(rson,mid+1,r);
}

void PushUp(int u){
	for(int i=0;i<5;i++)T[u].sum[i]=T[lson].sum[i]+T[rson].sum[((i-T[lson].cnt)%5+5)%5];
}

void Update(int u,int pos,int val,int op){
	T[u].cnt+=op;
	if(T[u].lef==T[u].rig){
		T[u].sum[0]+=val;return;
	}
	else {
		if(pos<=T[lson].rig)Update(lson,pos,val,op);
		else Update(rson,pos,val,op);
		PushUp(u);
	}
}

int main(){
	int n;
	while(scanf("%d",&n)==1){

		int num=0;
		for(int i=0;i<n;i++){
			scanf("%s",cmd[i]);
			if(cmd[i][0]!='s'){scanf("%d",&dat[i]);tmp[num++]=dat[i];}
		}

		sort(tmp,tmp+num);
		num=unique(tmp,tmp+num)-tmp;

		Build(1,1,num);
		for(int i=0;i<n;i++){
			int pos=lower_bound(tmp,tmp+num,dat[i])-tmp;
			if(cmd[i][0]=='a')Update(1,pos,dat[i],1);
			else if(cmd[i][0]=='d')Update(1,pos,-dat[i],-1);
			else printf("%I64d\n",T[1].sum[2]);
		}
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/qingniaofy/article/details/7985933