关于快读与cin
  • 板块学术版
  • 楼主muvum
  • 当前回复10
  • 已保存回复10
  • 发布时间2021/8/11 20:52
  • 上次更新2023/11/4 11:00:22
查看原帖
关于快读与cin
235696
muvum楼主2021/8/11 20:52

在做这道题的时候,我使用了快读输入,结果本地输出对提交错,改用cin后就A了,请问各位大佬能不能解释一下。

快读版本 \downarrow

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define l (i << 1)
#define r (i << 1 | 1)
#define lchild l, s, mid
#define rchild r, mid + 1, t
#define MAXN (int)2e5 + 5

inline int read() {
    int x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1; c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();
    }
    return x * f;
}

int n, m;
int max[4*MAXN], leaf[MAXN];

inline void pushup(int i) {
    if (max[l] > max[i]) max[i] = max[l];
    if (max[r] > max[i]) max[i] = max[r];
}

void build(int i, int s, int t) {
    if (s == t) {
        max[i] = read();
        leaf[s] = i; // 记录叶子区间的编号,即ID为s的人的成绩
        return;
    }

    int mid = s + ((t - s) >> 1);
    build(lchild); build(rchild);
    pushup(i);
}
// p是修改位置,v是要修改成的值
void update(int p, int v, int i, int s, int t) {
    if (s == t) {
        if (v > max[i]) max[i] = v;
        return;
    }

    int mid = s + ((t - s) >> 1);
    if (p <= mid) update(p, v, lchild);
    else update(p, v, rchild);
    pushup(i);
}
// L是求最大值的左端点,R是右端点
int getmax(int L, int R, int i, int s, int t) {
    if (L <= s && t <= R) return max[i];

    int mid = s + ((t - s) >> 1);
    int maxL = -1e9, maxR = -1e9;
    if (L <= mid) maxL = getmax(L, R, lchild); // 左子区间最大值
    if (R > mid)  maxR = getmax(L, R ,rchild); // 右子区间最大值
    return (maxL > maxR) ? maxL : maxR;
}

int main(void) {
    n = read(); m = read();

    build(1, 1, n);

    for (int i=1; i<=m; i++) {
        char c; scanf("%c", &c);
        int L, R; L = read(); R = read();
        if (c == 'Q') printf("%d\n", getmax(L, R, 1, 1, n));
        else if (max[leaf[L]] < R) update(L, R, 1, 1, n);
    }

    return 0;
}

cin版本 \downarrow

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define l (i << 1)
#define r (i << 1 | 1)
#define lchild l, s, mid
#define rchild r, mid + 1, t
#define MAXN (int)2e5 + 5

int n, m;
int max[4*MAXN], leaf[MAXN];

inline void pushup(int i) {
    if (max[l] > max[i]) max[i] = max[l];
    if (max[r] > max[i]) max[i] = max[r];
}

void build(int i, int s, int t) {
    if (s == t) {
        std::cin >> max[i];
        leaf[s] = i; // 记录叶子区间的编号,即ID为s的人的成绩
        return;
    }

    int mid = s + ((t - s) >> 1);
    build(lchild); build(rchild);
    pushup(i);
}
// p是修改位置,v是要修改成的值
void update(int p, int v, int i, int s, int t) {
    if (s == t) {
        if (v > max[i]) max[i] = v;
        return;
    }

    int mid = s + ((t - s) >> 1);
    if (p <= mid) update(p, v, lchild);
    else update(p, v, rchild);
    pushup(i);
}
// L是求最大值的左端点,R是右端点
int getmax(int L, int R, int i, int s, int t) {
    if (L <= s && t <= R) return max[i];

    int mid = s + ((t - s) >> 1);
    int maxL = -1e9, maxR = -1e9;
    if (L <= mid) maxL = getmax(L, R, lchild); // 左子区间最大值
    if (R > mid)  maxR = getmax(L, R ,rchild); // 右子区间最大值
    return (maxL > maxR) ? maxL : maxR;
}

int main(void) {
    std::ios::sync_with_stdio(false);

    std::cin >> n >> m;

    build(1, 1, n);

    for (int i=1; i<=m; i++) {
        char c; int L, R;
        std::cin >> c >> L >> R;
        if (c == 'Q') std::cout << getmax(L, R, 1, 1, n) << std::endl;
        else if (max[leaf[L]] < R) update(L, R, 1, 1, n);
    }

    return 0;
}
2021/8/11 20:52
加载中...