首页 > 其他分享 >AtCoder Beginner Contest 212 F Greedy Takahashi

AtCoder Beginner Contest 212 F Greedy Takahashi

时间:2023-05-18 15:33:32浏览次数:52  
标签:AtCoder typedef 212 Beginner ll mid pos maxn id

洛谷传送门

AtCoder 传送门

考虑每条边,因为是静态的,所以可以预处理出 \(f_{i,j}, g_{i,j}\) 表示从第 \(i\) 条边,往后跳 \(2^j\) 条边,跳到边的编号和目前的时间(如果不存在就当作跳到第 \(0\) 条边)。直接倍增处理即可。

询问就是找到从 \(u\) 开始的出边,能跳尽量跳。

注意一些细节。

code
// Problem: F - Greedy Takahashi
// Contest: AtCoder - AtCoder Beginner Contest 212
// URL: https://atcoder.jp/contests/abc212/tasks/abc212_f
// Memory Limit: 1024 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

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

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

const int maxn = 100100;
const int logn = 20;

ll n, m, q, f[maxn][logn], g[maxn][logn];

struct E {
	ll v, s, t, id;
	E(ll a = 0, ll b = 0, ll c = 0, ll d = 0) : v(a), s(b), t(c), id(d) {}
};

inline bool cmp(E a, E b) {
	return a.s < b.s;
}

struct node {
	ll u, v, s, t;
	node(ll a = 0, ll b = 0, ll c = 0, ll d = 0) : u(a), v(b), s(c), t(d) {}
} a[maxn];

vector<E> G[maxn];
vector<pii> qq[maxn];

void solve() {
	scanf("%lld%lld%lld", &n, &m, &q);
	for (int i = 1, u, v, s, t; i <= m; ++i) {
		scanf("%d%d%d%d", &u, &v, &s, &t);
		G[u].pb(v, ++s, t, i);
		a[i] = node(u, v, s, t);
	}
	for (int i = 1; i <= n; ++i) {
		sort(G[i].begin(), G[i].end(), cmp);
	}
	f[0][0] = 0;
	g[0][0] = 1e18;
	for (int u = 1; u <= n; ++u) {
		for (E e : G[u]) {
			ll v = e.v, t = e.t;
			int l = 0, r = (int)G[v].size() - 1, pos = -1;
			while (l <= r) {
				int mid = (l + r) >> 1;
				if (G[v][mid].s > t) {
					pos = mid;
					r = mid - 1;
				} else {
					l = mid + 1;
				}
			}
			if (pos == -1) {
				f[e.id][0] = 0;
				g[e.id][0] = 1e18;
			} else {
				f[e.id][0] = G[v][pos].id;
				g[e.id][0] = G[v][pos].t;
			}
		}
	}
	for (int j = 1; j <= 18; ++j) {
		for (int i = 0; i <= m; ++i) {
			f[i][j] = f[f[i][j - 1]][j - 1];
			g[i][j] = g[f[i][j - 1]][j - 1];
		}
	}
	while (q--) {
		ll u, x, y;
		scanf("%lld%lld%lld", &x, &u, &y);
		int l = 0, r = (int)G[u].size() - 1, pos = -1;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (G[u][mid].s > x) {
				pos = mid;
				r = mid - 1;
			} else {
				l = mid + 1;
			}
		}
		if (pos == -1) {
			printf("%lld\n", u);
			continue;
		}
		if (y < G[u][pos].s) {
			printf("%lld\n", u);
			continue;
		}
		if (y <= G[u][pos].t) {
			printf("%lld %lld\n", u, G[u][pos].v);
			continue;
		}
		int d = G[u][pos].id;
		for (int i = 18; ~i; --i) {
			if (g[d][i] < y) {
				d = f[d][i];
			}
		}
		if (!f[d][0]) {
			printf("%lld\n", a[d].v);
			continue;
		}
		int k = f[d][0];
		if (y < a[k].s) {
			printf("%lld\n", a[k].u);
		} else if (a[k].s <= y && y <= a[k].t) {
			printf("%lld %lld\n", a[k].u, a[k].v);
		}
	}
}

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

标签:AtCoder,typedef,212,Beginner,ll,mid,pos,maxn,id
From: https://www.cnblogs.com/zltzlt-blog/p/17412096.html

