BestCoder Round #50 digger(动态生成线段树)

来源:互联网 发布:网络公安报警电话 编辑:程序博客网 时间:2024/06/05 21:24

digger

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 335    Accepted Submission(s): 78


Problem Description
AFa have n mountains. These mountains in a line。All of mountains have same height initially. Every day , AFa would request ZJiaQ to cut some mountains or put stones in some mountains. And request ZJiaQ report how many mountains belong to “high mountain line”
when all of mountains in a range have same height, and higher than the nearest mountain in left and the nearest mountain in right. the range of mountains called “high mountain line”
of course ,the mountain in the most left and most right can’t be one of “high mountain line”
 

Input
  There are multiply case
In each case, the first line contains 3 integers: n(1<=n<=10^9) , q ( 1 <= q <= 50000), r(0 <= r<= 1000 ).n is the number of mountains. q is the number of days. r is the initial height .
In the next q lines, each line contains 3 integers: l, r, val(1<=l<=r<=n, -1000 <= val <= 1000). Means in the day the mountains’ height in range[l,r] have added by val. but you should let the l,r,val xor ans to get true l,r,val. ans is the answer you printed. 

 

Output
Print q lines, each line contains a single answer.
 

Sample Input
5 5 04 5 872 5 -483 3 174 5 -1715 5 -494
 

Sample Output
00011
 

Source
BestCoder Round #50 (div.2)
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5368 5363 5362 5361 5360 
 

解析:官方题解:
/*
对于求“高山脉”长度,可以利用线段树。树节点中保存左高度连续长度,左高度连续区间的高度,左高度连续区间的状态(即是否高于第一个高度不同的山),右高度连续长度,右高度连续区间的高度,右高度连续区间的状态,每段区间内的“高山脉”数量。每次更新时更新高度即可,在pushup过程中去计算新产生的“高山脉”。写起来难度不是很大,然后对于n很大且必须在线做这个条件只需对于线段树动态建立节点去维护即可
*/
这里解释一下什么是动态生成线段树。
       平时我们做的线段树,假设区间为[1,n],那我们通常都是直接以 1 号点表示区间【1,n】,以 i*2 号点表示 i 节点代表区间的左半区间,以 i*2+1 号点表示 i 节点多代表区间的右半区间,一半空间长度都定义为4*n。
        在本题中,n比较大,直接将所有线段树中的所有节点定义出来不现实,那我们注意到,这道题实际上是对区间进行的一系列操作,那么就可能存在一个区间【l,r】,在所有处理过程中,该区间都可以被当做一个整体来看待。   如果是这样的话,我们定义出与该区间的子区间相对应的节点就没有意义了。
        动态生成线段树就是:假设对某一节点 p (代表区间【l,r】)进行处理时,并不是对整个区间[l,r]进行处理,而是对其某个子区间进行处理,那这个时候,该区间就不能一直被当做一个整体来看待,所以生成两个初始子节点lp、rp(节点之均为初始值),表示该区间的左半部分与右半部分,然后父节点 p 在对两个新生成的子节点进行更新。

代码:
#include<cstdio>#define maxn 2000000using namespace std;int sum;int s[maxn],z[maxn],lson[maxn],rson[maxn];int lh[maxn],lc[maxn],rh[maxn],rc[maxn];bool lf[maxn],rf[maxn];void generate(int l,int r,int c){  sum++;  s[sum]=z[sum]=0;  lson[sum]=rson[sum]=0;  lh[sum]=rh[sum]=c;  lc[sum]=rc[sum]=r-l+1;  lf[sum]=rf[sum]=0;}void update(int p,int pl,int pr){  int lp=lson[p],rp=rson[p],mid=(pl+pr)/2;  s[p]=s[lp]+s[rp];  lh[p]=lh[lp],rh[p]=rh[rp];  lc[p]=lc[lp],rc[p]=rc[rp];  lf[p]=lf[lp],rf[p]=rf[rp];    if(lc[lp]==mid-pl+1)    {      if(lh[lp]==lh[rp])lc[p]+=lc[rp],lf[p]=lf[rp];      else lf[p]=lh[lp]>lh[rp];}  if(rc[rp]==pr-mid)    {      if(rh[rp]==rh[lp])rc[p]+=rc[lp],rf[p]=rf[lp];      else rf[p]=rh[rp]>rh[lp];}  if(rh[lp]>lh[rp] && rf[lp])s[p]+=rc[lp];  if(lh[rp]>rh[lp] && lf[rp])s[p]+=lc[rp];  if(rh[lp]==lh[rp] && rf[lp] && lf[rp])s[p]+=rc[lp]+lc[rp];}void add(int p,int pl,int pr,int l,int r,int d){  if(l<=pl && pr<=r)    {  z[p]+=d,lh[p]+=d,rh[p]+=d;  return;}  int lp=lson[p],rp=rson[p],mid=(pl+pr)/2;  if(lp==0)generate(pl,mid,0),lp=sum,lson[p]=sum;  if(rp==0)generate(mid+1,pr,0),rp=sum,rson[p]=sum;  if(z[p])    {      add(lp,pl,mid,pl,mid,z[p]);      add(rp,mid+1,pr,mid+1,pr,z[p]);      z[p]=0;}    if(r<=mid)add(lp,pl,mid,l,r,d);  if(l>mid)add(rp,mid+1,pr,l,r,d);  if(l<=mid && r>mid)    {      add(lp,pl,mid,l,r,d);      add(rp,mid+1,pr,l,r,d);}  update(p,pl,pr);}int main(){  int n,q,r,a,b,c,ans,i;  while(scanf("%d%d%d",&n,&q,&r)!=EOF)    {      ans=sum=0;      generate(1,n,0);      for(i=1;i<=q;i++)        {          scanf("%d%d%d",&a,&b,&c);  a^=ans,b^=ans,c^=ans;  add(1,1,n,a,b,c),ans=s[1];  printf("%d\n",ans);}}  return 0; }


0 0
原创粉丝点击