本文主要是介绍hdu4288--Coder--线段树--离线处理+离散化+想法!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
做过的线段树做到现在收获最大的一题~~~
以后还要多做几遍~~~
学会了左加右减的位移思想,
学会了离线处理数据,
学会了用lower_bound或者upper_bound寻找hash中某个数值所在的数组下标~~
整道题的思路和注释都写在代码里了。
//HDU 4288 线段树离线+离散化
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxnum=1e5+10;char op[maxnum][5];
int num[maxnum];
int hash[maxnum];struct Node
{int l,r;//int ll,rr;本来想写实际ll和rr的,后来发现其实可以通过upper_bound找到插入元素的index,就不必每次都去考虑实际大小来进行插入了ll sum[5];//分别对应余数为0,1,2,3,4的情况int cnt;//结点对应的区间内实际有多少个数
}tree[maxnum*3];void build(int l,int r,int root)
{tree[root].l=l,tree[root].r=r,tree[root].cnt=0;for(int i=0;i<5;++i)tree[root].sum[i]=0;if(l==r) return;int mid=(l+r)>>1;build(l,mid,root<<1);build(mid+1,r,root<<1|1);
}void update(int root,int position,int adder)
{int l=tree[root].l,r=tree[root].r;if(l==r){if(adder>0){tree[root].cnt=1;tree[root].sum[1]=hash[position];}else if(adder<0){tree[root].cnt=0;tree[root].sum[1]=0;}}else{tree[root].cnt+=adder;//这里一开始二货了。。。线段树更新时一定要考虑每次究竟要更新哪些东西。int mid=(l+r)>>1;if(position<=mid) update(root<<1,position,adder);else if(position>mid) update(root<<1|1,position,adder);for(int i=0;i<5;++i)tree[root].sum[i]=tree[root<<1].sum[i]+tree[root<<1|1].sum[(i-tree[root<<1].cnt%5+5)%5];//这里有一个左加右减的思想= =#好奇妙的说。。。。//父结点mod 5=i的值来源于左结点的值,加上右结点中 mod 5=x,中的x的值,即tree[root<<1|1].sum[x]的值。//而x=(i-tree[root<<1].cnt%5+5)%5//为什么呢,因为左结点中的起始结点前面没有数,而右结点前面有tree[root<<1].cnt个数//因而相当于把右结点向右平移cnt个单位//根据左加右减的思想,实际求的x即为i-tree[root<<1].cnt在5域下的值。}
}int main()
{//freopen("input.txt","r",stdin);int n;while(~scanf("%d",&n)){int i;int p=1,q=2;for(i=1;i<=n;++i){scanf("%s",op[i]);if(op[i][0]!='s'){scanf("%d",&num[i]);//用num数组记录每次操作存入的数hash[p++]=num[i];//hash进行离散化}}//离线处理--p;sort(hash+1,hash+p+1);for(i=2;i<=p;++i) if(hash[i]!=hash[i-1]) hash[q++]=hash[i];--q;build(1,q,1);for(int i=1;i<=n;++i){if(op[i][0]=='s') {cout<<tree[1].sum[3];printf("\n");}else{int position=lower_bound(hash+1,hash+1+q,num[i])-hash;//low_bound返回第一个大于等于value的值,记住指针范围!!!//或者写int position=upper_bound(hash+1,hash+1+q,num[i])-(hash+1);if(op[i][0]=='a') update(1,position,1);else if(op[i][0]=='d') update(1,position,-1);}}}return 0;
}
这篇关于hdu4288--Coder--线段树--离线处理+离散化+想法!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!