首页 > 其他分享 >CF1715E long way home

CF1715E long way home

时间:2022-11-02 12:44:46浏览次数:50  
标签:ch template int void pos CF1715E way inline home

本题并不难。
观察一下数据范围 \(k\) 非常小,那么不难发现我们可以把跳这个操作做 \(k\) 遍即可。

跳操作的式子一看就很斜率优化 \((u-v)^2=u^2+v^2-2uv\) 直接李超树维护即可。

#include<bits/stdc++.h>
#define RG register
#define LL long long
#define U(x, y, z) for(RG int x = y; x <= z; ++x)
#define D(x, y, z) for(RG int x = y; x >= z; --x)
#define update(x, y) (x = x + y >= mod ? x + y - mod : x + y)
using namespace std;
void read(){}
template<typename _Tp, typename... _Tps>
void read(_Tp &x, _Tps &...Ar) {
	x = 0; char ch = getchar(); bool flg = 0;
	for (; !isdigit(ch); ch = getchar()) flg |= (ch == '-');
	for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
	if (flg) x = -x;
	read(Ar...);	
}
inline char Getchar(){ char ch; for (ch = getchar(); !isalpha(ch); ch = getchar()); return ch;}
template <typename T> inline void write(T n){ char ch[60]; bool f = 1; int cnt = 0; if (n < 0) f = 0, n = -n; do{ch[++cnt] = char(n % 10 + 48); n /= 10; }while(n); if (f == 0) putchar('-'); for (; cnt; cnt--) putchar(ch[cnt]);}
template <typename T> inline void writeln(T n){write(n); putchar('\n');}
template <typename T> inline void writesp(T n){write(n); putchar(' ');}
template <typename T> inline void chkmin(T &x, T y){x = x < y ? x : y;}
template <typename T> inline void chkmax(T &x, T y){x = x > y ? x : y;}
template <typename T> inline T Min(T x, T y){return x < y ? x : y;}
template <typename T> inline T Max(T x, T y){return x > y ? x : y;}
inline void readstr(string &s) { s = ""; static char c = getchar(); while (isspace(c)) c = getchar(); while (!isspace(c)) s = s + c, c = getchar();}
inline void FO(string s){freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout);}

const int N = 1e5 + 10;
struct edge {
	int v, w, nxt;
} e[N << 1];

int cnt = 1, head[N], n, m, k;
inline void aedge(int u, int v, int w) {
	e[++cnt] = (edge) {v, w, head[u]};
	head[u] = cnt;
}

LL dis[N];
const LL INF = 1e18;
#define pii pair<LL, int>
#define fi first
#define se second
inline void dijkstra() {
	priority_queue< pii, vector< pii >, greater< pii > > q;
	U(i, 1, n) q.emplace(dis[i], i);
	while (!q.empty()) {
		pii x = q.top();
		q.pop();
		int u = x.se;
		if (dis[u] != x.fi) continue ;
		for (int i = head[u]; i; i = e[i].nxt) {
			int v = e[i].v, w = e[i].w;
			if (dis[u] + w < dis[v]) 
				q.emplace(dis[v] = dis[u] + w, v);
		}
	}
}

struct Seg {
	LL k, b;
	Seg(){}
	Seg(LL k, LL b) : k(k), b(b) {}
} s[N];

inline LL w(int id, int pos) {
	return s[id].k * pos + s[id].b;
}

struct Segment_Tree {
	int t[N << 2];
	#define lson (pos << 1)
	#define rson (pos << 1 | 1)

	inline void clear() {
		memset(t, 0, sizeof t);
	}

	inline void modify(int pos, int l, int r, int cur) {
		int mid = l + r >> 1;
		if (w(t[pos], mid) > w(cur, mid)) 
			swap(t[pos], cur);
		if (l == r) return ;
		if (w(t[pos], l) <= w(cur, l) && w(t[pos], r) <= w(cur, r)) return ;
		if (s[cur].k > s[t[pos]].k) modify(lson, l, mid, cur);
		else modify(rson, mid + 1, r, cur);
	}

