QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#896013#7462. Brodal queueyaoyanfeng48 2863ms409432kbC++987.0kb2025-02-12 20:42:412025-02-12 20:42:49

Judging History

This is the latest submission verdict.

  • [2025-02-12 20:42:49]
  • Judged
  • Verdict: 48
  • Time: 2863ms
  • Memory: 409432kb
  • [2025-02-12 20:42:41]
  • Submitted

answer

#include <bits/stdc++.h>
#define ll long long
#define eb push_back
namespace IO {
	const int S = (1 << 21);
	int digital = 2;
	bool is_round = 1;
	char b[S], *m = b, *n = b, g[S], *p = g;
	inline void f() { fwrite(g, 1, p - g, stdout), p = g; }
	struct R {
		inline bool w(char c) { return 47 < c && c < 58; }
		inline bool y(char c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t'; }
		inline char i() { if (m == n) n = (m = b) + fread(b, 1, S, stdin); return m == n ? ' ' : *m++; }
		template <class T> inline R &operator>>(T &x) { char c = i(); T f = 1; for (x = 0; !w(c);) { if(c == '-') f = -1; c = i(); } while (w(c)) x *= 10, x += c ^ 48, c = i(); x *= f; return *this; }
		inline R &operator>>(char *s) { int l = 0; char c; operator>>(c); for (; !y(c); s[l++] = c, c = i()) ; s[l] = '\0'; return *this; }
		inline R &operator>>(char &c) { do c = i(); while (y(c)); return *this; }
		template<class T> void tie(T x) {}
	} cin;
#define endl '\n'
	struct W {
		inline void o(char c) { *p++ = c; if (p - g == S) f(); }
		template <class T> inline W &operator<<(T x) { if (!x) return o(48), *this; if(x < 0) o('-'), x = -x; static int s[64], t = 0; while (x) s[++t] = x % 10, x /= 10; while (t) o(s[t--] + 48); return *this; }
		inline W &operator<<(char c) { o(c); return *this; }
		template <class T> inline W &operator<<(T *s) { int c = 0; while (s[c]) o(s[c++]); return *this; }
		template<class T> void tie(T x) {}
		~W() { f(); }
	} cout;
	struct E { template <class T> E &operator<<(T x) { return *this; } } cerr;
} ;

#define cin IO::cin
#define cout IO::cout
using namespace std;
const int N = 200200, B = 505, TEST = 0, OPT = 0, DBG = 0;
int n, m, s, t, sz;
int a[N];
int bl[B], br[B], fm[N];
int cnt[N][B];
int fz[N];
bool vis[N];
ll sum[B], f[B][B], tagx[B][B], tagy[B][B];
struct node{
    int l, r, col;
    node() {}
    node(int _l, int _r, int _col)
        : l(_l), r(_r), col(_col) {}
} cc;
vector<node> c[B];
inline void getinc(int t) {
    c[t].clear();
    for (int x = bl[t], y; x <= br[t]; x = y + 1) {
        y = x;
        while (y < br[t] && a[y + 1] == a[y]) ++y;
        c[t].eb(node(x, y, a[x]));
    }
    return;
}
inline void bckinc(int t) {
    sz = c[t].size();
    for (int i = 0; i < sz; ++i) {
        cc = c[t][i];
        fill(a + cc.l, a + cc.r + 1, cc.col);
    }
    return;
}
inline long long sqr(int x) {return 1ll * x * x;}
inline int qr(int x, int v) {return cnt[v][x] - cnt[v][x - 1];}
void init() {
    s = min(n, 1400);
    for (int l = 1, r = s; l <= n; l = r + 1, r = min(r + s, n)) {
        bl[++t] = l, br[t] = r;
        for (int i = l; i <= r; ++i) fm[i] = t;
        getinc(t);
        if (c[t].size() > 1) for (int i = l; i <= r; ++i) ++cnt[a[i]][t];
        for (int i = 1; i <= n; ++i) cnt[i][t] += cnt[i][t - 1], sum[t] += sqr(cnt[i][t]);
    }
    for (int i = 1; i <= t; ++i) {
        for (int j = 1; j <= n; ++j) f[i][i] += sqr(cnt[j][i]);
        for (int j = i + 1; j <= t; ++j) {
            f[i][j] = f[i][j - 1];
            sz = c[j].size();
            for (int k = 0; k < sz; ++k) {
                cc = c[j][k];
                if (!vis[cc.col]) f[i][j] += 1ll * cnt[cc.col][i] * qr(j, cc.col), vis[cc.col] = 1;
            }
            for (int k = 0; k < sz; ++k) vis[c[j][k].col] = 0;
        }
    }
    return;
}
vector<int> vec;
inline void add(int k, int v, ll x) {
    if (x == 0) return;
    for (int i = 1; i < k; ++i) tagx[k][i] += x * cnt[v][i];
    for (int i = k; i <= t; ++i) {
        ll v1 = x * cnt[v][i], v2 = v1 + sqr(x);
        tagx[i][i] += v1, tagy[k][i] += v2, sum[i] += sqr(x) + (v1 << 1), cnt[v][i] += x;
    }
    return;
}
inline ll askf(int l, int r) {
    ll res = f[l][r];
    for (int i = 1; i <= r; ++i) res += tagx[i][l];
    for (int i = 1; i <= l; ++i) res += tagy[i][r];
    return res;
}
inline ll ask(int l, int r) {
    if (l > r) return 0;
    return sum[r] + sum[l - 1] - askf(l - 1, r) * 2;
}
void updsm(int id, int l, int r, int v) {
    if (c[id].size() == 1 && (v == c[id][0].col || l == bl[id] && r == br[id])) return c[id][0].col = v, void();
    bckinc(id);
    bool flg = 1;
    for (int i = bl[id]; i <= br[id]; ++i) if (i < l || i > r) flg &= (a[i] == v);
    if (flg) {
        sz = c[id].size();
        for (int i = 0; i < sz; ++i) {
            cc = c[id][i];
            add(id, cc.col, -(cc.r - cc.l + 1));
        }
    }
    else {
        fz[v] += (!flg) * (r - l + 1), vec.eb(v), vis[v] = 1;
        if (c[id].size() != 1) for (int i = l; i <= r; ++i) {
            fz[a[i]]--;
            if (!vis[a[i]]) vec.eb(a[i]), vis[a[i]] = 1;
        }
        else for (int i = bl[id]; i <= br[id]; ++i) if (i < l || i > r) {
            ++fz[a[i]];
            if (!vis[a[i]]) vec.eb(a[i]), vis[a[i]] = 1;
        }
        sz = vec.size();
        for (int k = 0; k < sz; ++k) {
            int x = vec[k];
            add(id, x, fz[x]), fz[x] = vis[x] = 0; vec.clear();
        }
    }
    for (int i = l; i <= r; ++i) a[i] = v;
    getinc(id);
    return;
}
void updlrg(int id, int x) {
    if (c[id].size() == 1) c[id][0].col = x;
    else {
        sz = c[id].size();
        for (int i = 0; i < sz; ++i) cc = c[id][i], add(id, cc.col, -(cc.r - cc.l + 1));
        c[id].clear(), c[id].eb(node(bl[id], br[id], x));
    }
    return;
}
void change(int l, int r, int x) {
    int il = fm[l], ir = fm[r];
    if (il == ir) return updsm(il, l, r, x), void();
    updsm(il, l, br[il], x), updsm(ir, bl[ir], r, x);
    for (int i = il + 1; i < ir; ++i) updlrg(i, x);
    return;
}
ll query(int l, int r) {
    int il = fm[l], ir = fm[r];
    ll sum = 0;
    if (il == ir) {
        bckinc(il);
        for (int i = l; i <= r; ++i) ++fz[a[i]];
        for (int i = l; i <= r; ++i) if (!vis[a[i]]) sum += sqr(fz[a[i]]), vis[a[i]] = 1;
        for (int i = l; i <= r; ++i) vis[a[i]] = 0, fz[a[i]] = 0;
        return sum;
    }
    bckinc(il), bckinc(ir);
    for (int i = l; i <= br[il]; ++i) {
        ++fz[a[i]];
        if (!vis[a[i]]) vec.eb(a[i]), vis[a[i]] = 1;
    }
    for (int i = bl[ir]; i <= r; ++i) {
        ++fz[a[i]];
        if (!vis[a[i]]) vec.eb(a[i]), vis[a[i]] = 1;
    }
    for (int i = il + 1, pc; i < ir; ++i) if (c[i].size() == 1) {
        fz[(pc = c[i][0].col)] += br[i] - bl[i] + 1;
        if (!vis[pc]) vec.eb(pc), vis[pc] = 1;
    }
    sum = ask(il + 1, ir - 1), sz = vec.size();
    for (int i = 0; i < sz; ++i) {
        int x = vec[i];
        sum += sqr(fz[x]) + 2ll * (cnt[x][ir - 1] - cnt[x][il]) * fz[x];
        vis[x] = 0, fz[x] = 0;
    }
    vec.clear();
    return sum;
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    init();
    ll lstans = 0;
    for (ll i = 1, op, l, r, x; i <= m; ++i) {
        cin >> op >> l >> r;
        if (!TEST) lstans %= (1ll << 32), l ^= lstans, r ^= lstans;
        if (op == 1) {
            cin >> x, x ^= TEST ? 0 : lstans;
            change(l, r, x);
        }
        else cout << (lstans = (query(l, r) - r + l - 1) / 2) << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 1
Accepted

Test #1:

score: 1
Accepted
time: 0ms
memory: 13908kb

input:

10 12
6 9 9 4 7 8 10 4 9 2
2 1 4
1 0 5 0
2 3 6
2 10 9
1 7 9 2
2 7 9
1 2 7 1
1 2 11 4
2 6 10
1 3 12 0
1 14 14 15
2 7 12

output:

1
3
0
3
6
16

result:

ok 6 numbers

Subtask #2:

score: 9
Accepted

Test #2:

score: 9
Accepted
time: 2863ms
memory: 407360kb

input:

200000 200000
1801 1645 561 1609 920 737 249 121 609 966 220 209 641 761 1475 284 595 220 1853 905 1705 399 1737 264 551 681 119 81 1529 1884 1905 641 257 1241 241 401 675 442 505 1565 1391 1395 1803 1589 55 1521 945 229 167 1030 996 73 1473 387 400 1261 1559 405 1573 1823 1029 937 17 1810 1885 401 ...

output:

28358
517270
14881
2254491
8001066
1545378
642772
110901
407089
452894
666513
54715
1625913
1441
6923245
9696
49136
816131
2509617
1312876
343815
456703
1356423
1836337
1746327
660004
1089819
13914095
2148442
10048565
5171534
4750216
4919749
101478
577992
1103366
2608074
8109
519702
50997
5176718
95...

result:

ok 200000 numbers

Test #3:

score: 9
Accepted
time: 1812ms
memory: 409108kb

input:

200000 200000
17 183 173 114 41 1 53 88 168 151 98 126 41 63 151 141 26 70 171 97 189 20 41 90 33 27 181 193 11 67 194 9 193 21 121 73 49 183 23 53 179 101 173 101 181 151 113 41 41 200 87 61 21 160 21 197 137 1 178 165 25 17 99 101 123 41 141 179 18 135 9 63 93 1 17 121 181 165 23 84 151 72 185 195...

output:

57453674
8912267
106941240
43372080
3514
4140700
70940924
866065
45821048
93782304
2960142
6633447
4220263
2087922
23603247
16758529
39062682
28259929
13332277
4977435
60030213
8090899
242318
133034690
40197440
8097004
41318154
4955270
45526625
7549867
14419640
22190442
920
1509331
113034320
3984547...

result:

ok 200000 numbers

Test #4:

score: 9
Accepted
time: 2621ms
memory: 406764kb

input:

200000 200000
1 1 1 1 1 1 1 1 2 1 1 2 2 1 1 1 1 1 2 2 1 1 2 1 1 2 1 1 2 1 1 1 2 2 2 1 1 1 1 2 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 2 2 2 1 1 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 1 1 2 1 1 1 1 2 2 2 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 2 1 1 1 2 2 2 1 1 1 1 1 1 2 2 2 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 ...

output:

20458927
2278131900
5335501156
1812018679
794532792
213524614
807699550
679149900
2715059560
5276018812
1839228536
246261631
3409678917
492124212
36827175
6883274572
11190063
1028136358
2601277905
8103196
3723752846
84746491
3612860367
8310108440
2108227681
33432402
618846546
571630
141952752
517795...

result:

ok 200000 numbers

Subtask #3:

score: 19
Accepted

Dependency #1:

100%
Accepted

Test #5:

score: 19
Accepted
time: 0ms
memory: 13936kb

input:

500 500
281 301 126 111 121 485 237 185 243 443 279 146 116 365 413 403 227 461 79 215 136 58 442 385 416 31 421 260 77 89 231 196 401 409 406 385 421 331 193 193 251 79 76 301 364 222 286 129 437 91 181 77 389 11 303 217 318 391 69 171 477 1 26 225 87 385 253 86 41 217 59 399 186 249 391 61 373 416...

output:

13
166
36
7
75
2
37
7261
21989
22791
9394
2981
10
10324
45
3321
17296
9018
1128
1495
210
6226
10591
12387
5202
2953
6985
465
20366
10223
9493
14196
4934
45
15576
5251
8881
48948
22578
33299
153
20155
820
3381
31878
1282
11752
6643
1442
23822
3
43876
3486
15576
2121
4591
1081
4095
16662
210
14130
881...

result:

ok 263 numbers

Test #6:

score: 19
Accepted
time: 1ms
memory: 14632kb

input:

500 500
21 39 5 25 25 35 9 11 29 31 9 43 36 41 14 26 6 11 37 43 37 31 1 41 35 1 1 22 7 5 41 35 15 30 45 1 9 13 47 1 33 31 26 16 17 7 19 6 6 11 31 19 17 35 1 15 25 3 11 11 9 29 3 31 17 17 50 19 13 36 33 15 46 44 35 41 39 15 1 9 32 36 6 33 29 13 49 15 13 11 25 13 21 45 27 26 15 40 47 33 21 17 2 7 6 45...

output:

13954
12
13235
14316
6976
3235
22147
9845
16963
15030
2815
2199
3598
1080
564
105
44
15
35995
38233
1424
10962
13730
1081
18
9673
20089
2908
2926
5253
3602
472
21115
12561
48342
26565
3672
41622
3282
6786
24753
20110
595
18683
91
6335
0
35998
561
66
11959
8778
23279
3457
15
4662
37026
69045
210
1414...

result:

ok 238 numbers

Subtask #4:

score: 0
Time Limit Exceeded

Dependency #2:

100%
Accepted

Test #7:

score: 0
Time Limit Exceeded

input:

200000 200000
42117 11359 41220 12879 46629 34683 41056 7257 25769 48971 43167 9001 4589 3689 16459 23491 30843 15858 43281 24001 25831 43921 43680 19593 37551 43235 488 11642 36361 28687 23661 38456 16817 39826 30687 28321 6961 35601 20653 21989 32641 38223 15626 24193 15209 36626 36576 17166 14001...

output:

418951
29901
177
141979
53172
29610
756
1876
175800
9203
19666
46632
10724
195643
64577
57117
113508
68182
52178
55297
172867
220232
71578
39447
108116
395819
60262
169911
24597
38336
214485
27264
201704
51291
249199
64
2231
148307
8203
136171
52703
34537
37939
6771
9211
147445
96648
189649
71870
85...

result:


Subtask #5:

score: 19
Accepted

Test #12:

score: 19
Accepted
time: 1620ms
memory: 409432kb

input:

200000 200000
96162 15481 51305 66323 58345 67841 63351 179462 4914 118157 110677 76845 126971 69269 112773 199665 196553 135326 72861 194617 101133 147301 186526 55880 25433 71005 84700 54329 127457 80123 92345 198413 118472 128393 52712 178299 77837 30176 186913 88131 184501 51913 138202 142540 17...

output:

61487
18213
538754
9208486
2827151001
435128213
197337174
10841496
63467011
69331200
1337604281
1788330451
1622013202
1268525593
2097519070
255572136
8864376162
132934665
7413238977
3799037054
767017946
2841968268
1998310281
53898153
134537406
656686920
1920829680
29467951
114547402
856375126
663208...

result:

ok 99408 numbers

Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%