#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#ifdef DEBUG
#define display(x) cerr << #x << " = " << (x) << endl;
#define displaya(a, st, n)\
{cerr << #a << " = {";\
for(int qwq = (st); qwq <= (n); ++qwq) {\
if(qwq == (st)) cerr << ((a)[qwq]);\
else cerr << ", " << ((a)[qwq]);\
} cerr << "}" << endl;}
#define displayv(v) displaya(v, 0, (int)(v).size() - 1)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define display(x) ;
#define displaya(a, st, n) ;
#define displayv(v) ;
#define eprintf(...) if(0) fprintf(stderr, "...")
#endif
template<typename T> bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; }
template<typename T> bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; }
template<typename A, typename B>
ostream& operator << (ostream& out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
#ifndef LOCAL
char pool[1<<15|1],*it=pool+32768;
#define getchar() (it>=pool+32768?(pool[fread(pool,sizeof(char),\
1<<15,stdin)]=EOF,*((it=pool)++)):*(it++))
#endif
inline LL readint() {
LL a = 0; char c = getchar(), p = 0;
while(isspace(c)) c = getchar();
if(c == '-') p = 1, c = getchar();
while(isdigit(c)) a = a*10 + c - '0', c = getchar();
return p ? -a : a;
}
LL lowdiv(LL x, LL y) {
assert(y > 0);
return x / y + (x % y < 0 ? -1 : 0);
}
LL lowmod(LL x, LL y) {
return x - lowdiv(x, y) * y;
}
const int maxN = 200000 + 5;
int n, m;
LL L, C, a[maxN], b[maxN];
vector<int> h[maxN];
int to[maxN];
vector<int> G[maxN];
LL w[maxN], len[maxN];
bool vis[maxN];
vector<int> loop[maxN];
LL p[maxN];
int id[maxN], clus[maxN];
struct Node {
LL a, b;
int c;
LL coef;
int i;
Node() {}
Node(LL a, LL b, int c, LL coef, int i)
: a(a), b(b), c(c), coef(coef), i(i) {}
friend bool operator < (const Node &x, const Node &y) {
return x.a != y.a ? x.a < y.a
: x.b != y.b ? x.b < y.b
: x.c != y.c ? x.c < y.c
: x.i < y.i;
}
Node una() const {
Node res = *this;
res.a = 0;
return res;
}
};
vector<Node> sz, bigs, sh, tmp;
LL asz[maxN], abigs[maxN], ash[maxN];
LL D[maxN];
void add(int p, LL x) {
for(; p <= n; p += p & -p) D[p] += x;
}
LL sum(int p) {
LL r = 0;
for(; p > 0; p -= p & -p) r += D[p];
return r;
}
void dnc(vector<Node> &a, int L, int R, LL stat[]) { // tmp size
if(L >= R) return;
int M = (L + R) >> 1;
dnc(a, L, M, stat); dnc(a, M + 1, R, stat);
int k = L;
auto addL = [&](Node &p) {
if(p.i < 0) add(p.c, p.coef);
tmp[k++] = p;
};
auto addR = [&](Node &p) {
if(p.i > 0) stat[p.i] += sum(p.c) * p.coef;
tmp[k++] = p;
};
int i = L, j = M + 1;
while(i <= M && j <= R) {
if(a[i].una() < a[j].una()) addL(a[i++]);
else addR(a[j++]);
}
while(i <= M) addL(a[i++]);
while(j <= R) addR(a[j++]);
for(int t = L; t <= M; ++t) if(a[t].i < 0) add(a[t].c, -a[t].coef);
for(int t = L; t <= R; ++t) a[t] = tmp[t];
}
int dfs_clock = 0, pre[maxN], post[maxN], euler[maxN];
void dfs(int u) {
vis[u] = true;
euler[pre[u] = ++dfs_clock] = u;
// eprintf("euler[%d] = %d, p = %lld\n", dfs_clock, u, p[u]);
for(int t : h[u]) {
sz.emplace_back(0, p[u] + t, pre[u], 1, -u);
bigs.emplace_back(0, p[u] + t, pre[u], lowdiv(- p[u] - t, len[clus[u]]), -u);
sh.emplace_back(-lowmod(- p[u] - t, len[clus[u]]), p[u] + t, pre[u], 1, -u);
}
for(int v : G[u]) if(id[v] == -1) {
p[v] = p[u] + w[v];
clus[v] = clus[u];
dfs(v);
}
post[u] = dfs_clock;
}
void build(int u) {
while(!vis[u]) vis[u] = true, u = to[u];
vector<int> &loop = ::loop[u];
loop.push_back(u); p[u] = 0;
while(to[u] != loop[0]) {
p[to[u]] = p[u] - w[u];
u = to[u];
loop.push_back(u);
}
u = loop[0];
len[u] = 0;
for(int x : loop) len[u] += w[x];
for(int i = 0; i < (int)loop.size(); ++i) clus[loop[i]] = u, id[loop[i]] = i;
displayv(loop);
displaya(to, 1, n);
displaya(id, 1, n);
for(int x : loop) dfs(x);
}
int qu[maxN];
LL qt[maxN];
int main() {
n = readint(); m = readint(); L = readint(); C = readint();
for(int i = 1; i <= n; ++i) a[i] = readint();
for(int j = 1; j <= m; ++j) b[j] = readint();
for(int j = 1; j <= m; ++j) {
if(a[1] <= b[j]) {
int i = upper_bound(a + 1, a + n + 1, b[j]) - a;
--i;
h[i].push_back(b[j] - a[i]);
} else {
h[n].push_back(b[j] + L - a[n]);
}
}
for(int i = 1; i <= n; ++i) sort(h[i].begin(), h[i].end());
for(int i = 1; i <= n; ++i) {
int p = a[i] - C;
p = (p % L + L) % L;
if(a[1] <= p)
to[i] = upper_bound(a + 1, a + n + 1, p) - a - 1,
w[i] = C + p - a[to[i]];
else
to[i] = n,
w[i] = C + p + L - a[to[i]];
}
memset(vis, 0, sizeof(vis));
memset(clus, 0, sizeof(clus));
memset(id, -1, sizeof(id));
for(int i = 1; i <= n; ++i) G[to[i]].push_back(i);
for(int i = 1; i <= n; ++i) if(!vis[i]) build(i);
int q = readint();
for(int i = 1; i <= q; ++i) {
int u = readint(); qu[i] = u;
LL T = readint(); qt[i] = T;
if(id[u] == -1) {
sz.emplace_back(0, T + p[u], pre[u] - 1, -1, i);
sz.emplace_back(0, T + p[u], post[u], +1, i);
} else {
vector<int> &loop = ::loop[clus[u]];
int L = pre[loop[0]], M = post[u], R = post[loop.back()];
assert(L <= M && M <= R);
LL len = ::len[clus[u]];
sz.emplace_back(0, T + p[u], L - 1, -1, i);
sz.emplace_back(0, T + p[u], M, +1, i);
sz.emplace_back(0, T + p[u] - len, M, -(m + 1), i);
sz.emplace_back(0, T + p[u] - len, R, +(m + 1), i);
bigs.emplace_back(0, T + p[u], L - 1, -1, i);
bigs.emplace_back(0, T + p[u], M, +1, i);
bigs.emplace_back(0, T + p[u] - len, M, -1, i);
bigs.emplace_back(0, T + p[u] - len, R, +1, i);
LL t2 = lowmod(T + p[u], len);
sh.emplace_back(t2 - len, T + p[u], L - 1, -1, i);
sh.emplace_back(t2 - len, T + p[u], M, +1, i);
sh.emplace_back(t2 - 2 * len, T + p[u], L - 1, -1, i);
sh.emplace_back(t2 - 2 * len, T + p[u], M, +1, i);
sh.emplace_back(t2 - len, T + p[u] - len, M, -1, i);
sh.emplace_back(t2 - len, T + p[u] - len, R, +1, i);
sh.emplace_back(t2 - 2 * len, T + p[u] - len, M, -1, i);
sh.emplace_back(t2 - 2 * len, T + p[u] - len, R, +1, i);
}
}
memset(D, 0, sizeof(D));
sort(sz.begin(), sz.end());
sort(bigs.begin(), bigs.end());
sort(sh.begin(), sh.end());
tmp.resize(max({sz.size(), bigs.size(), sh.size()}));
dnc(sz, 0, (int)sz.size() - 1, asz);
dnc(bigs, 0, (int)bigs.size() - 1, abigs);
dnc(sh, 0, (int)sh.size() - 1, ash);
for(int i = 1; i <= q; ++i) {
int u = qu[i];
LL T = qt[i];
LL ans = 0;
if(id[u] == -1) {
ans = asz[i];
} else {
LL L = len[clus[u]];
LL lef = asz[i] % (m + 1), rig = asz[i] / (m + 1);
ans = (lef + rig) * lowdiv(T + p[u], L) + lef;
ans += abigs[i];
ans += ash[i];
// eprintf("len = %lld; %lld %lld %lld %lld\n", L, lef, rig, abigs[i], ash[i]);
}
printf("%lld\n", ans);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
21020 KB |
Output is correct |
2 |
Correct |
37 ms |
21820 KB |
Output is correct |
3 |
Correct |
31 ms |
21884 KB |
Output is correct |
4 |
Correct |
22 ms |
19416 KB |
Output is correct |
5 |
Correct |
22 ms |
19672 KB |
Output is correct |
6 |
Correct |
22 ms |
19672 KB |
Output is correct |
7 |
Correct |
23 ms |
19672 KB |
Output is correct |
8 |
Correct |
22 ms |
19292 KB |
Output is correct |
9 |
Correct |
23 ms |
19284 KB |
Output is correct |
10 |
Correct |
22 ms |
19292 KB |
Output is correct |
11 |
Correct |
23 ms |
19292 KB |
Output is correct |
12 |
Correct |
35 ms |
21768 KB |
Output is correct |
13 |
Correct |
36 ms |
21888 KB |
Output is correct |
14 |
Correct |
35 ms |
21892 KB |
Output is correct |
15 |
Correct |
23 ms |
19540 KB |
Output is correct |
16 |
Correct |
23 ms |
19544 KB |
Output is correct |
17 |
Correct |
23 ms |
19544 KB |
Output is correct |
18 |
Correct |
22 ms |
19544 KB |
Output is correct |
19 |
Correct |
22 ms |
19544 KB |
Output is correct |
20 |
Correct |
22 ms |
19544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1241 ms |
217856 KB |
Output is correct |
2 |
Correct |
334 ms |
73932 KB |
Output is correct |
3 |
Correct |
1892 ms |
236892 KB |
Output is correct |
4 |
Correct |
1408 ms |
239096 KB |
Output is correct |
5 |
Correct |
294 ms |
91676 KB |
Output is correct |
6 |
Correct |
302 ms |
91676 KB |
Output is correct |
7 |
Correct |
287 ms |
64316 KB |
Output is correct |
8 |
Correct |
283 ms |
64432 KB |
Output is correct |
9 |
Correct |
1649 ms |
232180 KB |
Output is correct |
10 |
Correct |
1394 ms |
232312 KB |
Output is correct |
11 |
Correct |
1768 ms |
231268 KB |
Output is correct |
12 |
Correct |
1760 ms |
231196 KB |
Output is correct |
13 |
Correct |
1753 ms |
231104 KB |
Output is correct |
14 |
Correct |
1473 ms |
230992 KB |
Output is correct |
15 |
Correct |
1685 ms |
232112 KB |
Output is correct |
16 |
Correct |
344 ms |
80804 KB |
Output is correct |
17 |
Correct |
298 ms |
80540 KB |
Output is correct |
18 |
Correct |
264 ms |
61224 KB |
Output is correct |
19 |
Correct |
247 ms |
61080 KB |
Output is correct |
20 |
Correct |
271 ms |
72272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
21020 KB |
Output is correct |
2 |
Correct |
37 ms |
21820 KB |
Output is correct |
3 |
Correct |
31 ms |
21884 KB |
Output is correct |
4 |
Correct |
22 ms |
19416 KB |
Output is correct |
5 |
Correct |
22 ms |
19672 KB |
Output is correct |
6 |
Correct |
22 ms |
19672 KB |
Output is correct |
7 |
Correct |
23 ms |
19672 KB |
Output is correct |
8 |
Correct |
22 ms |
19292 KB |
Output is correct |
9 |
Correct |
23 ms |
19284 KB |
Output is correct |
10 |
Correct |
22 ms |
19292 KB |
Output is correct |
11 |
Correct |
23 ms |
19292 KB |
Output is correct |
12 |
Correct |
35 ms |
21768 KB |
Output is correct |
13 |
Correct |
36 ms |
21888 KB |
Output is correct |
14 |
Correct |
35 ms |
21892 KB |
Output is correct |
15 |
Correct |
23 ms |
19540 KB |
Output is correct |
16 |
Correct |
23 ms |
19544 KB |
Output is correct |
17 |
Correct |
23 ms |
19544 KB |
Output is correct |
18 |
Correct |
22 ms |
19544 KB |
Output is correct |
19 |
Correct |
22 ms |
19544 KB |
Output is correct |
20 |
Correct |
22 ms |
19544 KB |
Output is correct |
21 |
Correct |
1241 ms |
217856 KB |
Output is correct |
22 |
Correct |
334 ms |
73932 KB |
Output is correct |
23 |
Correct |
1892 ms |
236892 KB |
Output is correct |
24 |
Correct |
1408 ms |
239096 KB |
Output is correct |
25 |
Correct |
294 ms |
91676 KB |
Output is correct |
26 |
Correct |
302 ms |
91676 KB |
Output is correct |
27 |
Correct |
287 ms |
64316 KB |
Output is correct |
28 |
Correct |
283 ms |
64432 KB |
Output is correct |
29 |
Correct |
1649 ms |
232180 KB |
Output is correct |
30 |
Correct |
1394 ms |
232312 KB |
Output is correct |
31 |
Correct |
1768 ms |
231268 KB |
Output is correct |
32 |
Correct |
1760 ms |
231196 KB |
Output is correct |
33 |
Correct |
1753 ms |
231104 KB |
Output is correct |
34 |
Correct |
1473 ms |
230992 KB |
Output is correct |
35 |
Correct |
1685 ms |
232112 KB |
Output is correct |
36 |
Correct |
344 ms |
80804 KB |
Output is correct |
37 |
Correct |
298 ms |
80540 KB |
Output is correct |
38 |
Correct |
264 ms |
61224 KB |
Output is correct |
39 |
Correct |
247 ms |
61080 KB |
Output is correct |
40 |
Correct |
271 ms |
72272 KB |
Output is correct |
41 |
Correct |
754 ms |
106472 KB |
Output is correct |
42 |
Correct |
2394 ms |
272572 KB |
Output is correct |
43 |
Correct |
1459 ms |
237616 KB |
Output is correct |
44 |
Correct |
1756 ms |
270944 KB |
Output is correct |
45 |
Correct |
729 ms |
127716 KB |
Output is correct |
46 |
Correct |
759 ms |
127796 KB |
Output is correct |
47 |
Correct |
710 ms |
128648 KB |
Output is correct |
48 |
Correct |
507 ms |
124904 KB |
Output is correct |
49 |
Correct |
521 ms |
124988 KB |
Output is correct |
50 |
Correct |
678 ms |
100276 KB |
Output is correct |
51 |
Correct |
764 ms |
100556 KB |
Output is correct |
52 |
Correct |
2270 ms |
267256 KB |
Output is correct |
53 |
Correct |
2074 ms |
268392 KB |
Output is correct |
54 |
Correct |
2269 ms |
267256 KB |
Output is correct |
55 |
Correct |
2212 ms |
266572 KB |
Output is correct |
56 |
Correct |
791 ms |
116836 KB |
Output is correct |
57 |
Correct |
804 ms |
117120 KB |
Output is correct |
58 |
Correct |
757 ms |
117732 KB |
Output is correct |
59 |
Correct |
545 ms |
113720 KB |
Output is correct |
60 |
Correct |
554 ms |
114152 KB |
Output is correct |
61 |
Correct |
597 ms |
114148 KB |
Output is correct |
62 |
Correct |
2224 ms |
268556 KB |
Output is correct |
63 |
Correct |
632 ms |
94824 KB |
Output is correct |
64 |
Correct |
626 ms |
94908 KB |
Output is correct |
65 |
Correct |
650 ms |
95336 KB |
Output is correct |
66 |
Correct |
571 ms |
94220 KB |
Output is correct |
67 |
Correct |
590 ms |
94192 KB |
Output is correct |
68 |
Correct |
576 ms |
94184 KB |
Output is correct |
69 |
Correct |
704 ms |
106732 KB |
Output is correct |
70 |
Correct |
730 ms |
107116 KB |
Output is correct |
71 |
Correct |
729 ms |
107368 KB |
Output is correct |
72 |
Correct |
719 ms |
106848 KB |
Output is correct |