	inline LL query(int pos, int l, int r, int p) {
		if (l == r) 
			return w(t[pos], p);
		int mid = l + r >> 1;
		LL res = w(t[pos], p);
		if (p <= mid) chkmin(res, query(lson, l, mid, p));
		else chkmin(res, query(rson, mid + 1, r, p));
		return res;
	}
} sgt;

int main(){
	//FO("");
	s[0] = Seg(0, INF);
	read(n, m, k);
	U(i, 1, m) {
		int u, v, w;
		read(u, v, w);
		aedge(u, v, w);
		aedge(v, u, w);
	}

	U(i, 2, n) dis[i] = INF;
	dijkstra();
	U(i, 1, k) {
		sgt.clear();
		U(j, 1, n) {
			s[j] = Seg(-2 * j, (LL) j * j + dis[j]);
			sgt.modify(1, 1, n, j);
		}
		U(j, 1, n) chkmin(dis[j], sgt.query(1, 1, n, j) + (LL) j * j);
		dijkstra();
	}
	U(i, 1, n) writesp(dis[i]);
	return 0;
}

标签:ch,template,int,void,pos,CF1715E,way,inline,home
From: https://www.cnblogs.com/SouthernWay/p/16850644.html

相关文章

  • 关于 NGINX Kubernetes Gateway,你需要知道的 5 件事
    原文作者:IlyaKrutovofF5原文链接:​​​关于NGINXKubernetesGateway,你需要知道的5件事​​转载来源:NGINX官方网站在过去的几年里,F5NGINX帮助您成功走完了Kuberne......
  • Istio egress gateway
    EgressGateway逻辑示意图EgressGateway配置要点各SidecarEnvoy上访问特定外部主机的流量,要路由至EgressGatewayEgressGateway要将相应的流量路由至相应的外部......
  • Gateway
    一、GateWay1.作用对用户请求做身份认证、权限校验将用户请求路由到微服务,并实现负载均衡对用户请求做限流2.使用1.创建模块,引入GateWay网关依赖和nacos依赖<!--......
  • Algorithm: Lecture 4. Divide-and-Conquer Homework
    author:Miyasakadate:2022-10-31title:"Algorithm:Lecture4.Divide-and-ConquerHomework"*Inthiswork,alltheindexofarraystartsby1.Question:Bin......
  • asyncapi event-gateway
    支持的功能消息验证消息操作消息聚合消息过滤验证节流路由监控(包括追踪)参考架构说明目前来说官方的似乎还只支持基于kafka的处理,当前基于事件消息模式玩法......
  • 玩客云 docker安装homeassistant
    dockerpullportainer/portainer:latest dockerrun-d\--name="hass"\--privileged\--restart=unless-stopped\-eTZ='Asia/Shanghai'\-v/home/hass/......
  • Istio(四):创建部署Gateway并使用网关暴露服务
    目录一.模块概览二.系统环境三.Gateway网关3.1使用Gateway四.实战:使用Gateway发布服务4.1创建部署并使用网关暴露4.2清理一.模块概览在Kubernetes集群中,服务的发布方......
  • linux分配 /home磁盘给根目录
    系统为centos7系统安装之后,根目录空间只有50G,/home有800多G,而/home使用较少,所以将/home空间分配给根目录。1,查看磁盘使用情况:df-h 2,减少/home (/dev/mapper/centos-......
  • 如何不改动 GatewayWorker 依赖包下自定义协议
    前言:     GatewayWorker是Workerman的一个框架,对应用层开发者更友好。GatewayWorker多了一个网关,也就是Gateway,负责与客户端连接,消息转发等。而自定义的协......
  • Homework 3 : C++ class inheritance Answer
    Homework3:C++classinheritanceAnswerHomework3:C++classinheritanceInstructor:ZhiyaoLiangCIS111&EIE1112021Spring源码传送门传送门:https://pan.ba......