【HDU】【5376】

digger

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


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 0 4 5 87 2 5 -48 3 3 17 4 5 -171 5 5 -494
 

Sample Output
  
  
0 0 0 1 1
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5368  5366  5365  5364  5363 


【L,R)  类似于分布函数,,, 或者是导数。。。 如果 > 0则递增,  < 0 则递减。

然后分类讨论



#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;

#define X first
#define Y second

map<int,int> m;

int n,q,r;
int ans = 0;
const int INF = 1000000000;
void modify(int x,int v)
{
   if(v == 0) return;
    map<int,int>::iterator it,L,R;
   if(m[x] == 0)
   {
       it = L = R = m.find(x);
       -- L;
       ++ R;
      if(v > 0)
      {
          if (L->Y < 0 && R->Y < 0)    
          	  ans += (R->X -x);
          else if (L->Y < 0 && R->Y > 0) 
              ans = ans;
          else if (L->Y > 0 && R->Y < 0) 
              ans -= (x - L->X);
          else if (L->Y > 0 && R->Y > 0)  
              ans = ans;
      }
      else
      {
          if (L->Y < 0 && R->Y < 0)    
              ans = ans;
          else if (L->Y < 0 && R->Y > 0) 
              ans = ans;
          else if (L->Y > 0 && R->Y < 0)
              ans -= (R->X - x);
          else if (L->Y > 0 && R->Y > 0) 
              ans += (x - L->X);
      }
      m[x] = v;
   }
   else
   {
      it = m.find(x);
      if(it->Y > 0 && it->Y + v > 0 || it->Y < 0 && it->Y + v < 0)
      {
          it->Y += v;
          return;
      }
      L = R = it;
      -- L;
      ++ R;
      int v1 = it->Y;
      int v2 = (it->Y)+ v;
      if((it->Y)> 0)
      {
          if (L->Y < 0 && R->Y < 0)    
              ans -= (R->X - x);
          else if (L->Y < 0 && R->Y > 0) 
              ans = ans;
          else if (L->Y > 0 && R->Y < 0) 
              ans += (x - L->X);
          else if(L->Y > 0 && R->Y > 0)  
              ans = ans;
      }
      else
      {
          if (L->Y < 0 && R->Y < 0)    
              ans = ans;
          else if (L->Y < 0 && R->Y > 0) 
              ans = ans;
          else if (L->Y > 0 && R->Y < 0) 
              ans += (R->X - x);
          else if(L->Y > 0 && R->Y > 0)   
              ans -= (x - L->X);
      }

      m.erase(it);
      modify(x,v2);
        
   }
}
int main()
{
    
    while(scanf("%d%d%d",&n,&q,&r) != EOF)
    {
        ans = 0;
        m.clear();
        m[1] = -INF;
        m[n + 1] = INF;
        for (int i=0;i<q;i++)
        {
            int l,r,v;
            scanf("%d%d%d",&l,&r,&v);

           
            l ^= ans;
            r ^= ans;
            v ^= ans;

   		  if(l > r) swap(l,r);
            modify(l,v);
            modify(r+1,-v);

            printf("%d\n",ans);

        }    
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值