首页 > 其他分享 >ICPC2022 Xi'an R A Bridge

ICPC2022 Xi'an R A Bridge

时间:2023-12-22 09:55:24浏览次数:23  
标签:ICPC2022 typedef Xi Bridge int long sz maxn define

洛谷传送门

QOJ 传送门

感觉很妙啊,应该不止蓝吧?

首先一个转化是每次建桥操作就相当于交换两条链的后半部分,可以看看扶苏那篇题解的图。

我们将每个点表示为形如 \((x, y)\) 的二元组表示它初始在第 \(x\) 行第 \(y\) 列,按 \(y\) 为键值排序,那么一次询问就是查询一条链的最大值。

这些操作可以用 FHQ Treap 完成,就是每次修改 \((a, b)\) 就把两棵 Treap 分别按 \(\le b\) split 成四棵,交换后再 merge。查询直接暴力跳右儿子就行。

但是显然不能存 \(nm\) 个点。考虑我维护连续段而不是维护一个个单点,每个连续段形如 \((x, l, r)\) 表示初始在第 \(x\) 行第 \(l\) 列到第 \(r\) 列的点,意义是它们在同一条链上并且连边 \(l \to l + 1 \to \cdots \to r\)。

那么每次修改操作会多产生 \(4\) 个段,所以段的个数就是 \(O(n + q)\) 的,可以接受。

实现时比较繁琐,每一行要额外开个 set 维护原来在这一行的段方便对于每次修改找到 \(b\) 所在的段,还要开个 map 维护每个三元组 \((x, l, r)\) 对应到 Treap 上的哪个点。为了找对应结点的根还要维护每个点的父亲。

总时间复杂度是 \(O((n + q) \log n)\)。

code
// Problem: P9358 [ICPC2022 Xi'an R] Bridge
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9358
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 500100;

int n, m, q;
set<pii> S[maxn];

int ls[maxn], rs[maxn], p[maxn], sz[maxn], rt[maxn], fa[maxn], nt;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
struct node {
	int l, r, x;
	node(int a = 0, int b = 0, int c = 0) : l(a), r(b), x(c) {}
} val[maxn];

map<node, int> mp;

inline bool operator < (const node &a, const node &b) {
	return a.l < b.l || (a.l == b.l && a.x < b.x);
}

inline int newnode(node x) {
	int u = ++nt;
	val[u] = x;
	mp[x] = u;
	p[u] = rnd();
	sz[u] = 1;
	return u;
}

inline void pushup(int x) {
	sz[x] = sz[ls[x]] + sz[rs[x]] + 1;
	if (ls[x]) {
		fa[ls[x]] = x;
	}
	if (rs[x]) {
		fa[rs[x]] = x;
	}
}

void split(int u, int k, int &x, int &y) {
	if (!u) {
		x = y = 0;
		return;
	}
	if (val[u].r <= k) {
		x = u;
		split(rs[u], k, rs[u], y);
	} else {
		y = u;
		split(ls[u], k, x, ls[u]);
	}
	pushup(u);
}

int merge(int x, int y) {
	if (!x || !y) {
		return x + y;
	}
	if (p[x] < p[y]) {
		rs[x] = merge(rs[x], y);
		pushup(x);
		return x;
	} else {
		ls[y] = merge(x, ls[y]);
		pushup(y);
		return y;
	}
}

inline int getrt(int x) {
	while (fa[x]) {
		x = fa[x];
	}
	return x;
}

inline int query(int x) {
	while (rs[x]) {
		x = rs[x];
	}
	return val[x].x;
}

inline int erase(node u) {
	int rt = getrt(mp[u]), x, y, z;
	split(rt, u.r, x, z);
	split(x, u.l - 1, x, y);
	return rt = merge(x, z);
}

inline void insert(int &rt, node u) {
	int x, y;
	split(rt, u.r, x, y);
	rt = merge(merge(x, newnode(u)), y);
}

