显示代码纯文本
#include <iostream>
using namespace std;
const int N = 1e3 + 3;
typedef unsigned int ui;
ui a[N], b[N], c[N];
ui ta[N * 4], tb[N * 4], tc[N * 4];
ui gcd(ui x, ui y) {
if (y == 0) return x;
return gcd(y, x % y);
}
#define mid ((l + r) / 2)
#define lc (u * 2)
#define rc (u * 2 + 1)
void builda(int u, int l, int r) {
if (l == r) {
ta[u] = a[l];
return;
}
builda(lc, l, mid);
builda(rc, mid + 1, r);
ta[u] = ta[lc] & ta[rc];
}
void buildb(int u, int l, int r) {
if (l == r) {
tb[u] = b[l];
return;
}
buildb(lc, l, mid);
buildb(rc, mid + 1, r);
tb[u] = tb[lc] | tb[rc];
}
void buildc(int u, int l, int r) {
if (l == r) {
tc[u] = c[l];
return;
}
buildc(lc, l, mid);
buildc(rc, mid + 1, r);
tc[u] = gcd(tc[lc], tc[rc]);
}
ui aska(int u, int l, int r, int s, int t) {
if (s <= l && r <= t) {
return ta[u];
}
ui ans = (0x7fffffff * 2U + 1U);
if (s <= mid) ans &= aska(lc, l, mid, s, t);
if (mid + 1 <= t) ans &= aska(rc, mid + 1, r, s, t);
return ans;
}
ui askb(int u, int l, int r, int s, int t) {
if (s <= l && r <= t) {
return tb[u];
}
ui ans = 0;
if (s <= mid) ans |= askb(lc, l, mid, s, t);
if (mid + 1 <= t) ans |= askb(rc, mid + 1, r, s, t);
return ans;
}
ui askc(int u, int l, int r, int s, int t) {
if (s <= l && r <= t) {
return tc[u];
}
ui ans1 = 0, ans2 = 0;
if (s <= mid) ans1 = askc(lc, l, mid, s, t);
if (mid + 1 <= t) ans2 = askc(rc, mid + 1, r, s, t);
if (ans1 == 0) return ans2;
if (ans2 == 0) return ans1;
return gcd(ans1, ans2);
}
ui s[N][N];
// ui so(int x1, int y1, int x2, int y2) { return s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1]; }
int main() {
int n, m;
freopen("shijian.in", "r", stdin);
freopen("shijian.out", "w", stdout);
bool te = 1;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%u", &a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%u", &b[i]);
if (b[i] != 1) te = 0;
}
for (int i = 1; i <= n; i++) {
scanf("%u", &c[i]);
}
if (te) {
// to be continued.
}
builda(1, 1, n);
buildb(1, 1, n);
buildc(1, 1, n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
s[i][j] = aska(1, 1, n, i, j) * askb(1, 1, n, i, j) * askc(1, 1, n, i, j);
s[i][j] += s[i][j - 1];
}
}
while (m--) {
int l, r;
cin >> l >> r;
ui ans = 0;
for (int i = l; i <= r; i++) {
ans += s[i][r] - s[i][i - 1];
}
cout << ans << endl;
}
return 0;
}