P9335 [Ynoi2001] 雪に咲く花

· · 题解

思路:

我们令 A(l, r), B(l, r), C(l, r) 分别表示这三个贡献。

考虑离线后扫描线,但是发现 q 特别大,应该是 O(1) 级别的,于是维护增量,即我们令 s_i 表示 i 到当前这个右端点 r 的区间子区间贡献之和,当 r \to r + 1 时,我们有:

s_i \gets s_i + \sum_{l = i}^{r + 1} A(l, r + 1) B(l, r + 1) C(l, r + 1)

这个式子过于庞大了,但是注意到区间与/或/公约数,当固定一个端点时总取值只有 \log V 种,于是我们可以令 A_i = A(i, r), B_i = B(i, r), C_i = C(i, r),每次右端点 r \to r + 1,暴力倒着三个指针枚举左端点去更新 A_l, B_l, C_l,如果无法改变了则退出。

于是考虑答案:

s_i \gets s_i + \sum_{l = i}^{r + 1} A_l B_l C_l

发现还是不太好整体维护,令:

sum_i = \sum_{k = 1}^i A_k B_k C_k

于是 s_i \gets s_i + sum_{r + 1} - sum_{i - 1},对 sum 来说,因为只是单点修改,相当于只需要后缀加;因为 s_i 每次先整体加 sum_{r + 1},然后再减去 sum_{i - 1},所以当我们查询 s_i 时,相当于询问 sum_{i - 1} 的历史和。

看起来还是不太好做,但是你会发现,每次右移右端点,你因为要更新 A, B, C,所以你可以顺路找到这些位置的后缀加的贡献,即可以找到所以 sum_i 的变化量,此时在维护一个 tim_i 表示上次修改的时间,查询时多减去 (now - tim_i) sum_{i - 1} 即可。

时间复杂度为 O(n \log w + q)。

完整代码:

