P1531 I Hate It 题解 线段树 模拟 枚举

I Hate It

传送门

题目背景

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。这让很多学生很反感。

题目描述

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

输入格式

第一行,有两个正整数 n n n m m m 0 < n ≤ 2 × 1 0 5 , 0 < m < 5000 0<n \le 2\times 10^5,0<m<5000 0<n2×105,0<m<5000),分别代表学生的数目和操作的数目。学生 ID 编号分别从 1 1 1 编到 n n n

第二行包含 n n n 个整数,代表这 n n n 个学生的初始成绩,其中第 i i i 个数代表 ID 为 i i i 的学生的成绩。

接下来有 m m m 行。每一行有一个字符 c c c(只取 QU),和两个正整数 a a a b b b

  • c c cQ 的时候,表示这是一条询问操作,它询问 ID 从 a a a b b b(包括 a , b a,b a,b) 的学生当中,成绩最高的是多少;
  • c c cU 的时候,表示这是一条更新操作,如果当前 a a a 学生的成绩低于 b b b,则把 ID 为 a a a 的学生的成绩更改为 b b b,否则不改动。

输出格式

对于每一次询问操作输出一行一个整数,表示最高成绩。

样例 #1

样例输入 #1

5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

样例输出 #1

5
6
5
9

解题思路

前言

首先,这题是线段树模版题;然后,我调了10min;最后,它只有一组输入。为什么要说这几句呢,因为我们的()()(<-开启词汇联想,放飞你的想象)教练把这题改成了多组输入,导致我AC后在提交洛谷无法AC,(此处加入大量氧化钙)。

正文

题目的思路非常Easy,写个for循环,先判断第一个字符是Q还是U。若是Q则询问询问ID从 a a a b b b(包括 a a a, b b b) 的学生当中,成绩最高的是多少;若是U则更新 a a a 学生的成绩为 max ⁡ ( n o w , b ) \max(now,b) max(now,b)。(就是一道()()(<-继续词汇联想)线段树模版题。)

AC Code

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
#define int long long
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int Maxn = 2e5 + 5;
int n, m, a[Maxn];
int s[800005];
inline void Build(int l, int r, int rt);
inline void Update(int l, int r, int rt, int L, int v);
inline int Query(int l, int r, int rt, int L, int R);
inline void work() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> a[i];
	Build(1, n, 1);
	char c;
	int x, y;
	for (int i = 1; i <= m; i++) {
		cin >> c >> x >> y;
		if (c == 'Q')cout << Query(1, n, 1, x, y) << endl;
		else Update(1, n, 1, x, y);
	}
}
signed main() {
	fast_io
	work();
	return 0;
}
inline void Build(int l, int r, int rt) {
	if (l == r) {
		s[rt] = a[l];
		return ;
	}
	int mid = l + r >> 1;
	Build(l, mid, rt << 1);
	Build(mid + 1, r, rt << 1 | 1);
	s[rt] = max(s[rt << 1], s[rt << 1 | 1]);
}
inline void Update(int l, int r, int rt, int L, int v) {
	if (l == r) {
		if (s[rt] < v) s[rt] = v;
		return ;
	}
	int mid = l + r >> 1;
	if (mid >= L) Update(l, mid, rt << 1, L, v);
	if (mid < L) Update(mid + 1, r, rt << 1 | 1, L, v);
	s[rt] = max(s[rt << 1], s[rt << 1 | 1]);
}
inline int Query(int l, int r, int rt, int L, int R) {
	if (L <= l && r <= R) return s[rt];
	int mid = (l + r) >> 1, ans = -1e9;
	if (mid >= L) ans = max(ans, Query(l, mid, rt << 1, L, R));
	if (mid < R) ans = max(ans, Query(mid + 1, r, rt << 1 | 1, L, R));
	return ans;
}

()()() (<-词汇联想,启动!)洛谷这题: 普及 / 提高 − \textcolor{yellow}{ 普及/提高−} 普及/提高,(十分耀眼)非常适合中国宝宝体质。

  • 33
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值