The 2021 ICPC Asia Regionals Online Contest (I) D Edge of Taixuan (思维+线段树)

D Edge of Taixuan

Fu Hua, a network engineer who comes from Mount Taixuan, has a very important mission:
After years of slow internet and broken promises, Mount Taixuan finally has a village head who determines to upgrade the internet with broadband.
To build a fully connected world and deliver the benefits of technology to everyone and everywhere, Fu Hua returns to Mount Taixuan to help the village head to set up a strong network.
There are N village buildings at Mount Taixuan, and they are labelled with integers from 1 to N.
Shi Bao, the village head of Mount Taixuan, is responsible for the network maintenance. Her office locates at building 1.
Fu Hua already finished most work on the network setting, and Shi Bao’s office (building 1) has connected to broadband as the network gateway of Mount Taixuan. The only remaining work is to connect all other buildings (building 2,…,N) to building 1 with bidirectional cables.
Shi Bao said that she is more familiar with Mount Taixuan and can finish the cable settlement better than Fu Hua. Considering that it is indeed not a difficult task, Fu Hua agreed and gave all cables to Shi Bao. Fu Hua then left Mount Taixuan and was scheduled to come back for network testing in M days.
M days have passed. Fu Hua returns to Mount Taixuan and finds that Shi Bao has messed up the network connections: too many cables are connected between buildings and the network is overloaded. No one is able to access the internet now.
Shi Bao tells Fu Hua how she managed to connect the buildings with cables:
Before the first day, there is no cable connection between any two village buildings.
On the i-th day after Fu Hua left, Shi Bao chooses three integer Li,Ri,Wi such that 1≤Li<Ri≤N,and 1≤Wi≤1e5. Then for all pair (u,v) such that Li≤u<v≤Ri , Shi Bao connects building u and v, using the cables with the same length Wi.
Mount Taixuan’s network has shut down since there are too many redundant connections. Fu Hua has to disconnect some cables to repair the network and only keep the necessary connections. She can choose a set of cables and remove them from the network, and she must make sure that after the removal, there still exists a set of remaining cables that connect the building i to building 1 directly or indirectly.
It is possible that some buildings do not have a path connected to the building 1 before Fu Hua remove cables because Shi Bao neglected them. In this case, Fu Hua will reevaluate Shi Bao’s ability of network maintenance and give her a lesson of internet common sense.
Fu Hua wants to maximize the total length of removed cables to save the budget.
Please tell Fu Hua the greatest possible total length, or does she need to prepare for a lesson.

输入描述

There are T test cases in this problem.
The first line has one integer T.
For each test case:
The first line has two integers N and M, which denotes the number of buildings at Mount Taixuan and the number of days that Shibao used for cable settlement.
In the next M lines, the i-th line has 3 integers Li, Ri, Wi, describing how Shi Bao connect the cables on the i-th day.
It guarantees that 1≤T≤10, and in a single test case, 1≤N,M,Wi≤1e5, 1≤Li<Ri≤N.

输出描述

For each test case, you should first print "Case #t: " (without quotes), where t is the index of this test case. Then, if some buildings are still not connected to building 1 after Shi Bao’s M-day work, you should print “Gotta prepare a lesson” (without quotes), otherwise, you should print the maximum total length of cables that Fu Hua can remove.

样例

输入

4
3 1
1 3 4
3 1
1 2 3
5 2
1 4 1
4 5 10
3 2
1 3 4
1 2 3

输出

Case #1: 4
Case #2: Gotta prepare a lesson
Case #3: 3
Case #4: 8

题意

题目又臭又长实在是无力吐槽,日常就是阅读理解。给n个点,m组l,r,w,每组l,r,w的意思是:在第l个点到第r个点的任意两点间有一条边,其权为w。求使每个点都直接或间接与点1相连的情况下,能断开的边的权之和最大为多少。

思路

初看感觉像最小生成树,然后一看数据范围发现不可能,连接的边的条数最大可能达到1e15的级别,这种情况下想直接做根本不可能。然而,给定的都是连续的一个个区间,猜测可以只关注两相邻点间的边,这个让我自己想反正我是没想到。线段树维护点i到点i+1的边权及边权和,把l,r,w按w的大小从大到小排序后进行区间修改,这样最后留下的就是最小的权,最后区间查询得到留下的边的权之和,再用所有边权之和减去它即可。

代码

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=1e5+5;
const ll INF=1e11;

struct edge {
	int l,r,w;
	bool operator < (edge e) {
		return w>e.w;
	}
}e[N];

struct node {
	int l,r;
	int la;
	ll s;
}tr[N<<2];

int t;
int n,m;

void pushup(int u) {
	tr[u].s=tr[u<<1].s+tr[u<<1|1].s;
}

void cal(node &t,int la) {
	t.la=la;
	t.s=(ll)la*(t.r-t.l+1);
}

void pushdown(int u) {
    if(tr[u].la==0) return;
	cal(tr[u<<1],tr[u].la);
	cal(tr[u<<1|1],tr[u].la);
	tr[u].la=0;
}

void build(int u,int l,int r)  {
	tr[u]={l,r,0,INF};
	if(l==r) return;
	int mid=l+r>>1;
	build(u<<1,l,mid);
	build(u<<1|1,mid+1,r);
	pushup(u);
}

void modify(int u,int l,int r,int w) {
	if(tr[u].l>=l&&tr[u].r<=r) {
		cal(tr[u],w);
		return;
	}
	pushdown(u);
	int mid=tr[u].l+tr[u].r>>1;
	if(l<=mid) modify(u<<1,l,r,w);
	if(r>mid) modify(u<<1|1,l,r,w);
	pushup(u);
}

int main() {
	cin>>t;
	for(int tt=1;tt<=t;tt++) {
		cin>>n>>m;
		build(1,1,n-1);
		ll res=0;
		for(int i=1;i<=m;i++) {
			int l,r,w;
			scanf("%d%d%d",&l,&r,&w);
			res+=(ll)(r-l+1)*(r-l)/2*w;
			e[i]={l,r,w};
		}
		sort(e+1,e+m+1);
		for(int i=1;i<=m;i++) 
		    modify(1,e[i].l,e[i].r-1,e[i].w);
		printf("Case #%d: ",tt);
		if(tr[1].s>=INF) printf("Gotta prepare a lesson\n");
		else printf("%lld\n",res-tr[1].s);
	}
	return 0;
} 

原题链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值