基础篇:
NYOJ 1185 最大最小值
传送门
描述
给出N个整数,执行M次询问。
对于每次询问,首先输入三个整数C、L、R:
如果C等于1,输出第L个数到第R个数之间的最小值;
如果C等于2,输出第L个数到第R个数之间的最大值;
如果C等于3,输出第L个数到第R个数之间的最小值与最大值的和。
(包括第L个数和第R个数)。
输入
首先输入一个整数T(T≤100),表示有T组数据。
对于每组数据,先输入一个整数N(1≤N≤10000),表示有N个整数;
接下来一行有N个整数a(1≤a≤10000);
然后输入一个整数M,表示有M次询问;
接下来有M行(1≤M≤10000),每行有3个整数C、L、R(1≤C≤3,1≤L≤R≤N)。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define scini(n) scanf("%d", &n)
#define scinl(n) scanf("%lld", &n)
#define scinii(n, m) scanf("%d%d", &n, &m)
#define scinll(n, m) scanf("%lld%lld", &n, &m)
#define scout(n) printf("%d\n", n);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 10010;
using namespace std;
inline int read(){
int x(0),f(1);
char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
struct node{
int max_num;
int min_num;
}Sqtree[maxn<<2];
inline void PushUp(int rt)
{
Sqtree[rt].max_num = max(Sqtree[rt<<1].max_num, Sqtree[rt<<1|1].max_num);
Sqtree[rt].min_num = min(Sqtree[rt<<1].min_num, Sqtree[rt<<1|1].min_num);
}
inline void buildtree(int l, int r, int rt)
{
if(l == r){
Sqtree[rt].max_num = read();
Sqtree[rt].min_num = Sqtree[rt].max_num;
return;
}
int m = (l + r) >> 1;
buildtree(lson);
buildtree(rson);
PushUp(rt);
}
inline int query(int L, int R, int l, int r, int rt, int flag)
{
if(L <= l && r <= R){
return flag==0?Sqtree[rt].min_num:Sqtree[rt].max_num;
}
int m = (l + r) >> 1;
int ret;
if(flag == 1){
ret = 0;
if(L <= m){
ret = max(ret, query(L, R, lson, 1));
}
if(R > m){
ret = max(ret, query(L, R, rson, 1));
}
}
else{
ret = INF;
if(L <= m){
ret = min(ret, query(L, R, lson, 0));
}
if(R > m){
ret = min(ret, query(L, R, rson, 0));
}
}
return ret;
}
int main()
{
//fin;
int N, Q;
int Case;
Case = read();
while(Case--){
N = read();
buildtree(1, N, 1);
Q = read();
while(Q--){
int C, L, R;
C = read();
L = read();
R = read();
if(1 == C){
printf("%d\n", query(L, R, 1, N, 1, 0));
}
else if(2 == C){
printf("%d\n", query(L, R, 1, N, 1, 1));
}else{
printf("%d\n", query(L, R, 1, N, 1, 1)+query(L, R, 1, N, 1, 0));
}
}
}
return 0;
}
或者可以这样写:
struct node{
int max_num;
int min_num;
}Sqtree[maxn<<2];
inline void PushUp(int rt)
{
Sqtree[rt].max_num = max(Sqtree[rt<<1].max_num, Sqtree[rt<<1|1].max_num);
Sqtree[rt].min_num = min(Sqtree[rt<<1].min_num, Sqtree[rt<<1|1].min_num);
}
inline void buildtree(int l, int r, int rt)
{
if(l == r){
Sqtree[rt].max_num = read();
Sqtree[rt].min_num = Sqtree[rt].max_num;
return;
}
int m = (l + r) >> 1;
buildtree(lson);
buildtree(rson);
PushUp(rt);
}
inline int querymax(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return Sqtree[rt].max_num;
}
int m = (l + r) >> 1;
int ret = 0;
if(L <= m){
ret = max(ret, querymax(L, R, lson));
}
if(R > m){
ret = max(ret, querymax(L, R, rson));
}
return ret;
}
inline int querymin(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return Sqtree[rt].min_num;
}
int m = (l + r) >> 1;
int ret = INF;
if(L <= m){
ret = min(ret, querymin(L, R, lson));
}
if(R > m){
ret = min(ret, querymin(L, R, rson));
}
return ret;
}