#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") constexpr Int INF = 1001001001001001001LL; int N, Q; vector A, B; vector P, K; vector X; int main() { for (; ~scanf("%d%d", &N, &Q); ) { A.resize(N); for (int i = 0; i < N; ++i) scanf("%lld", &A[i]); B.resize(N); for (int i = 0; i < N; ++i) scanf("%lld", &B[i]); P.resize(Q); K.resize(Q); X.resize(Q); for (int q = 0; q < Q; ++q) { scanf("%d%lld%d", &P[q], &X[q], &K[q]); P[q] -= 1; K[q] -= 2; } int segN; for (segN = 1; segN < Q; segN <<= 1) {} // (i, c): k -> c + B[k-i] vector>> icss(segN << 1); vector>> qxss(N); for (int q = 0; q < Q; ++q) { qxss[P[q]].emplace_back(q, X[q]); } for (int i = 0; i < N; ++i) { const auto &qxs = qxss[i]; const int len = qxs.size(); for (int j = 0; j <= len; ++j) { int a = (j - 1 >= 0) ? qxs[j - 1].first : 0; int b = (j < len) ? qxs[j].first : Q; const Int c = (j - 1 >= 0) ? qxs[j - 1].second : A[i]; // cerr<>= 1, b >>= 1) { if (a & 1) icss[a++].emplace_back(i, c); if (b & 1) icss[--b].emplace_back(i, c); } } } // TODO vector ans(Q, INF); for (int q = 0; q < Q; ++q) { for (int a = segN + q; a; a >>= 1) { for (const auto &ic : icss[a]) { const int j = K[q] - ic.first; if (0 <= j && j < N) { chmin(ans[q], ic.second + B[j]); } } } } for (int q = 0; q < Q; ++q) { printf("%lld\n", ans[q]); } } return 0; }