HDU5367-digger

digger

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


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
 


题意:连续大小一样的数被称为山,必须在它的左边和右边都有比它矮的山时,它被称为高山,有q次操作,操作是把区间都加上一个数,输出全部高山的总长度

解题思路:动态线段树



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, u, v, w, tot, hh;

struct node
{
	int lh, rh, lr, rl, lf, rf, l, r;
	void clear() { l = r = lh = rh = lr = rl = lf = rf = 0; }
}x[2000005];

int L[2000005], R[2000005], sum[2000005], h[2000005];

int newnode(int l, int r)
{
	sum[++tot] = 0;
	L[tot] = R[tot] = 0;
	x[tot].clear();
	x[tot].l = x[tot].rl = l;
	x[tot].r = x[tot].lr = r;
	return tot;
}

void Merge(int k, int l, int r, int ll, int rr)
{
	int llh = x[ll].lh + h[k];
	int lrh = x[ll].rh + h[k];
	int rlh = x[rr].lh + h[k];
	int rrh = x[rr].rh + h[k];
	int mid = (l + r) >> 1;
	x[k].lh = llh; x[k].rh = rrh;
	if (x[ll].lr == x[ll].r && llh == rlh)
	{
		x[k].lr = rr ? x[rr].lr : r;
		x[k].rf = x[rr].rf;
	}
	else
	{
		x[k].lr = ll ? x[ll].lr : mid;
		if (x[ll].lr == x[ll].r) x[k].rf = llh > rlh ? 1 : 0;
		else x[k].rf = x[ll].rf;
	}
	if (x[rr].rl == x[rr].l&&rrh == lrh)
	{
		x[k].rl = ll ? x[ll].rl : l;
		x[k].lf = x[ll].lf;
	}
	else
	{
		x[k].rl = rr ? x[rr].rl : (mid + 1);
		if (x[rr].rl == x[rr].l) x[k].lf = rrh > lrh ? 1 : 0;
		else x[k].lf = x[rr].lf;
	}
	sum[k] = sum[ll] + sum[rr];
	if (x[ll].rl > x[ll].l&&lrh > rlh&&x[ll].lf) sum[k] += x[ll].r - x[ll].rl + 1;
	if (x[rr].lr < x[rr].r&&rlh > lrh&&x[rr].rf) sum[k] += x[rr].lr - x[rr].l + 1;
	if (x[ll].rl > x[ll].l&&x[rr].lr < x[rr].r&&lrh == rlh&&x[ll].lf&&x[rr].rf) sum[k] += x[rr].lr - x[ll].rl + 1;
}

void update(int k, int l, int r, int ll, int rr, int val)
{
	if (ll <= l&&r <= rr) { x[k].lh+=val,x[k].rh+=val,h[k] += val; return; }
	int mid = (l + r) >> 1;
	if (ll <= mid)
	{
		if (!L[k]) L[k] = newnode(l, mid);
		update(L[k], l, mid, ll, rr, val);
	}
	if (rr > mid)
	{
		if (!R[k]) R[k] = newnode(mid + 1, r);
		update(R[k], mid + 1, r, ll, rr, val);
	}
	Merge(k, l, r, L[k], R[k]);
}

int main()
{
	while (~scanf("%d%d%d", &n, &m, &hh))
	{
		tot = 0;
		x[0].clear();
		tot = newnode(1, n);
		memset(h, 0, sizeof h);
		while (m--)
		{
			scanf("%d%d%d", &u, &v, &w);
			u ^= sum[1];
			v ^= sum[1];
			w ^= sum[1];
			if (u > v) swap(u, v);
			update(1, 1, n, u, v, w);
			printf("%d\n", sum[1]);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值