Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 6,561 Bytes
f96d8dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Spider Spring
// Solution by Jacob Plachta

#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <sstream>
using namespace std;

#define LL long long
#define LD long double
#define PR pair<int,int>

#define Fox(i,n) for (i=0; i<n; i++)
#define Fox1(i,n) for (i=1; i<=n; i++)
#define FoxI(i,a,b) for (i=a; i<=b; i++)
#define FoxR(i,n) for (i=(n)-1; 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<typename T> T Abs(T x) { return(x < 0 ? -x : x); }
template<typename T> 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<LL,LL> // (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<int> 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);
}