CSP-J/S模板复习

【模板】堆[普及-]

题目描述

给定一个数列,初始为空,请支持下面三种操作:

  1. 给定一个整数 x x x,请将 x x x 加入到数列中。
  2. 输出数列中最小的数。
  3. 删除数列中最小的数(如果有多个数最小,只删除 1 1 1 个)。

输入格式

第一行是一个整数,表示操作的次数 n n n
接下来 n n n 行,每行表示一次操作。每行首先有一个整数 o p op op 表示操作类型。

  • o p = 1 op = 1 op=1,则后面有一个整数 x x x,表示要将 x x x 加入数列。
  • o p = 2 op = 2 op=2,则表示要求输出数列中的最小数。
  • o p = 3 op = 3 op=3,则表示删除数列中的最小数。如果有多个数最小,只删除 1 1 1 个。

输出格式

对于每个操作 2 2 2,输出一行一个整数表示答案。

样例 #1

样例输入 #1

5
1 2
1 5
2
3
2

样例输出 #1

2
5

提示

【数据规模与约定】

  • 对于 100 % 100\% 100% 的数据,保证 1 ≤ n ≤ 1 0 6 1 \leq n \leq 10^6 1n106 1 ≤ x < 2 31 1 \leq x \lt 2^{31} 1x<231 o p ∈ { 1 , 2 , 3 } op \in \{1, 2, 3\} op{ 1,2,3}

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n, a[N], len;
void build(int x) {
   
    a[++len] = x;
    int now = len;
    while (len != 1) {
   
        if (a[len] < a[len / 2]) {
   
            swap(a[len], a[len / 2]);
            len >>= 1;
        } else
            break;
    }
}
void get() {
   
    a[1] = a[len--];
    int now = 1;
    while (now <= len / 2) {
   
        int id = now * 2;
        if (id + 1 <= len && a[id + 1] < a[id])
            id++;
        if (a[now] > a[id])
            swap(a[id], a[now]), now = id;
        else
            break;
    }
}
int main() {
   
    cin >> n;
    for (int i = 1; i <= n; i++) {
   
        int x;
        scanf("%d", &x);
        if (x == 2) printf("%d\n", a[1]);
        else if (x == 3) get();
        else scanf("%d", &x), build(x);
    }
    return 0;
}

Flag:luogu模板缩点[普及+/提高]!!!

P3385 【模板】负环[普及/提高-]

【模板】ST 表[普及/提高-]

题目背景

这是一道 ST 表经典题——静态区间最大值

请注意最大数据时限只有 0.8s,数据强度不低,请务必保证你的每次查询复杂度为 O ( 1 ) O(1) O(1)。若使用更高时间复杂度算法不保证能通过。

如果您认为您的代码时间复杂度正确但是 TLE,可以尝试使用快速读入:

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-48;ch=getchar();}
	return x*f;
}

函数返回值为读入的第一个整数。

快速读入作用仅为加快读入,并非强制使用。

题目描述

给定一个长度为 N N N 的数列,和 $ M $ 次询问,求出每一次询问的区间内数字的最大值。

输入格式

第一行包含两个整数 N , M N,M N,M,分别表示数列的长度和询问的个数。

第二行包含 N N N 个整数(记为 a i a_i ai),依次表示数列的第 i i i 项。

接下来 M M M 行,每行包含两个整数 l i , r i l_i,r_i li,ri,表示查询的区间为 [ l i , r i ] [l_i,r_i] [li,ri]

输出格式

输出包含 M M M 行,每行一个整数,依次表示每一次询问的结果。

样例 #1

样例输入 #1

8 8
9 3 1 7 5 6 0 8
1 6
1 5
2 7
2 6
1 8
4 8
3 7
1 8

样例输出 #1

9
9
7
7
9
8
7
9

提示

对于 100 % 100\% 100% 的数据,满足 1 ≤ N ≤ 10 5 1\le N\le {10}^5 1N105 1 ≤ M ≤ 2 × 10 6 1\le M\le 2\times{10}^6 1M2×106 a i ∈ [ 0 , 10 9 ] a_i\in[0,{10}^9] ai[0,109] 1 ≤ l i ≤ r i ≤ N 1\le l_i\le r_i\le N 1liriN

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n, nn, m, a[N], f[N][23], Logn[N];
template <typename T> void read(T &x){
   
	x = 0; int f = 1; char ch = getchar();
	while (!isdigit(ch)) {
   if (ch == '-') f = -1; ch = getchar();}
	while (isdigit(ch)) {
   x = x * 10 + ch - '0'; ch = getchar();}
	x *= f;
}
void init() {
   
    cin >> n >> m;
    for( int i = 1;i <= n;i++) read(f[i][0]);
    Logn[1] = 0, Logn[2] = 1;
    for( int i = 3;i < N;i++ ) {
   
        Logn[i] = Logn[(i >> 1)] + 1;
    }
    nn = log2(n);
}

int main() {
   
    init();
    for(int j = 1;j <= nn;j++ )
        for(int i = 1;i + (1 << j) - 1 <= n;i++ )
            f[i][j] = max( f[i][j - 1],f[i + (1 << (j - 1))][j - 1]);
    for(int i = 1, x, y;i <= m;i++) {
   
        read(x), read(y);
        int s = Logn[y - x + 1];
        printf("%d\n", max(f
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值