#pragma GCC target("avx2")
#include<bits/stdc++.h>
#define mkp(x, y) make_pair(x, y)
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define lowbit(x) x & (-x)
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int uint;
bool Begin;
const int N = 1e6 + 1, M = 5e6 + 1;
namespace FastIO {
// ------------------------------
// #define IN_HAS_NEG
// #define OUT_HAS_NEG
// #define CHK_EOF
#define DISABLE_MMAP
// ------------------------------
#if __cplusplus < 201400
#error Please use C++14 or higher.
#endif
#if __cplusplus > 201700
#define INLINE_V inline
#else
#define INLINE_V
#endif
#if (defined(LOCAL) || defined(_WIN32)) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifndef DISABLE_MMAP
#include <sys/mman.h>
#endif
#ifdef LOCAL
    inline char gc() { return getchar(); }
    inline void pc(char c) { putchar(c); }
#else
#ifdef DISABLE_MMAP
    INLINE_V constexpr int _READ_SIZE = 1 << 18;
    INLINE_V static char _read_buffer[_READ_SIZE], *_read_ptr = nullptr,
    *_read_ptr_end = nullptr;
    inline char gc() {
        if (__builtin_expect(_read_ptr == _read_ptr_end, false)) {
            _read_ptr = _read_buffer;
            _read_ptr_end =
            _read_buffer + fread(_read_buffer, 1, _READ_SIZE, stdin);
#ifdef CHK_EOF
            if (__builtin_expect(_read_ptr == _read_ptr_end, false)) return EOF;
#endif
        }
        return *_read_ptr++;
    }
#else
    INLINE_V static const char *_read_ptr =
    (const char *)mmap(nullptr, INT_MAX, 1, 2, 0, 0);
    inline char gc() { return *_read_ptr++; }
#endif
    INLINE_V constexpr int _WRITE_SIZE = 1 << 18;
    INLINE_V static char _write_buffer[_WRITE_SIZE], *_write_ptr = _write_buffer;
    inline void pc(char c) {
        *_write_ptr++ = c;
        if (__builtin_expect(_write_buffer + _WRITE_SIZE == _write_ptr, false)) {
            fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
            _write_ptr = _write_buffer;
        }
    }
    INLINE_V struct _auto_flush {
        ~_auto_flush() {
            fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
        }
    } _auto_flush;
#endif
#ifdef CHK_EOF
    inline bool _isdigit(char c) { return (c & 16) && c != EOF; }
    inline bool _isgraph(char c) { return c > 32 && c != EOF; }
#else
    inline bool _isdigit(char c) { return c & 16; }
    inline bool _isgraph(char c) { return c > 32; }
#endif
    template <class T>
    INLINE_V constexpr bool _is_integer = numeric_limits<T>::is_integer;
    template <class T>
    INLINE_V constexpr bool _is_signed = numeric_limits<T>::is_signed;
    template <class T>
    INLINE_V constexpr bool _is_unsigned = _is_integer<T> && !_is_signed<T>;
    template <>
    INLINE_V constexpr bool _is_integer<__int128> = true;
    template <>
    INLINE_V constexpr bool _is_integer<__uint128_t> = true;
    template <>
    INLINE_V constexpr bool _is_signed<__int128> = true;
    template <>
    INLINE_V constexpr bool _is_unsigned<__uint128_t> = true;
#undef INLINE_V
    inline void read(char &c) {
        do c = gc();
        while (!_isgraph(c));
    }
    inline void read_cstr(char *s) {
        char c = gc();
        while (!_isgraph(c)) c = gc();
        while (_isgraph(c)) *s++ = c, c = gc();
        *s = 0;
    }
    inline void read(string &s) {
        char c = gc();
        s.clear();
        while (!_isgraph(c)) c = gc();
        while (_isgraph(c)) s.push_back(c), c = gc();
    }
#ifdef IN_HAS_NEG
    template <class T, enable_if_t<_is_signed<T>, int> = 0>
    inline void read(T &x) {
        char c = gc();
        bool f = true;
        x = 0;
        while (!_isdigit(c)) {
            if (c == 45) f = false;
            c = gc();
        }
        if (f)
            while (_isdigit(c)) x = x * 10 + (c & 15), c = gc();
        else
            while (_isdigit(c)) x = x * 10 - (c & 15), c = gc();
    }
    template <class T, enable_if_t<_is_unsigned<T>, int> = 0>
#else
    template <class T, enable_if_t<_is_integer<T>, int> = 0>
#endif
    inline void read(T &x) {
        char c = gc();
        while (!_isdigit(c)) c = gc();
        x = 0;
        while (_isdigit(c)) x = x * 10 + (c & 15), c = gc();
    }
    inline void write(char c) { pc(c); }
    inline void write_cstr(const char *s) {
        while (*s) pc(*s++);
    }
    inline void write(const string &s) {
        for (char c : s) pc(c);
    }
#ifdef OUT_HAS_NEG
    template <class T, enable_if_t<_is_signed<T>, int> = 0>
    inline void write(T x) {
        char buffer[numeric_limits<T>::digits10 + 1];
        int digits = 0;
        if (x >= 0) do
            buffer[digits++] = (x % 10) | 48, x /= 10;
        while (x);
        else {
            pc(45);
            do buffer[digits++] = -(x % 10) | 48, x /= 10;
            while (x);
        }
        while (digits) pc(buffer[--digits]);
    }
    template <class T, enable_if_t<_is_unsigned<T>, int> = 0>
#else
    template <class T, enable_if_t<_is_integer<T>, int> = 0>
#endif
    inline void write(T x) {
        char buffer[numeric_limits<T>::digits10 + 1];
        int digits = 0;
        do buffer[digits++] = (x % 10) | 48, x /= 10;
        while (x);
        while (digits) pc(buffer[--digits]);
    }
    template <int N>
    struct _tuple_io_helper {
        template <class... T>
        static inline void _read(tuple<T...> &x) {
            _tuple_io_helper<N - 1>::_read(x), read(get<N - 1>(x));
        }
        template <class... T>
        static inline void _write(const tuple<T...> &x) {
            _tuple_io_helper<N - 1>::_write(x), pc(32), write(get<N - 1>(x));
        }
    };
    template <>
    struct _tuple_io_helper<1> {
        template <class... T>
        static inline void _read(tuple<T...> &x) {
            read(get<0>(x));
        }
        template <class... T>
        static inline void _write(const tuple<T...> &x) {
            write(get<0>(x));
        }
    };
    template <class... T>
    inline void read(tuple<T...> &x) {
        _tuple_io_helper<sizeof...(T)>::_read(x);
    }
    template <class... T>
    inline void write(const tuple<T...> &x) {
        _tuple_io_helper<sizeof...(T)>::_write(x);
    }
    template <class T1, class T2>
    inline void read(pair<T1, T2> &x) {
        read(x.first), read(x.second);
    }
    template <class T1, class T2>
    inline void write(const pair<T1, T2> &x) {
        write(x.first), pc(32), write(x.second);
    }
    template <class T1, class... T2>
    inline void read(T1 &x, T2 &...y) {
        read(x), read(y...);
    }
    template <class... T>
    inline void read_cstr(char *x, T *...y) {
        read_cstr(x), read_cstr(y...);
    }
    template <class T1, class... T2>
    inline void write(const T1 &x, const T2 &...y) {
        write(x), write(y...);
    }
    template <class... T>
    inline void write_cstr(const char *x, const T *...y) {
        write_cstr(x), write_cstr(y...);
    }
    template <class T>
    inline void print(const T &x) {
        write(x);
    }
    inline void print_cstr(const char *x) { write_cstr(x); }
    template <class T1, class... T2>
    inline void print(const T1 &x, const T2 &...y) {
        print(x), pc(32), print(y...);
    }
    template <class... T>
    inline void print_cstr(const char *x, const T *...y) {
        print_cstr(x), print_cstr(y...);
    }
    inline void println() {}
    inline void println_cstr() {}
    template <class... T>
    inline void println(const T &...x) {
        print(x...), pc(10);
    }
    template <class... T>
    inline void println_cstr(const T *...x) {
        print_cstr(x...), pc(10);
    }
}  // namespace FastIO
using namespace FastIO;
struct Edge{
    int l, r, id;
}q[M];
bool flag;
int n, m, i, l, r, cnt, idx, st, lim = 1;
int num[N], h[M];
uint x, aa, bb, cc;
uint a[N], b[N], c[N];
uint sum[N], tim[N]; // sum[i] = sum h[j], tim
uint s[N]; // ans[i, r]
uint p[N]; // sum sum[i][r]
uint ans[M];
bool End;
int main(){
//  open("orandgcd.in", "orandgcd.out");
    read(n), read(m);
    for(i = 1; i <= n; i = -~i)
        read(a[i]);
    for(i = 1; i <= n; i = -~i)
        read(b[i]);
    for(i = 1; i <= n; i = -~i)
        read(c[i]);
    for(i = 1; i <= m; i = -~i){
        read(q[i].l), read(q[i].r);
        q[i].id = i;
        ++num[q[i].r];
    }
    for(i = 1; i <= n; i = -~i)
        num[i] += num[i - 1];
    for(i = 1; i <= m; i = -~i)
        h[num[q[i].r]--] = i;
    for(r = 1; r <= n; r = -~r){
        tim[r] = r + 1;
        sum[r] = sum[r - 1] + a[r] * b[r] * c[r];
        st = r;
        while(--st){
            aa = a[st] & a[r], bb = b[st] | b[r], cc = __gcd(c[st], c[r]);
            if(aa == a[st] && bb == b[st] && cc == c[st])
                break;
            s[st + 1] -= (r - tim[st]) * sum[st];
            tim[st] = r;
            a[st] = aa, b[st] = bb, c[st] = cc;
        }
        for(i = st + 1; i <= r; i = -~i)
            sum[i] = sum[i - 1] + a[i] * b[i] * c[i];
        p[r] = p[r - 1] + sum[r];
        while(lim <= m){
            if(q[h[lim]].r != r)
                break;
            x = q[h[lim]].l;
            ans[q[h[lim]].id] = s[x] - (r - tim[x - 1] + 1) * sum[x - 1] + p[r] - p[x - 1];
            lim = -~lim;
        }
    }
    for(int i = 1; i <= m; i = -~i)
        write(ans[i], '\n');
    //cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB";
    return 0;
}