hdu 4107当卡段树「建议收藏」

hdu 4107当卡段树

大家好,又见面了,我是全栈君。

其核心思想是记录最大的节点值和最低值,假设max<p要么min>=p时间,在节点只变化add值,不要子树遍历;否则,就往子树递归。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

const int maxn = 2e5+50;
int N, P;

struct node{
	int l, r, Min, Max, add;
	int mid() { return (l+r)/2; }
}tree[maxn<<2];

int p, n;
void buildTree(int l, int r, int rt)
{
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].add = 0;
	tree[rt].Min = 0;
	tree[rt].Max = 0;
	if(l == r) return ;
	int mid = tree[rt].mid();
	buildTree(l, mid, rt<<1);
	buildTree(mid+1, r, rt<<1|1);
}

void update(int l, int r, int rt, int L, int R, int add)
{
	if(L <= l && R >= r)
	{
		if(tree[rt].Max < P)
		{
			tree[rt].add += add;
			tree[rt].Min += add;
			tree[rt].Max += add;
			return ;
		}
		else if(tree[rt].Min >= P)
		{
			tree[rt].add += 2*add;
			tree[rt].Min += 2*add;
			tree[rt].Max += 2*add;
			return ;
		}
	}
	if(tree[rt].add){
		tree[rt<<1].add += tree[rt].add;
		tree[rt<<1].Min += tree[rt].add;
		tree[rt<<1].Max += tree[rt].add; 
		tree[rt<<1|1].add += tree[rt].add;
		tree[rt<<1|1].Min += tree[rt].add;
		tree[rt<<1|1].Max += tree[rt].add;
		
		tree[rt].add = 0;
	}
	if(l == r) return ;
	int mid = tree[rt].mid();
	if(L <= mid) update(l, mid, rt<<1, L, R, add);
	if(R > mid) update(mid+1, r, rt<<1|1, L, R, add);
	
	tree[rt].Min = min(tree[rt<<1].Min, tree[rt<<1|1].Min);
	tree[rt].Max = max(tree[rt<<1].Max, tree[rt<<1|1].Max);
} 

void query(int l, int r, int rt)
{
	if(tree[rt].Min == tree[rt].Max){
		for(int i = l; i <= r; i ++)
			printf( i == N ? "%d\n" : "%d ", tree[rt].Min );
		return ;
	}
	if(tree[rt].add)
	{
		tree[rt<<1].add += tree[rt].add;
		tree[rt<<1].Min += tree[rt].add;
		tree[rt<<1].Max += tree[rt].add; 
		tree[rt<<1|1].add += tree[rt].add;
		tree[rt<<1|1].Min += tree[rt].add;
		tree[rt<<1|1].Max += tree[rt].add;
		
		tree[rt].add = 0;
	}	
	if(l == r) return ;
	int mid = tree[rt].mid();
	query(l, mid, rt<<1);
	query(mid+1, r, rt<<1|1);
}

int main()
{
	int n, m, p;
	int a, b, c;
	while(~scanf("%d%d%d", &n, &m, &p))
	{
		N = n;
		P = p;
		buildTree(1, n, 1);
		for(int i = 0; i < m; i ++)
		{
			scanf("%d%d%d", &a, &b, &c);
			update(1, n, 1, a, b, c);
		}
		query(1, n, 1);
	}
} 


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116662.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月17日 下午10:00
下一篇 2022年1月17日 下午10:00


相关推荐

  • 如何安装matplotlib并且在pycharm中使用

    如何安装matplotlib并且在pycharm中使用1 首先使用快捷键 win R 打开 cmd 2 键入下列任一个命令更新 pip 包 python mpipinstallu mpipinstall Upippipinsta mpipinstall Upipsetuptoo 本文键入第一个 python mpipinstall

    2026年3月27日
    1
  • netstat -an 结果分析

    netstat -an 结果分析netstat an 及其结果分析 netstat 结果前言 这几天由于病毒的日益流行 许多朋友开始对防毒和防黑重视起来 装了不少的病毒或网络防火墙 诚然 通过防火墙我们可以得到许多有关我们计算机的信息 不过 windows 自带的 netstat 更加小巧玲珑 可以让你不费吹灰之力就可以对本机的开放端口和连接信息一览无余 针对 netstat 命令的用

    2026年3月26日
    1
  • 深入理解C++中的mutable关键字

    深入理解C++中的mutable关键字mutable 的中文意思是 可变的 易变的 跟 constant 既 C 中的 const 是反义词 在 C 中 mutable 也是为了突破 const 的限制而设置的 被 mutable 修饰的变量 将永远处于可变的状态 即使在一个 const 函数中 我们知道 如果类的成员函数不会改变对象的状态 那么这个成员函数一般会声明成 const 的 但是 有些时候 我们需要在 const 的函数里面修改一些跟类状态

    2026年3月19日
    2
  • gridview分页显示_html分页显示数据

    gridview分页显示_html分页显示数据首先把CSS样式代码粘贴过来:.gv{   border:1pxsolid#D7D7D7;   font-size:12px;   text-align:center;}.gvHeader{   color:#3F6293;   background-color:#F7F7F7;   height:24px;   line-height:24px;   tex

    2025年11月27日
    6
  • linux查看防火墙装填,linux 查看防火墙状态

    linux查看防火墙装填,linux 查看防火墙状态1 查看防火墙状态 systemctlsta cmdstate 查看默认防火墙状态 关闭后显示 notrunning 开启后显示 running 2 查看防火墙是否开机启动 systemctlis enabledfirew 关闭防火墙 systemctlsto servi

    2026年3月18日
    2
  • windows安装配置openclaw

    windows安装配置openclaw

    2026年3月13日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号