// Spider Spring // Solution by Jacob Plachta #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; #define LL long long #define LD long double #define PR pair #define Fox(i,n) for (i=0; i=0; i--) #define FoxR1(i,n) for (i=n; i>0; i--) #define FoxRI(i,a,b) for (i=b; i>=a; i--) #define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++) #define Min(a,b) a=min(a,b) #define Max(a,b) a=max(a,b) #define Sz(s) int((s).size()) #define All(s) (s).begin(),(s).end() #define Fill(s,v) memset(s,v,sizeof(s)) #define pb push_back #define mp make_pair #define x first #define y second template T Abs(T x) { return(x < 0 ? -x : x); } template T Sqr(T x) { return(x * x); } string plural(string s) { return(Sz(s) && s[Sz(s) - 1] == 'x' ? s + "en" : s + "s"); } const int INF = (int)1e9; const LD EPS = 1e-12; const LD PI = acos(-1.0); #define GETCHAR getchar_unlocked bool Read(int& x) { char c, r = 0, n = 0; x = 0; for (;;) { c = GETCHAR(); if ((c < 0) && (!r)) return(0); if ((c == '-') && (!r)) n = 1; else if ((c >= '0') && (c <= '9')) x = x * 10 + c - '0', r = 1; else if (r) break; } if (n) x = -x; return(1); } void ReadSeq(int* V, int N, int K) { int i, A, B, C, D; Fox(i, K) Read(V[i]); Read(A), Read(B), Read(C), Read(D); FoxI(i, K, N - 1) V[i] = ((LL)A * V[i - 2] + (LL)B * V[i - 1] + C) % D + 1; } #define MOD 1000000007 #define LIM 1000001 #define TVAL LL #define TLAZY pair // (set, increase) #define TLIM 2100000 TVAL ZERO_VAL = 0; TLAZY ZERO_LAZY = mp(0, 0); struct SegTree { void UpdateValForUpdateOrLazy(TVAL& a, TLAZY v) { if (v.x) a = v.x; if (v.y) a += v.y; } void UpdateLazyForUpdateOrLazy(TLAZY& a, TLAZY v) { if (v.x) a.x = v.x; if (v.y) a.y += v.y; } TVAL CombVals(TVAL v1, TVAL v2) { return(v1 + v2); } int N, sz; TVAL tree[TLIM]; TLAZY lazy[TLIM]; SegTree() {} SegTree(int _N) { Init(_N); } void Init(int _N) { N = _N; for (sz = 1; sz < N; sz <<= 1); Clear(); } void Clear() { int i; Fox(i, sz << 1) tree[i] = ZERO_VAL; Fox(i, sz << 1) lazy[i] = ZERO_LAZY; } void Prop(int i) { TLAZY v = lazy[i]; lazy[i] = ZERO_LAZY; UpdateValForUpdateOrLazy(tree[i], v); if (i < sz) { int c1 = i << 1, c2 = c1 + 1; UpdateLazyForUpdateOrLazy(lazy[c1], v); UpdateLazyForUpdateOrLazy(lazy[c2], v); } } void Comp(int i) { int c1 = i << 1, c2 = c1 + 1; tree[i] = CombVals(tree[c1], tree[c2]); } TVAL Query( int a, int b, int i = 1, int r1 = 0, int r2 = -1 ) { if (r2 < 0) r2 = sz - 1; Prop(i); if (a <= r1 && r2 <= b) return(tree[i]); int m = (r1 + r2) >> 1, c = i << 1; TVAL ret = ZERO_VAL; if (a <= m) ret = CombVals(ret, Query(a, b, c, r1, m)); if (b > m) ret = CombVals(ret, Query(a, b, c + 1, m + 1, r2)); return(ret); } void Update( int a, int b, TLAZY v, int i = 1, int r1 = 0, int r2 = -1 ) { if (r2 < 0) r2 = sz - 1; Prop(i); if (a <= r1 && r2 <= b) { UpdateLazyForUpdateOrLazy(lazy[i], v); Prop(i); return; } int m = (r1 + r2) >> 1, c = i << 1; if (a <= m) Update(a, b, v, c, r1, m); if (b > m) Update(a, b, v, c + 1, m + 1, r2); Prop(c), Prop(c + 1), Comp(i); } void UpdateOne(int i, TLAZY v) { i += sz; UpdateValForUpdateOrLazy(tree[i], v); while (i > 1) { i >>= 1; Comp(i); } } }; int N, M, K; int H[LIM], X[LIM], Y[LIM], Z[LIM], W[LIM]; set LS; // 0: height of column i // 1: sum of A endpoint weights with endpoint value i // 2: sum of A endpoint weighted values with endpoint value i // 3: sum of B endpoint weights with endpoint value i // 4: sum of B endpoint weighted values with endpoint value i SegTree ST[5]; void UpdateHeights(int a, int b, int h) { ST[0].Update(a, b, mp(h, 0)); } void UpdateLineSegment(int i, int d) // d = 1 (add) / -1 (remove) { if (i < 0 || i + 1 >= N) return; int a = ST[0].Query(i, i), b = ST[0].Query(i + 1, i + 1); if (a > b) swap(a, b); LL w = (LL)(i + 1) * (N - i - 1); ST[1].UpdateOne(a, mp(0, w % MOD * d)); ST[2].UpdateOne(a, mp(0, a * w % MOD * d)); ST[3].UpdateOne(b, mp(0, w % MOD * d)); ST[4].UpdateOne(b, mp(0, b * w % MOD * d)); if (d > 0) LS.insert(i); else LS.erase(i); } int ProcessCase() { int i; // input Read(N), Read(M), Read(K); ReadSeq(H, N, K); ReadSeq(X, M, K); ReadSeq(Y, M, K); ReadSeq(Z, M, K); ReadSeq(W, M, K); // init segment trees, line segment set, and horizontal sum Fox(i, 5) ST[i].Init(!i ? N : LIM); LS.clear(); LL horiz = 0; Fox(i, N) { UpdateHeights(i, i, H[i]); UpdateLineSegment(i - 1, 1); horiz = (horiz + (LL)i * (N - i)) % MOD; } // process events int ans = 1; Fox(i, M) { // update int x = X[i] - 1; int y = min(x + Y[i], N) - 1; int z = Z[i]; auto I = LS.lower_bound(x - 1); while (I != LS.end() && *I <= y) { int k = *I; I++; UpdateLineSegment(k, -1); } UpdateHeights(x, y, z); UpdateLineSegment(x - 1, 1); UpdateLineSegment(y, 1); // query int w = W[i]; LL d1 = ST[4].Query(w + 1, LIM - 1) - ST[2].Query(w + 1, LIM - 1); LL d2 = ST[3].Query(w + 1, LIM - 1) - ST[1].Query(w + 1, LIM - 1); LL vert = ((d1 - d2 % MOD * w) % MOD + MOD) % MOD; ans = (LL)ans * (horiz + vert) * 2 % MOD; } return(ans); } int main() { int T, t; Read(T); Fox1(t, T) printf("Case #%d: %d\n", t, ProcessCase()); return(0); }