首页 > 其他分享 >P8773 题解

P8773 题解

时间:2023-01-07 15:47:15浏览次数:55  
标签:loc le int 题解 pos P8773 include mx

前言

题目传送门!

更好的阅读体验?

一道有趣的题目。

思路

对于一个数 \(a_i\),如果有 \(a_i \oplus t = x\),显然 \(t = a_i \oplus x\)。

设 \(loc_i\) 表示上一个 \(t\) 出现的位置。这个是很容易维护的。

那么对于一组询问 \([l, r]\),如果存在,必然是有一个 \(l \le i \le r\) 满足 \(loc_i \ge l\)。

换种说法,\([l, r]\) 存在,当且仅当 \(\max\limits_{l \le i \le r} loc_i \ge l\)。

这玩意显然可以用线段树维护。然后就做完了。

但是也是可以用 ST 的,并且更优,但是我肯定不会再打一份代码啊

代码

这个是模拟赛时候的代码。因为赛时降智就写了线段树,每组询问一只 \(\log\)。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, q, x, loc[1919810];
namespace SegmentTree {
	int mx[400005];
	#define ls pos << 1
	#define rs pos << 1 | 1
	void build(int l, int r, int pos)
	{
		if (l == r)
		{
			int a;
			scanf("%d", &a);
			loc[a] = l, mx[pos] = loc[a ^ x];			
			return;
		}
		int mid = (l + r) >> 1;
		build(l, mid, ls), build(mid + 1, r, rs);
		mx[pos] = max(mx[ls], mx[rs]);
	}
	int query(int l, int r, int pos, int L, int R)
	{
		if (L <= l && r <= R) return mx[pos];
		int mid = (l + r) >> 1, ans = 0;
		if (L <= mid) ans = max(ans, query(l, mid, ls, L, R));
		if (mid < R) ans = max(ans, query(mid + 1, r, rs, L, R));
		return ans;
	}
}; using namespace SegmentTree;

int main()
{
	scanf("%d%d%d", &n, &q, &x);
	build(1, n, 1);
	while (q--)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		if (query(1, n, 1, l, r) >= l) puts("yes"); else puts("no");
	}
	return 0;
}

希望能帮助到大家!

标签:loc,le,int,题解,pos,P8773,include,mx
From: https://www.cnblogs.com/liangbowen/p/17032762.html

相关文章

  • [ABC255F] Pre-order and In-order 题解
    [ABC255F]Pre-orderandIn-orderSolution目录[ABC255F]Pre-orderandIn-orderSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面给......
  • AtCoder Beginner Contest 255 题解
    AtCoderBeginnerContest255Solution目录AtCoderBeginnerContest255Solution更好的阅读体验戳此进入题面链接题面Luogu链接abcd跳了[ABC255E]LuckyNumbers题......
  • [ABC255G] Constrained Nim 题解
    [ABC255G]ConstrainedNimSolution目录[ABC255G]ConstrainedNimSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面一般Nim游戏基......
  • [ABC256F] Cumulative Cumulative Cumulative Sum 题解
    [ABC256F]CumulativeCumulativeCumulativeSumSolution目录[ABC256F]CumulativeCumulativeCumulativeSumSolution更好的阅读体验戳此进入题面SolutionCodeUPD更......
  • [ABC256E] Takahashi's Anguish 题解
    [ABC256E]Takahashi'sAnguishSolution目录[ABC256E]Takahashi'sAnguishSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面存在$n......
  • [ABC252E] Road Reduction 题解
    [ABC252E]RoadReductionSolution目录[ABC252E]RoadReductionSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面给定$n$个点$......
  • [ABC252D] Distinct Trio 题解
    [ABC252D]DistinctTrioSolution目录[ABC252D]DistinctTrioSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面给定序列$a_n$,求满......
  • P8865 [NOIP2022] 种花 题解
    P8865[NOIP2022]种花Solution目录P8865[NOIP2022]种花Solution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面大概就是在有障碍的网格图......
  • 【题解】P4218 [CTSC2010]珠宝商
    这种题出出来有什么必要吗,不就是难写的暴力弱智题。题意给定一棵树和一个文本串\(T\),每个结点上有一个字符,问树上任意路径构成的字符串在\(T\)中的出现次数之和。\(n......
  • SDK更新不了问题解决
    问题阐述相信大家在更新SDK的时候都会遇到更新不了的问题,而且打不开Google搜索,这是因为天朝墙了Google,所以要么只能通过科学上网或者改HOSTS才能访问,更新SDK!本节来介绍两种......