void solve() {
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 1; i <= n; ++i) {
		S[i].emplace(0, m);
		rt[i] = newnode(node(0, m, i));
	}
	while (q--) {
		int op, x, y;
		scanf("%d%d", &op, &x);
		if (op == 1) {
			scanf("%d", &y);
			auto i = --S[x].lower_bound(mkp(y, 0)), j = --S[x + 1].lower_bound(mkp(y, 0));
			pii p1 = *i, p2 = *j;
			int r1 = erase(node(p1.fst, p1.scd, x));
			int r2 = erase(node(p2.fst, p2.scd, x + 1));
			S[x].erase(i);
			S[x + 1].erase(j);
			S[x].emplace(p1.fst, y - 1);
			S[x].emplace(y, p1.scd);
			S[x + 1].emplace(p2.fst, y - 1);
			S[x + 1].emplace(y, p2.scd);
			insert(r1, node(p1.fst, y - 1, x));
			insert(r1, node(y, p1.scd, x));
			insert(r2, node(p2.fst, y - 1, x + 1));
			insert(r2, node(y, p2.scd, x + 1));
			int xa, ya, xb, yb;
			split(r1, y - 1, xa, ya);
			split(r2, y - 1, xb, yb);
			r1 = merge(xa, yb);
			r2 = merge(xb, ya);
			fa[r1] = fa[r2] = 0;
		} else {
			pii p = *S[x].begin();
			printf("%d\n", query(getrt(mp[node(p.fst, p.scd, x)])));
		}
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:ICPC2022,typedef,Xi,Bridge,int,long,sz,maxn,define
From: https://www.cnblogs.com/zltzlt-blog/p/17920615.html

相关文章

  • XILINX HLS 入坑记录 之 写RAM 综合出 读取+写入Ram
    最近使用XilinxHLS来开发算法的IPcore,使用的Vitis2021,发现光是EDA工具就存在很多的bug,比如:1.经常C综合停留在Usingflow_target'vivado'不给任何报错提示,永远卡死;2.点击coSimulationvivado启动后读取脚本卡死,不能正常仿真;3.C综合给出的资源使用和IPCore实现后......
  • 微服务启动-端口already exist
    微服务项目启动eureka成功,port:8761,再次启动其他服务都报错:8761端口已经alreadyexist,如何解决?明明各自服务在其各自的application.yaml文件都配置了端口号port,不应该有冲突诶。在确定自己没有编写错误的前提下,不断重启就行了!!!下面看情况去测试,主要是我没搞清楚问题来源。搜了好......
  • 从零开始用 Axios 请求后端接口
    对于前端同学来说,请求后端接口是一个非常通用的东西。在十几年前的时候,我们还用Ajax去请求后端接口。但在2023年的今天,很多框架都很成熟了,我们有了更加快捷的方式——Axios框架。请求框架哪家强?对于使用Vue技术栈的同学来说,其实接口请求框架就三种:vue-resource、Axios......
  • [Troubleshooting] kubectl cp exit code 255 - exec: \"tar\": executable file no
    0.背景kubectlcpcontainer文件到本地host报错:$kubectlcptest/po-test-pod-0:/tmp./-cctr-test-containertime="2023-12-20T02:17:29Z"level=errormsg="execfailed:unabletostartcontainerprocess:exec:\"tar\":executablefile......
  • vue项目多axios实例动态创建
    //通用请求拦截器importaxiosfrom"axios";importQsfrom"qs";importstorefrom"@/store";importrouterfrom"@/router";import{Loading,Message}from"element-ui";//引用element-ui的加载和消息提示组件letloading......
  • subprocess.CalledProcessError: Command ‘[‘ninja‘, ‘-v‘]‘ returned non-zero
    一、原因pytorch版本大于1.5二、解决1、降低pytorch版本将pytorch版本降到1.5以下2、禁用ninjiapytorch默认使用ninjia作为backend,将其禁用。替换为以下代码setup(...,cmdclass={#'build_ext':BuildExtension,'build_ext':BuildExtensi......
  • mapstruct报错 No property named "XXXX" exists in source parameter(s). Type "XXXX
    1、问题现象java:Nopropertynamed"XXXX"existsinsourceparameter(s).Type"XXXX"hasnoproperties.2、相关环境依赖版本jdk:17maven:3.8.8springboot:3.1.4lombok:1.18.30mapstruct:1.5.53、解决办法在pom.xml中加入如下配置<annotationProcessor......
  • Nacos启动:[NACOS HTTP-POST] The maximum number of tolerable server reconnection e
    一、表象二、分析源码:publicHttpRestResult<String>httpPost(Stringpath,Map<String,String>headers,Map<String,String>paramValues,Stringencode,longreadTimeoutMs)throwsException{finallongendTime=System.currentTi......
  • SeaFormer: Squeeze-enhanced Axial Transformer for Mobile Semantic Segmentation
    SeaFormer:Squeeze-enhancedAxialTransformerforMobileSemanticSegmentation*Authors:[[QiangWan]],[[ZilongHuang]],[[JiachenLu]],[[GangYu]],[[LiZhang]]初读印象comment::(SeaFormer)提出了一种适用于移动设备的轻量级网络,设计了一个通用的注意力块,特......
  • Expectation-Maximization Attention Networks for Semantic Segmentation 使用了EM算
    Expectation-MaximizationAttentionNetworksforSemanticSegmentation*Authors:[[XiaLi]],[[ZhishengZhong]],[[JianlongWu]],[[YiboYang]],[[ZhouchenLin]],[[HongLiu]]DOI:10.1109/ICCV.2019.00926Locallibrary初读印象comment::(EMANet)用期望......