// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define norm(x, m) ((((x) % (m)) + (m)) % (m))
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 4e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
struct fenwick{
ll fn[maxn], TOT; // over flow nakone!
vector<int> done;
void add(int pos, ll x){
TOT+= x;
for(pos+= 3; pos < maxn; pos+= (pos & -pos))
done.PB(pos), fn[pos]+= x;
}
ll ask(int pos){
ll ans = 0;
for(pos+= 3; pos > 0; pos-= (pos & -pos))
ans+= fn[pos];
return ans;
}
ll askG(int pos){
return TOT - ask(pos-1);
}
void restart(){
TOT = 0;
while(sz(done)){
fn[done.back()] = 0;
done.pop_back();
}
}
};// restart ham bayad dashte bashe
fenwick fen, fen2;
int a[maxn], b[maxn], f[maxn], fl[maxn], n;
int mark[maxn];
vector< pair<int, ll> > qur[maxn];
ll ans[maxn];
vector<int> v[maxn];
int ROOT;
ll dis[maxn];
vector<ll> arr;
void prep(int u){
if(u >= n)
arr.PB(dis[u]);
for(int y : v[u])
if(y != ROOT)
dis[y] = dis[u] + fl[y], prep(y);
}
void dfs(int u){
if(u >= n)
fen.add(LB(arr, dis[u]), 1);
for(auto x : qur[u])
ans[x.F]-= fen.ask(UB(arr, x.S + dis[u]) - 1);
for(int y : v[u])
if(y != ROOT)
dfs(y);
for(auto x : qur[u])
ans[x.F]+= fen.ask(UB(arr, x.S + dis[u]) - 1);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
int m, C, L;
cin >> n >> m >> L >> C;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < m; i++)
cin >> b[i];
{
int pt = 0;
while(pt < m && b[pt] < a[0])
f[n + pt] = n-1, fl[n + pt] = L-a[n-1]+b[pt], pt++;
int pt2 = 0;
while(pt < m){
while(pt2+1 < n && a[pt2 + 1] < b[pt])
pt2++;
f[n + pt] = pt2;
fl[n + pt] = b[pt] - a[pt2];
pt++;
}
}
{
for(int i = 0; i < n; i++){
int pos = (((a[i] - C) % L) + L) % L;
int id = upper_bound(a, a+n, pos) - a;
f[i] = (id + n - 1) % n;
fl[i] = C + ((((pos - a[f[i]]) % L) + L) % L);
}
}
int q;
cin >> q;
for(int i = 0; i < q; i++){
int u;
cin >> u;
ll T;
cin >> T;
--u;
qur[u].PB({i, T});
}
vector<int> roots;
for(int i = 0; i < n; i++){
int tmp = i, tmpb = i;
while(mark[tmp] == 0)
mark[tmp] = i+1, tmpb = tmp, tmp = f[tmp];
if(mark[tmp] == i+1){
roots.PB(tmpb);
}
}
for(int i = 0; i < n + m; i++){
v[f[i]].PB(i);
}
for(int u : roots){
ROOT = u;
fen.restart();
arr.clear();
prep(u);
sort(arr.begin(), arr.end());
vector< pair<ll, int> >arr2;
for(ll x : arr){
if(arr2.empty() || arr2.back().F != x)
arr2.PB({x, 1});
else
arr2[sz(arr2)-1].S++;
}
arr.clear();
for(auto x : arr2){
arr.PB(x.F);
}
dfs(u);
int tmp = u;
ll cyc = 0;
vector< pair<int, pair<ll, pair<int, ll> > > > tdo;
do{
cyc+= fl[tmp];
tmp = f[tmp];
for(auto x : qur[tmp]){
tdo.PB( { UB(arr, x.S - cyc), {cyc, x} } );
}
}while(tmp != u);
sort(tdo.begin(), tdo.end());
fen2.restart();
vector<ll> arr3;
for(ll x : arr){
arr3.PB(x % cyc);
}
sort(arr3.begin(), arr3.end());
arr3.resize( unique(arr3.begin(), arr3.end()) - arr3.begin() );
int nw = 0;
ll cnt = 0, cnt2 = 0;
for(auto it : tdo){
while(nw < it.F){
fen2.add( LB(arr3, arr[nw] % cyc), arr2[nw].S );
cnt+= arr2[nw].S;
cnt2+= 1ll * arr2[nw].S * (arr[nw] / cyc);
nw++;
}
auto [id, T] = it.S.S;
ll lz = it.S.F;
ans[id]+= (1 + T/cyc) * cnt;
ans[id]-= cnt2 + 1ll * cnt * (lz / cyc) + fen2.askG( LB(arr3, ((-lz) % cyc) + cyc) );
ll L = (T % cyc) + 1, R = cyc-1;
if(L <= R){
L = norm(L - lz, cyc), R = norm(R - lz, cyc);
if(L <= R)
ans[id]-= fen2.askG(LB(arr3, L)) - fen2.askG(UB(arr3, R));
else
ans[id]-= fen2.askG(LB(arr3, L)) + fen2.ask(UB(arr3, R) - 1);
}
}
/*
ll lz = 0;
do{
lz+= fl[tmp];
tmp = f[tmp];
for(auto x : qur[tmp]){
for(auto y : arr2){
if(x.S >= y.F + lz)
ans[x.F]+= 1ll * y.S * (((x.S - y.F - lz) / cyc) + 1);
}
}
}while(tmp != u);*/
}
for(int i = 0; i < q; i++){
cout << ans[i] << "\n";
}
return 0;
}
Compilation message
harvest.cpp: In function 'int main()':
harvest.cpp:185:11: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
auto [id, T] = it.S.S;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19712 KB |
Output is correct |
2 |
Correct |
18 ms |
19712 KB |
Output is correct |
3 |
Correct |
19 ms |
20476 KB |
Output is correct |
4 |
Correct |
18 ms |
20096 KB |
Output is correct |
5 |
Correct |
18 ms |
20480 KB |
Output is correct |
6 |
Correct |
18 ms |
20544 KB |
Output is correct |
7 |
Correct |
18 ms |
20480 KB |
Output is correct |
8 |
Correct |
17 ms |
20072 KB |
Output is correct |
9 |
Correct |
17 ms |
20096 KB |
Output is correct |
10 |
Correct |
18 ms |
20096 KB |
Output is correct |
11 |
Correct |
18 ms |
20224 KB |
Output is correct |
12 |
Correct |
19 ms |
20544 KB |
Output is correct |
13 |
Correct |
20 ms |
20928 KB |
Output is correct |
14 |
Correct |
19 ms |
20352 KB |
Output is correct |
15 |
Correct |
18 ms |
20352 KB |
Output is correct |
16 |
Correct |
18 ms |
20352 KB |
Output is correct |
17 |
Correct |
18 ms |
20352 KB |
Output is correct |
18 |
Correct |
18 ms |
20224 KB |
Output is correct |
19 |
Correct |
19 ms |
20352 KB |
Output is correct |
20 |
Correct |
18 ms |
20224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
40628 KB |
Output is correct |
2 |
Correct |
272 ms |
43280 KB |
Output is correct |
3 |
Correct |
277 ms |
43756 KB |
Output is correct |
4 |
Correct |
248 ms |
53732 KB |
Output is correct |
5 |
Correct |
279 ms |
70468 KB |
Output is correct |
6 |
Correct |
273 ms |
70516 KB |
Output is correct |
7 |
Correct |
233 ms |
39656 KB |
Output is correct |
8 |
Correct |
233 ms |
39916 KB |
Output is correct |
9 |
Correct |
397 ms |
77860 KB |
Output is correct |
10 |
Correct |
250 ms |
79448 KB |
Output is correct |
11 |
Correct |
532 ms |
77856 KB |
Output is correct |
12 |
Correct |
513 ms |
77860 KB |
Output is correct |
13 |
Correct |
513 ms |
77740 KB |
Output is correct |
14 |
Correct |
351 ms |
79332 KB |
Output is correct |
15 |
Correct |
359 ms |
66832 KB |
Output is correct |
16 |
Correct |
290 ms |
57844 KB |
Output is correct |
17 |
Correct |
268 ms |
58048 KB |
Output is correct |
18 |
Correct |
168 ms |
37520 KB |
Output is correct |
19 |
Correct |
172 ms |
37556 KB |
Output is correct |
20 |
Correct |
217 ms |
43176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19712 KB |
Output is correct |
2 |
Correct |
18 ms |
19712 KB |
Output is correct |
3 |
Correct |
19 ms |
20476 KB |
Output is correct |
4 |
Correct |
18 ms |
20096 KB |
Output is correct |
5 |
Correct |
18 ms |
20480 KB |
Output is correct |
6 |
Correct |
18 ms |
20544 KB |
Output is correct |
7 |
Correct |
18 ms |
20480 KB |
Output is correct |
8 |
Correct |
17 ms |
20072 KB |
Output is correct |
9 |
Correct |
17 ms |
20096 KB |
Output is correct |
10 |
Correct |
18 ms |
20096 KB |
Output is correct |
11 |
Correct |
18 ms |
20224 KB |
Output is correct |
12 |
Correct |
19 ms |
20544 KB |
Output is correct |
13 |
Correct |
20 ms |
20928 KB |
Output is correct |
14 |
Correct |
19 ms |
20352 KB |
Output is correct |
15 |
Correct |
18 ms |
20352 KB |
Output is correct |
16 |
Correct |
18 ms |
20352 KB |
Output is correct |
17 |
Correct |
18 ms |
20352 KB |
Output is correct |
18 |
Correct |
18 ms |
20224 KB |
Output is correct |
19 |
Correct |
19 ms |
20352 KB |
Output is correct |
20 |
Correct |
18 ms |
20224 KB |
Output is correct |
21 |
Correct |
177 ms |
40628 KB |
Output is correct |
22 |
Correct |
272 ms |
43280 KB |
Output is correct |
23 |
Correct |
277 ms |
43756 KB |
Output is correct |
24 |
Correct |
248 ms |
53732 KB |
Output is correct |
25 |
Correct |
279 ms |
70468 KB |
Output is correct |
26 |
Correct |
273 ms |
70516 KB |
Output is correct |
27 |
Correct |
233 ms |
39656 KB |
Output is correct |
28 |
Correct |
233 ms |
39916 KB |
Output is correct |
29 |
Correct |
397 ms |
77860 KB |
Output is correct |
30 |
Correct |
250 ms |
79448 KB |
Output is correct |
31 |
Correct |
532 ms |
77856 KB |
Output is correct |
32 |
Correct |
513 ms |
77860 KB |
Output is correct |
33 |
Correct |
513 ms |
77740 KB |
Output is correct |
34 |
Correct |
351 ms |
79332 KB |
Output is correct |
35 |
Correct |
359 ms |
66832 KB |
Output is correct |
36 |
Correct |
290 ms |
57844 KB |
Output is correct |
37 |
Correct |
268 ms |
58048 KB |
Output is correct |
38 |
Correct |
168 ms |
37520 KB |
Output is correct |
39 |
Correct |
172 ms |
37556 KB |
Output is correct |
40 |
Correct |
217 ms |
43176 KB |
Output is correct |
41 |
Correct |
408 ms |
64972 KB |
Output is correct |
42 |
Correct |
351 ms |
47212 KB |
Output is correct |
43 |
Correct |
215 ms |
52064 KB |
Output is correct |
44 |
Correct |
395 ms |
74968 KB |
Output is correct |
45 |
Correct |
374 ms |
90668 KB |
Output is correct |
46 |
Correct |
402 ms |
91440 KB |
Output is correct |
47 |
Correct |
411 ms |
92148 KB |
Output is correct |
48 |
Correct |
384 ms |
93296 KB |
Output is correct |
49 |
Correct |
397 ms |
93388 KB |
Output is correct |
50 |
Correct |
378 ms |
64716 KB |
Output is correct |
51 |
Correct |
368 ms |
79696 KB |
Output is correct |
52 |
Correct |
571 ms |
113344 KB |
Output is correct |
53 |
Correct |
525 ms |
113804 KB |
Output is correct |
54 |
Correct |
581 ms |
113568 KB |
Output is correct |
55 |
Correct |
704 ms |
99904 KB |
Output is correct |
56 |
Correct |
476 ms |
94076 KB |
Output is correct |
57 |
Correct |
411 ms |
79056 KB |
Output is correct |
58 |
Correct |
450 ms |
95700 KB |
Output is correct |
59 |
Correct |
383 ms |
80512 KB |
Output is correct |
60 |
Correct |
388 ms |
80656 KB |
Output is correct |
61 |
Correct |
410 ms |
80852 KB |
Output is correct |
62 |
Correct |
538 ms |
87748 KB |
Output is correct |
63 |
Correct |
330 ms |
74460 KB |
Output is correct |
64 |
Correct |
308 ms |
74580 KB |
Output is correct |
65 |
Correct |
309 ms |
74840 KB |
Output is correct |
66 |
Correct |
302 ms |
75280 KB |
Output is correct |
67 |
Correct |
271 ms |
59604 KB |
Output is correct |
68 |
Correct |
301 ms |
75228 KB |
Output is correct |
69 |
Correct |
397 ms |
83404 KB |
Output is correct |
70 |
Correct |
341 ms |
63948 KB |
Output is correct |
71 |
Correct |
386 ms |
81100 KB |
Output is correct |
72 |
Correct |
389 ms |
81104 KB |
Output is correct |