线段树求调
查看原帖
线段树求调
394729
Weight_of_the_Soul楼主2022/7/21 14:37
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#define ll long long
using namespace std;

const int N = 3e5 + 5;

int n, m;
int a[N], pre[N];
int cnt, las;
int nxt[N];

struct Tree {
    int l, r;
    int mx, mn, pre_mx, ldat, rdat;
    int g;

    #define l(x) t[x].l
    #define r(x) t[x].r
    #define mx(x) t[x].mx
    #define mn(x) t[x].mn
    #define pre_mx(x) t[x].pre_mx
    #define ldat(x) t[x].ldat
    #define rdat(x) t[x].rdat
    #define g(x) t[x].g

}t[N << 2];

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

map <int, int> ds;
set <int> s[N];

#define It set <int> :: iterator

Tree upd(int p) {
    Tree tmp;
    tmp.l = l(p);
    tmp.r = r(p);
    tmp.ldat = ldat(p << 1);
    tmp.rdat = rdat(p << 1 | 1);
    tmp.mx = max(mx(p << 1), mx(p << 1 | 1));
    tmp.mn = min(mn(p << 1), mn(p << 1 | 1));
    tmp.pre_mx = max(pre_mx(p << 1), pre_mx(p << 1 | 1));
    tmp.g = gcd(gcd(g(p << 1), g(p << 1 | 1)), abs(rdat(p << 1) - ldat(p << 1 | 1)));
    return tmp;
}

void build(int p, int l, int r) {
    l(p) = l, r(p) = r;
    if(l == r) {
        ldat(p) = a[l];
        rdat(p) = a[l];
        mx(p) = a[l];
        mn(p) = a[l];
        pre_mx(p) = pre[l];
        g(p) = 0;
        return ;
    }

    int mid = (l + r) >> 1;
    
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);

    t[p] = upd(p);
}

void change(int p, int pos, int x, int pre_mx) {
    if(l(p) == r(p)) {
        ldat(p) = x;
        rdat(p) = x;
        mx(p) = x;
        mn(p) = x;
        pre_mx(p) = pre_mx;
        g(p) = 0;
        return ;
    }

    int mid = (l(p) + r(p)) >> 1;

    if(pos <= mid) change(p << 1, pos, x, pre_mx);
    if(pos > mid) change(p << 1 | 1, pos, x, pre_mx);

    t[p] = upd(p);
}

Tree ask(int p, int l, int r) {
    if(l <= l(p) && r >= r(p)) return t[p];

    int mid = (l(p) + r(p)) >> 1;

    if(l <= mid) return ask(p << 1, l, r);
    if(r > mid) return ask(p << 1 | 1, l, r);
    t[p] = upd(p);
    return t[p];
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        if(ds.find(a[i]) == ds.end()) ds[a[i]] = ++cnt, s[cnt].insert(i);
        else {
            int y = ds[a[i]];
            int z = *--s[y].end();
            pre[i] = z;
            nxt[z] = i;
            s[y].insert(i);
        }
    }
    build(1, 1, n);
    for(int i = 1; i <= n; i++) {
        int opt, x, y;
        scanf("%d", &opt);
        if(opt == 1) {
            scanf("%d%d", &x, &y);
            x ^= las, y ^= las;
            nxt[pre[x]] = nxt[x];
            pre[nxt[x]] = pre[x];
            int z = a[nxt[x]];
            change(1, x, z, pre[x]);
            s[ds[a[x]]].erase(x);
            if(ds.find(y) == ds.end()) {
                ds[y] = ++cnt;
                s[cnt].insert(i);
                pre[x] = nxt[x] = 0;
            } else {
                z = ds[y];
                It it = s[z].lower_bound(x);
                if(it == s[z].begin()) {
                    pre[x] = 0;
                    nxt[x] = *it;
                    pre[*it] = x;
                    change(1, *it, a[*it], x);
                } else if(it == s[z].end()) {
                    it--;
                    pre[x] = *it;
                    nxt[x] = 0;
                    nxt[*it] = x;
                } else {
                    It it1 = --it;
                    it++;
                    pre[x] = *it1;
                    nxt[x] = *it;
                    pre[*it1] = x;
                    pre[*it] = x;
                    change(1, *it, a[*it], x);
                }
            }
            a[x] = y;
            change(1, x, y, pre[x]);
        } else {
            int z;
            scanf("%d%d%d", &x, &y, &z);
            x ^= las, y ^= las, z ^= las;
            Tree tmp = ask(1, x, y);
            if(tmp.mx - tmp.mx == z * (y - x) && (tmp.g == z || !tmp.g) && (tmp.pre_mx < x || !z)) las++, printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}
2022/7/21 14:37
加载中...