题解 P3674 【小清新人渣的本愿】

· · 题解

题意简述

尽管题意已经够简洁了但是我还是要坚持我的题解风格

题解

dllxl难的的一个比较水的题。

我们初一看这道题目。没修改,不强制在线,基本上大思路就往莫队走了。

考虑一种暴力做法,对三个操作分别开桶算贡献,加法减法的计算方法比较简单,加法就是减法的逆运算,反着开就行了。乘法直接枚举约数,毕竟这道题的值域和数列长度及询问是一样的。

发现容易炸,又因为我们的桶是根据存在性开的,所以我们可以用bitset来优化。

记录两个bitset为classic和inverse。

如果为操作一的话就是:

\operatorname{ANS_{Q_{i}->id}}=(\operatorname{classic\ bitand\ (classic\ shl\ Q_{i}->x)}).\operatorname{any()}

操作二类似:

\operatorname{ANS_{Q_{i}->id}}=(\operatorname{classic\ bitand\ (inverse\ shr\ (100000-Q_{i}->x))}).\operatorname{any()}

操作三则直接枚举。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <bitset>

using namespace std;

char buf[1 << 21], *p1 = buf, *p2 = buf;
#ifndef ONLINE_JUDGE
#define gc() getchar()
#else
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#endif
#define is() (ch >= '0' && ch <= '9')

template < typename Type >
void read(Type& a) {
    a = 0; char ch; bool f = 0;
    while (!(ch = gc(), is())) if (ch == '-') f = 1;
    while (is()) a = (a << 3) + (a << 1) + (ch ^ '0'), ch = gc();
    a = (f ? -a : a);
}

template < typename Type, typename... Args >
void read(Type& t, Args&... args) {
    read(t), read(args...);
}

const int N = 1e5 + 5, R = 100000;
struct Query_Node {
    int id, pos;
    int tp, l, r, x;
    bool operator < (const Query_Node& rhs) const {
        if (pos != rhs.pos)     return pos < rhs.pos;
        else    return r < rhs.r;
    }
} e[N];
int n, m, Size, a[N], cnt[N], ans[N];
bitset < N > classic, inverse;

void Add(int x) {
    ++cnt[a[x]];
    if (cnt[a[x]] == 1) {
        classic[a[x]] = 1;
        inverse[R - a[x]] = 1;
    }
}

void Del(int x) {
    --cnt[a[x]];
    if (cnt[a[x]] == 0) {
        classic[a[x]] = 0;
        inverse[R - a[x]] = 0;
    }
}

void Contribute() {
    int l = 1, r = 0;
    for (int i = 1; i <= m; ++i) {
        while (l < e[i].l) Del(l++);
        while (l > e[i].l) Add(--l);
        while (r > e[i].r) Del(r--);
        while (r < e[i].r) Add(++r);
        if (e[i].tp == 1) {
            ans[e[i].id] = (classic & (classic << e[i].x)).any();
        } else if (e[i].tp == 2) {
            ans[e[i].id] = (classic & (inverse >> (R - e[i].x))).any();
        } else {
            for (int j = 1; j * j <= e[i].x; ++j) {
                if (e[i].x % j == 0 && classic[j] && classic[e[i].x / j])
                    ans[e[i].id] = 1;
            }
        }
    }
}

signed main() {
    read(n, m), Size = sqrt(n);
    for (int i = 1; i <= n; ++i)    read(a[i]);
    for (int i = 1; i <= m; ++i) {
        read(e[i].tp, e[i].l, e[i].r, e[i].x);
        e[i].id = i, e[i].pos = (e[i].l - 1) / Size + 1;
    }
    sort(e + 1, e + 1 + m);
    Contribute();
    for (int i = 1; i <= m; ++i)    puts(ans[i] ? "hana" : "bi");
    return 0;
}