相关文章

  • AtCoder Beginner Contest 253(E,F)
    AtCoderBeginnerContest253(E,F)E(dp,前缀和)E大意就是求满足以下要求的的序列的个数\(1\),满足\(a_i\)都在\([1,m]\)的范围里面\(2\),满足$\verta_i-a_{i+1}\vert$大于\(k\)之前做过一个类似的题目,是绝对值小于\(k\),不过大同小异这里我使用了前缀和来优化但是这里......
  • AtCoder Beginner Contest 200 F Minflip Summation
    洛谷传送门AtCoder传送门显然的策略:选择全部\(0\)段变成\(1\),或选择全部\(1\)段变成\(0\)。归纳可得一般性的结论:设字符串中\(s_i\nes_{i+1}\)的位置数为\(k\),答案为\(\left\lceil\frac{k}{2}\right\rceil\)。因为在模意义下不能上取整,考虑记\(k\)的奇偶性(这样......
  • AtCoder Regular Contest 160 D Mahjong
    洛谷传送门AtCoder传送门搞笑题,我不会做,我更搞笑。考虑逆序操作,即初始有一个全\(0\)序列,每次单点加\(k\)或者长为\(k\)区间加\(1\)。考虑把一个操作集合唯一对应到一个最终序列,不难发现只要限制每个区间加\(1\)的次数\(<k\)即可。因为如果正序操作,加上了限制,每个......
  • AtCoder Beginner Contest 301 F Anti-DDoS
    洛谷传送门AtCoder传送门考虑分类计数,讨论“没有DD”、“有DD无o”、“有DDo无S”三种情况。没有DD,枚举有几种大写字母出现过;剩下两种情况,考虑设\(f_{i,0/1}\)分别表示两种情况的方案数。\(f_{i,0}\)可以从\(f_{i-1,0}\)填大写字母转移,也可以枚举第一个出现两......
  • Atcoder Regular Contest 101
    F-RobotsandExits\(n\)个人,\(m\)个出口在一条数轴上,坐标是正整数。你每次可以让所有人向左或向右一步,人在某个出口上后就离开。求多少种操作的方案使得人全部走光。两个方案相同当且仅当存在至少一个人在两次操作序列进行完成后从不同的出口消失。对\(10^9+7\)取模。\(1......
  • AtCoder Beginner Contest 223 G Vertex Deletion
    洛谷传送门AtCoder传送门设\(f_{u,0/1}\)为\(u\)的子树,\(u\)是否在匹配内的最大匹配数。注意到对于一个匹配,在它深度较浅的点上才会被计入答案。转移大概是\(f_{u,0}\)取\(\sum\limits_{v\inson_u}\max(f_{v,0},f_{v,1})\),\(f_{u,1}\)要从儿子中选一个\(f_{v,0......
  • AtCoder Beginner Contest 301
    title:AtCoderBeginnerContest301categories:算法题解description:咕咕咕tags:-Atcoder-贪心-BFS-DPcover:/img/chino/vec/chino17.jpgkatex:truedate:2023-05-1322:47:31A-OverallWinner(abc301a)题目大意给定一个字符串表示高桥和青木......
  • AtCoder Regular Contest 129 C Multiple of 7
    洛谷传送门AtCoder传送门首先\(\text{7777...777}\)(\(x\)个\(7\))对能被\(7\)整除子串数量的贡献是\(\frac{x(x+1)}{2}\)。把\(n\)分解成若干\(x_i\)使得\(\sum\limits_{i=1}^m\frac{x_i(x_i+1)}{2}=n\),表示每段\(x_i\)个\(7\)。怎么把它们组合在一起呢?一个......
  • AtCoder Beginner Contest 163 F path pass i
    洛谷传送门AtCoder传送门感觉我的做法比较奇葩(容斥,总路径数减去只走点权为\(k\)的路径。设点权为\(k\)的点数为\(c_k\),点权不为\(k\)的点构成的每个连通块大小为\(s_i\),那么\(ans_k=\frac{n(n-1)}{2}-\sum\frac{s_i(s_i-1)}{2}+c_k\)。考虑快速计算\(\su......
  • AtCoder Beginner Contest 207 F Tree Patrolling
    洛谷传送门AtCoder传送门简单树形dp。设\(f_{u,i,p=0/1,q=0/1}\)为\(u\)的子树中被覆盖点数为\(i\),\(u\)有没有被覆盖,\(u\)有没有被选。转移树形背包合并即可,需要分类讨论。要注意如果\(u\)没被覆盖,\(v\)选了,或者\(u\)选了,\(v\)没被覆盖,被覆盖点数要\(+1\)。......