P5355 题解

· · 题解

思路

莫队。

此题有一个弱化版:P3674 小清新人渣的本愿,在这题中只有加减乘三个操作,而本题多了一个除。

对于加减乘三个操作可以去看看扶苏的题解,在这篇题解中主要讲讲除的做法。

考虑分类讨论:

代码

#include <bits/stdc++.h>
using namespace std;

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int read() {
    char c = getchar();
    int x = 0;
    bool f = 0;
    for (; !isdigit(c); c = getchar())
        f ^= !(c ^ 45);
    for (; isdigit(c); c = getchar())
        x = (x << 1) + (x << 3) + (c ^ 48);
    if (f)
        x = -x;
    return x;
}

const int maxn = 100100, N = 100010;

struct query {
    int l, r, id, pos, op, v;
} q[maxn];
int n, m, block, a[maxn], cnt[maxn], bel[maxn], tp[maxn], pre[maxn];
bool res[maxn];
bitset<maxn> b1, b2;

struct query2 {
    int l, r, id;
};
vector<query2> qs[500];

bool cmp(query a, query b){
    return a.pos ^ b.pos ? a.l < b.l : ((a.pos & 1) ? a.r < b.r : a.r > b.r);
}

void add(int x) {
    if ((++cnt[x]) == 1) {
        b1[x] = b2[N - x] = 1;
    }
}

void del(int x) {
    if (!(--cnt[x])) {
        b1[x] = b2[N - x] = 0;
    }
}

int main() {
    n = read();
    m = read();
    for (int i = 1; i <= n; ++i) {
        a[i] = read();
    }
    block = sqrt(n);
    for (int i = 1; i <= n; ++i) {
        bel[i] = (i - 1) / block + 1;
    }
    int len = 0;
    for (int i = 1; i <= m; ++i) {
        int op, ql, qr, x;
        op = read();
        ql = read();
        qr = read();
        x = read();
        if (op == 4 && x < block) {
            qs[x].push_back({ql, qr, i});
        } else {
            q[++len] = (query){ql, qr, i, bel[ql], op, x};
        }
    }
    sort(q + 1, q + len + 1, cmp);
    int l = 1, r = 0;
    for (int i = 1; i <= len; ++i) {
        int ql = q[i].l, qr = q[i].r;
        while (l < ql) {
            del(a[l++]);
        }
        while (l > ql) {
            add(a[--l]);
        }
        while (r < qr) {
            add(a[++r]);
        }
        while (r > qr) {
            del(a[r--]);
        }
        int op = q[i].op, x = q[i].v;
        if (op == 1) {
            res[q[i].id] = (b1 & (b1 << x)).any();
        } else if (op == 2) {
            res[q[i].id] = (b1 & (b2 >> (N - x))).any();
        } else if (op == 3) {
            for (int j = 1; j * j <= x; ++j) {
                if (x % j == 0) {
                    if (b1[j] && b1[x / j]) {
                        res[q[i].id] = 1;
                        break;
                    }
                }
            }
        } else {
            int cnt = 0;
            for (int j = 1; j * x <= N; ++j) {
                ++cnt;
                if (b1[j] && b1[j * x]) {
                    res[q[i].id] = 1;
                    break;
                }
            }
        }
    }
    for (int i = 1; i < block; ++i) {
        if (qs[i].empty()) {
            continue;
        }
        memset(pre, 0, sizeof(pre));
        memset(tp, 0, sizeof(tp));
        int l = 0;
        for (int j = 1; j <= n; ++j) {
            pre[a[j]] = j;
            if (a[j] * i <= N) {
                l = max(l, pre[a[j] * i]);
            }
            if (a[j] % i == 0) {
                l = max(l, pre[a[j] / i]);
            }
            tp[j] = l;
        }
        for (int j = 0; j < qs[i].size(); ++j) {
            res[qs[i][j].id] = qs[i][j].l <= tp[qs[i][j].r];
        }
    }
    for (int i = 1; i <= m; ++i) {
        puts(res[i] ? "yuno" : "yumi");
    }
    return 0;
}