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
|
#include <bits/stdc++.h> using namespace std; #define int long long #define ull unsigned long long #define ALL(a) (a).begin(), (a).end() #define pb push_back #define mk make_pair #define pii pair<int, int> #define pis pair<int, string> #define sec second #define fir first #define sz(a) int((a).size()) #define rep(i, x, y) for (int i = x; i <= y; i++) #define repp(i, x, y) for (int i = x; i >= y; i--) #define Yes cout << "Yes" << endl #define YES cout << "YES" << endl #define No cout << "No" << endl #define NO cout << "NO" << endl #define debug(x) cerr << #x << ": " << x << endl #define double long double const int INF = 0x3f3f3f3f; const int MOD = 1000000007; const int N = 2e5 + 10;
#define ls p << 1 #define rs p << 1 | 1
int n, q; int a[N], b[N], c[N], d[N];
int gcd(int x, int y) { if (x == 0) return y; return gcd(y % x, x); }
int seg[N << 2];
void pushup(int p) { seg[p] = gcd(seg[ls], seg[rs]); }
void build(int p, int l, int r) { if (l == r) { seg[p] = c[l]; return; } int mid = l + r >> 1; build(ls, l, mid); build(rs, mid + 1, r); pushup(p); }
int query(int p, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return seg[p]; } int mid = l + r >> 1; int res = 0; if (mid >= ll) res = gcd(query(ls, l, mid, ll, rr), res); if (mid < rr) res = gcd(query(rs, mid + 1, r, ll, rr), res); return res; }
int seg2[N << 2];
void pushup2(int p) { seg2[p] = gcd(seg2[ls], seg2[rs]); }
void build2(int p, int l, int r) { if (l == r) { seg2[p] = d[l]; return; } int mid = l + r >> 1; build2(ls, l, mid); build2(rs, mid + 1, r); pushup2(p); }
int query2(int p, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return seg2[p]; } int mid = l + r >> 1; int res = 0; if (mid >= ll) res = gcd(query2(ls, l, mid, ll, rr), res); if (mid < rr) res = gcd(query2(rs, mid + 1, r, ll, rr), res); return res; }
signed main() { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; c[i] = abs(a[i] - a[i - 1]); } for (int i = 1; i <= n; i++) { cin >> b[i]; d[i] = abs(b[i] - b[i - 1]); } build(1, 1, n); build2(1, 1, n); while (q--) { int h1, h2, w1, w2; cin >> h1 >> h2 >> w1 >> w2; if (h1 == h2 && w1 == w2) cout << a[h1] + b[w1] << endl; else if (h1 == h2) cout << gcd(a[h1] + b[w1], query2(1, 1, n, w1 + 1, w2)) << endl; else if (w1 == w2) cout << gcd(a[h1] + b[w1], query(1, 1, n, h1 + 1, h2)) << endl; else { cout << gcd(gcd(a[h1] + b[w1], query(1, 1, n, h1 + 1, h2)), query2(1, 1, n, w1 + 1, w2)) << endl; } } return 0; }
|