【HDU: Tunnel Warface】(线段树 | 连续区间计数 | 树上二分)

/**
 *  HDU:    1540 Tunnel Warface
 *  题目大意:  有N个连通地道,接下来有M个操作,
 *  D x -> 炸掉x号地道
 *  Q k -> 查询包括k的最长地道
 *  R   -> 修复最近依次被炸毁的地道
 *  数据范围:N, M <= 50000
 **/

/**
 *  HDU:    1540 Tunnel Warface
 *  题目大意:  有N个连通地道,接下来有M个操作,
 *  D x -> 炸掉x号地道
 *  Q k -> 查询包括k的最长地道
 *  R   -> 修复最近依次被炸毁的地道
 *  数据范围:N, M <= 50000
 **/
 
#include <array>
#include <stack>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <functional>
constexpr auto __MAX__ { static_cast<int>(5.0 * 1e5) };

std::array<int, __MAX__ << 2 | 1> extend {};
std::array<int, __MAX__ << 2 | 1> lstretch {};
std::array<int, __MAX__ << 2 | 1> rstretch {};
std::stack<int> recent {};
int N {}, M {};

constexpr int left(int x) noexcept { return x << 1; }
constexpr int right(int x) noexcept { return x << 1| 1; }

void update(int first, int last, int index) noexcept {
	extend[index] = std::ranges::max({
		extend[left(index)],
		extend[right(index)],
		std::plus<>{} (
			lstretch[right(index)],
			rstretch[left(index)])
		});
	int middle { (first + last) / 2 };
	lstretch[index] = lstretch[left(index)];
	if(lstretch[left(index)] == middle - first)
		lstretch[index] += lstretch[right(index)];
	rstretch[index] = rstretch[left(index)];
	if(rstretch[left(index)] == last - middle)
		rstretch[index] += rstretch[left(index)];
}

void change(int position, int value, int first, int last, int index) noexcept {
	if(first + 1== last)
		return (void) (lstretch[index] = rstretch[index] = extend[index] = value);
	int middle { (first + last) / 2 };
	if(position < middle) change(position, value, first, middle, left(index));
	else change(position, value, middle, last, right(index));
	update(first, last, index);
}


void build(int first, int last, int index) noexcept {
	if(first + 1 == last)
		return (void) (extend[index] = lstretch[index] = rstretch[index] = 1);
	int middle { (first + last)  / 2 };
	build(first, middle, left(index));
	build(middle, last, right(index));
	update(first, last, index);
}

int query(int key, int first, int last, int index) noexcept {
	if(first + 1 == last) return extend[index];
	int middle { (first + middle) / 2 };
	if(key < middle) {
		if(middle - rstretch[left(index)] <= key)
			return rstretch[left(index)] + lstretch[right(index)];
		else
			return query(key, first, middle, left(index));
	}else {
		if(key <= middle + lstretch[right(index)] - 1)
			return rstretch[left(index)] + lstretch[right(index)];
		else return query(key, middle, last, right(index));
	}
}

void handle_for_once(std::string & operation) {
	std::getline(std::cin, operation);
	if(operation.at(0) == 'R') {
		change(recent.top(), 1, 1, N + 1, 1);
		recent.pop();
	}else if(operation.at(0) == 'D') {
		int x {};
		std::scanf("%d", &x);
		change(x, 0, 1, N + 1, 1);
		recent.push(x);
	}else {
		int k {};
		std::scanf("%d", &k);
		std::printf("%d\n", query(k, 1, N + 1, 1));
	}
}

int main() {
	while(~std::scanf("%d%d", &N, &M) && !(N || M)) {
		build(1, N + 1, 1);
		std::string operation;
		while(M --)
			handle_for_once(operation);
	}
}
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XNB's Not a Beginner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值