目录

P5610-[Ynoi2013] 大学

P5610-[Ynoi2013] 大学

题目:

题目描述:

一个长为 $n$ 的非负整数序列 $a$,支持以下两个操作:

  • 1 l r x:把区间 $[l, r]$ 中所有 $x$ 的倍数除以 $x$。
  • 2 l r:查询区间 $[l, r]$ 的和。

本题强制在线,每次的 $l, r, x$ 需要 xor 上上次答案,如果之前没有询问,则上次答案为 $0$。

输入格式:

第一行两个整数表示 $n, m$。

第二行 $n$ 个非负整数表示 $a_i$。

之后 $m$ 行每行一个操作。

  • 1 l r x:把区间 $[l, r]$ 中所有 $x$ 的倍数除以 $x$。
  • 2 l r:查询区间 $[l, r]$ 的和。

输出格式:

对于每次询问,输出一行一个整数表示答案。

样例:

样例输入1:

1
2
3
4
5

5 2
1 2 3 4 5
1 1 5 2
2 1 5

样例输出1:

1
2

12

思路:

实现:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Problem: P5610 [Ynoi2013] 大学
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5610
// Memory Limit: 500 MB
// Time Limit: 500 ms
// Author: Ybw051114
//
// Powered by CP Editor (https://cpeditor.org)

#include <algorithm>
#include <cstdio>
#include <cstring>

inline char getch()
{
    static const int N = 1e6;
    static char buf[N], *p1 = NULL, *p2 = NULL;
    return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, N, stdin), p1 == p2) ? EOF : *p1++;
}
inline void putch(char c)
{
#define flush() fwrite(buf, 1, len, stdout), len = 0
    static const int N = 1e6;
    static char buf[N];
    static int len = 0;
    if (c == EOF)
        flush();
    else
        buf[len++] = c;
    if (len == N)
        flush();
#undef flush
}

inline long long getint()
{
    long long a = 0;
    char ch;
    do
        ch = getch();
    while (ch < '0' || ch > '9');
    do
        a = a * 10 + ch - '0', ch = getch();
    while (ch >= '0' && ch <= '9');
    return a;
}

inline void writeint(long long a)
{
    static char buf[20], len = 0;
    if (a < 0)
        putch('-'), a = -a;
    do
        buf[len++] = a % 10 + '0', a /= 10;
    while (a);
    while (len)
        putch(buf[--len]);
}

const int N = 1e5 + 10, M = 5e5 + 10, sqrtN = 720;
int n, m, q, a[N], s[M], cnt[M], id[N * sqrtN], fa[N * sqrtN], las[M], nxt[M * 20], to[M * 20], cntt;
inline void add(const int &x, const int &y)
{
    if (cnt[x])
    {
        ++cntt;
        to[cntt] = y;
        nxt[cntt] = las[x];
        las[x] = cntt;
    }
}
inline int find(register int x)
{
    while (x != fa[x])
        x = fa[x] = fa[fa[x]];
    return x;
}
long long c[N], lastans;
inline void update(register int x, const int &a)
{
    for (; x <= n; x += x & -x)
        c[x] += a;
}
inline long long query(register int x)
{
    register long long a = 0;
    for (; x; x -= x & -x)
        a += c[x];
    return a;
}
inline int lower_bound(register int l, register int r, const int &x)
{
    register int m;
    while (l < r)
    {
        m = (l + r) >> 1;
        if (x <= id[m])
            r = m;
        else
            l = m + 1;
    }
    return l;
}
inline int upper_bound(register int l, register int r, const int &x)
{
    int m;
    while (l < r)
    {
        m = (l + r) >> 1;
        if (x < id[m])
            r = m;
        else
            l = m + 1;
    }
    return l;
}
int main()
{
    register int i, j, opt;
    register long long l, r, x;
    n = getint();
    q = getint();
    for (i = 1; i <= n; ++i)
    {
        a[i] = getint();
        if (m < a[i])
            m = a[i];
        ++cnt[a[i]];
        update(i, a[i]);
    }
    for (i = 2; i <= m; ++i)
    {
        add(i, i);
        for (j = i << 1; j <= m; j += i)
            cnt[i] += cnt[j], add(j, i);
    }
    for (i = 2; i <= m + 1; ++i)
        s[i] = s[i - 1] + cnt[i - 1] + 2, cnt[i - 1] = 0, id[s[i] - 1] = 1e9;
    for (i = 1; i <= s[m + 1]; ++i)
        fa[i] = i;
    for (i = 1; i <= n; ++i)
        if (a[i] > 1)
        {
            for (j = las[a[i]]; j; j = nxt[j])
                id[s[to[j]] + ++cnt[to[j]]] = i;
        }
    while (q--)
    {
        opt = getint();
        l = getint() ^ lastans;
        r = getint() ^ lastans;
        if (opt == 1)
        {
            x = getint() ^ lastans;
            if (x > 1)
            {
                l = find(lower_bound(s[x], s[x + 1] - 1, l));
                r = find(upper_bound(s[x], s[x + 1] - 1, r));
                for (i = l; i < r; i = find(i + 1))
                    if (a[id[i]] % x)
                        fa[i] = find(i + 1);
                    else
                        update(id[i], -a[id[i]] + a[id[i]] / x), a[id[i]] /= x;
            }
        }
        else
            writeint(lastans = query(r) - query(l - 1)), putch('\n');
    }
    putch(EOF);
    return 0;
}