# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
224193 |
2020-04-17T12:13:52 Z |
gs14004 |
Harvest (JOI20_harvest) |
C++17 |
|
1629 ms |
181196 KB |
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<lint, lint>;
const int MAXN = 400005;
struct b1{
lint tree[MAXN];
void add(int x, lint v){
for(int i=x+1; i<MAXN; i+=i&-i){
tree[i] += v;
}
}
lint query(int x){
lint ret = 0;
for(int i=x+1; i; i-=i&-i){
ret += tree[i];
}
return ret;
}
}b1, b2;
struct fuck{
vector<int> tree[MAXN];
vector<lint> elem[MAXN];
int SZ;
void prepare(vector<pi> v){
SZ = sz(v) + 2;
for(auto &[x, y] : v){
for(int i=x+1; i<SZ; i+=i&-i){
elem[i].push_back(y);
}
}
for(int i=0; i<SZ; i++){
sort(all(elem[i]));
elem[i].resize(unique(all(elem[i])) - elem[i].begin());
tree[i].resize(sz(elem[i]) + 1);
}
}
void add(int x, lint v, int arg){
for(int i=x+1; i<SZ; i+=i&-i){
auto lp = lower_bound(all(elem[i]), v) - elem[i].begin() + 1;
for(int j=lp; j<sz(tree[i]); j+=j&-j){
tree[i][j] += arg;
}
}
}
int query(int x, lint y){
int ret = 0;
for(int i=x+1; i; i-=i&-i){
int pos = upper_bound(all(elem[i]), y) - elem[i].begin();
for(int j=pos; j; j-=j&-j){
ret += tree[i][j];
}
}
return ret;
}
void clear(){
for(int i=0; i<SZ; i++){
tree[i].clear();
elem[i].clear();
}
}
}fuck;
int n, m, q, l, c;
vector<pi> v[MAXN];
vector<pi> gph[MAXN];
vector<lint> deliv[MAXN];
int nxt[MAXN]; lint len[MAXN];
lint cyclen[MAXN], ans[MAXN];
void solve(vector<int> vtx){
lint L = cyclen[vtx[0]];
vector<lint> sum(sz(vtx));
vector<lint> crd;
for(int i=sz(vtx)-1; i>=0; i--){
sum[i] = (i+1 < sz(vtx) ? sum[i+1] : 0) + len[vtx[i]];
for(auto &j : deliv[vtx[i]]){
crd.push_back(j + sum[i]);
crd.push_back(j + L + sum[i]);
}
}
sort(all(crd));
crd.resize(unique(all(crd)) - crd.begin());
vector<pi> preps;
for(int i=0; i<sz(crd); i++){
preps.emplace_back(i, L - crd[i] % L);
}
fuck.prepare(preps);
auto ADD = [&](lint x){
int pos = lower_bound(all(crd), x) - crd.begin();
b1.add(pos, +1);
b2.add(pos, -(x/L));
fuck.add(pos, L - x % L, +1);
};
auto REM = [&](lint x){
int pos = lower_bound(all(crd), x) - crd.begin();
b1.add(pos, -1);
b2.add(pos, +x/L);
fuck.add(pos, L - x % L, -1);
};
for(int i=sz(vtx)-1; i>=0; i--){
sum[i] = (i+1 < sz(vtx) ? sum[i+1] : 0) + len[vtx[i]];
for(auto &j : deliv[vtx[i]]){
ADD(j + L + sum[i]);
}
}
for(int i=0; i<sz(vtx); i++){
for(auto &j : deliv[vtx[i]]){
REM(j + L + sum[i]);
ADD(j + sum[i]);
}
for(auto &[d, idx] : v[vtx[i]]){
int j = lower_bound(all(crd), d + sum[i] + 1) - crd.begin();
int cnt = b1.query(j - 1);
ans[idx] += b2.query(j - 1);
ans[idx] -= fuck.query(j - 1, L - 1 - (d + sum[i]) % L);
ans[idx] += (1 + (d + sum[i]) / L) * cnt;
}
}
for(int i=sz(vtx)-1; i>=0; i--){
for(auto &j : deliv[vtx[i]]){
REM(j + sum[i]);
}
}
fuck.clear();
}
struct query{
int st, ed;
lint x; int idx;
};
lint dep[MAXN];
int din[MAXN], dout[MAXN], piv;
void dfs(int x, vector<pi> &pinst, vector<query> &qinst){
din[x] = ++piv;
for(auto &i : deliv[x]){
pinst.emplace_back(din[x], dep[x] + i);
}
for(auto &[w, v] : gph[x]){
dep[v] = dep[x] + w;
dfs(v, pinst, qinst);
}
dout[x] = piv;
for(auto &[thres, idx] : v[x]){
qinst.push_back({din[x], dout[x], thres + dep[x], (int)idx});
}
deliv[x].clear();
}
void solve(){
vector<int> indeg(n);
for(int i=0; i<n; i++){
gph[nxt[i]].emplace_back(len[i], i);
indeg[nxt[i]]++;
}
queue<int> que;
for(int i=0; i<n; i++){
if(!indeg[i]){
que.push(i);
}
}
while(sz(que)){
int i = que.front();
que.pop();
indeg[nxt[i]]--;
if(indeg[nxt[i]] == 0) que.push(nxt[i]);
}
for(int i=0; i<n; i++){
if(indeg[i] && !cyclen[i]){
lint tot = len[i];
for(int j=nxt[i]; j!=i; j=nxt[j]) tot += len[j];
for(int j=i; !cyclen[j]; j=nxt[j]){
cyclen[j] = tot;
}
}
}
for(int i=0; i<n; i++){
if(indeg[i]){
vector<pi> pinst;
vector<query> qinst;
for(auto &[w, v] : gph[i]){
if(indeg[v]) continue;
dep[v] = w;
dfs(v, pinst, qinst);
}
for(auto &j : pinst){
deliv[i].push_back(j.second);
}
{
vector<lint> crd;
for(auto &[pos, thres] : pinst) crd.push_back(thres);
sort(all(crd));
crd.resize(unique(all(crd)) - crd.begin());
for(auto &[pos, thres] : pinst){
thres = lower_bound(all(crd), thres) - crd.begin();
}
sort(all(pinst));
sort(all(qinst), [&](const query &a, const query &b){
return a.st < b.st;
});
int p = 0;
for(auto &i : qinst){
while(p < sz(pinst) && pinst[p].first < i.st){
b1.add(pinst[p++].second, +1);
}
int pos = upper_bound(all(crd), i.x) - crd.begin();
ans[i.idx] -= b1.query(pos - 1);
}
for(int i=0; i<p; i++) b1.add(pinst[i].second, -1);
p = 0;
sort(all(qinst), [&](const query &a, const query &b){
return a.ed < b.ed;
});
for(auto &i : qinst){
while(p < sz(pinst) && pinst[p].first <= i.ed){
b1.add(pinst[p++].second, +1);
}
int pos = upper_bound(all(crd), i.x) - crd.begin();
ans[i.idx] += b1.query(pos - 1);
}
for(int i=0; i<p; i++) b1.add(pinst[i].second, -1);
}
}
}
for(int i=0; i<n; i++){
if(!indeg[i]) continue;
vector<int> vtx;
for(int j=i; indeg[j]; j=nxt[j]){
vtx.push_back(j);
indeg[j] = 0;
}
solve(vtx);
}
}
int main(){
scanf("%d %d %d %d",&n,&m,&l,&c);
vector<int> a(n), b(m), idx(n);
for(int i=0; i<n; i++){
scanf("%d",&a[i]);
a[i] = (l - a[i]) % l;
idx[i] = i;
}
for(int i=0; i<m; i++){
scanf("%d",&b[i]);
b[i] = (l - b[i]) % l;
}
reverse(all(a));
reverse(all(idx));
reverse(all(b));
if(a.back() == 0){
rotate(a.begin(), a.end() - 1, a.end());
rotate(idx.begin(), idx.end() - 1, idx.end());
}
scanf("%d",&q);
for(int i=0; i<q; i++){
int x;
lint t;
scanf("%d %lld",&x,&t);
x = idx[x - 1];
v[x].emplace_back(t, i);
}
for(int i=0; i<n; i++){
int nxtpos = (c + a[i]) % l;
nxt[i] = lower_bound(all(a), nxtpos) - a.begin();
lint newpos = (c + a[i]) + (nxt[i] == n ? (a[0] + l) : a[nxt[i]]) - nxtpos;
len[i] = newpos - a[i];
if(nxt[i] == n) nxt[i] = 0;
}
for(int i=0; i<m; i++){
int nxt = lower_bound(all(a), b[i]) - a.begin();
if(nxt == n) nxt = 0;
int delay = (a[nxt] - b[i] + l) % l;
deliv[nxt].push_back(delay);
}
solve();
for(int i=0; i<q; i++) printf("%lld\n", ans[i]);
}
Compilation message
harvest.cpp: In function 'void solve()':
harvest.cpp:198:26: warning: unused variable 'pos' [-Wunused-variable]
for(auto &[pos, thres] : pinst) crd.push_back(thres);
^
harvest.cpp:201:26: warning: unused variable 'pos' [-Wunused-variable]
for(auto &[pos, thres] : pinst){
^
harvest.cpp: In function 'int main()':
harvest.cpp:244:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d",&n,&m,&l,&c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
harvest.cpp:247:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
harvest.cpp:252:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&b[i]);
~~~~~^~~~~~~~~~~~
harvest.cpp:262:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
harvest.cpp:266:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %lld",&x,&t);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
47608 KB |
Output is correct |
2 |
Correct |
38 ms |
47864 KB |
Output is correct |
3 |
Correct |
52 ms |
48888 KB |
Output is correct |
4 |
Correct |
50 ms |
48972 KB |
Output is correct |
5 |
Correct |
43 ms |
49308 KB |
Output is correct |
6 |
Correct |
43 ms |
49316 KB |
Output is correct |
7 |
Correct |
50 ms |
49316 KB |
Output is correct |
8 |
Correct |
43 ms |
48940 KB |
Output is correct |
9 |
Correct |
45 ms |
48988 KB |
Output is correct |
10 |
Correct |
43 ms |
48964 KB |
Output is correct |
11 |
Correct |
44 ms |
48888 KB |
Output is correct |
12 |
Correct |
47 ms |
48888 KB |
Output is correct |
13 |
Correct |
45 ms |
49024 KB |
Output is correct |
14 |
Correct |
42 ms |
48572 KB |
Output is correct |
15 |
Correct |
46 ms |
49064 KB |
Output is correct |
16 |
Correct |
45 ms |
49064 KB |
Output is correct |
17 |
Correct |
46 ms |
49064 KB |
Output is correct |
18 |
Correct |
44 ms |
49076 KB |
Output is correct |
19 |
Correct |
44 ms |
49080 KB |
Output is correct |
20 |
Correct |
48 ms |
49068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
58588 KB |
Output is correct |
2 |
Correct |
336 ms |
75580 KB |
Output is correct |
3 |
Correct |
294 ms |
68984 KB |
Output is correct |
4 |
Correct |
287 ms |
70752 KB |
Output is correct |
5 |
Correct |
301 ms |
97428 KB |
Output is correct |
6 |
Correct |
313 ms |
97428 KB |
Output is correct |
7 |
Correct |
273 ms |
75648 KB |
Output is correct |
8 |
Correct |
271 ms |
75612 KB |
Output is correct |
9 |
Correct |
338 ms |
73204 KB |
Output is correct |
10 |
Correct |
241 ms |
78432 KB |
Output is correct |
11 |
Correct |
365 ms |
78064 KB |
Output is correct |
12 |
Correct |
403 ms |
78192 KB |
Output is correct |
13 |
Correct |
425 ms |
78188 KB |
Output is correct |
14 |
Correct |
262 ms |
77536 KB |
Output is correct |
15 |
Correct |
361 ms |
76448 KB |
Output is correct |
16 |
Correct |
311 ms |
91260 KB |
Output is correct |
17 |
Correct |
357 ms |
91280 KB |
Output is correct |
18 |
Correct |
219 ms |
70252 KB |
Output is correct |
19 |
Correct |
222 ms |
70276 KB |
Output is correct |
20 |
Correct |
276 ms |
80656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
47608 KB |
Output is correct |
2 |
Correct |
38 ms |
47864 KB |
Output is correct |
3 |
Correct |
52 ms |
48888 KB |
Output is correct |
4 |
Correct |
50 ms |
48972 KB |
Output is correct |
5 |
Correct |
43 ms |
49308 KB |
Output is correct |
6 |
Correct |
43 ms |
49316 KB |
Output is correct |
7 |
Correct |
50 ms |
49316 KB |
Output is correct |
8 |
Correct |
43 ms |
48940 KB |
Output is correct |
9 |
Correct |
45 ms |
48988 KB |
Output is correct |
10 |
Correct |
43 ms |
48964 KB |
Output is correct |
11 |
Correct |
44 ms |
48888 KB |
Output is correct |
12 |
Correct |
47 ms |
48888 KB |
Output is correct |
13 |
Correct |
45 ms |
49024 KB |
Output is correct |
14 |
Correct |
42 ms |
48572 KB |
Output is correct |
15 |
Correct |
46 ms |
49064 KB |
Output is correct |
16 |
Correct |
45 ms |
49064 KB |
Output is correct |
17 |
Correct |
46 ms |
49064 KB |
Output is correct |
18 |
Correct |
44 ms |
49076 KB |
Output is correct |
19 |
Correct |
44 ms |
49080 KB |
Output is correct |
20 |
Correct |
48 ms |
49068 KB |
Output is correct |
21 |
Correct |
197 ms |
58588 KB |
Output is correct |
22 |
Correct |
336 ms |
75580 KB |
Output is correct |
23 |
Correct |
294 ms |
68984 KB |
Output is correct |
24 |
Correct |
287 ms |
70752 KB |
Output is correct |
25 |
Correct |
301 ms |
97428 KB |
Output is correct |
26 |
Correct |
313 ms |
97428 KB |
Output is correct |
27 |
Correct |
273 ms |
75648 KB |
Output is correct |
28 |
Correct |
271 ms |
75612 KB |
Output is correct |
29 |
Correct |
338 ms |
73204 KB |
Output is correct |
30 |
Correct |
241 ms |
78432 KB |
Output is correct |
31 |
Correct |
365 ms |
78064 KB |
Output is correct |
32 |
Correct |
403 ms |
78192 KB |
Output is correct |
33 |
Correct |
425 ms |
78188 KB |
Output is correct |
34 |
Correct |
262 ms |
77536 KB |
Output is correct |
35 |
Correct |
361 ms |
76448 KB |
Output is correct |
36 |
Correct |
311 ms |
91260 KB |
Output is correct |
37 |
Correct |
357 ms |
91280 KB |
Output is correct |
38 |
Correct |
219 ms |
70252 KB |
Output is correct |
39 |
Correct |
222 ms |
70276 KB |
Output is correct |
40 |
Correct |
276 ms |
80656 KB |
Output is correct |
41 |
Correct |
1582 ms |
162040 KB |
Output is correct |
42 |
Correct |
458 ms |
77304 KB |
Output is correct |
43 |
Correct |
242 ms |
72292 KB |
Output is correct |
44 |
Correct |
1322 ms |
152792 KB |
Output is correct |
45 |
Correct |
1291 ms |
177740 KB |
Output is correct |
46 |
Correct |
1313 ms |
178620 KB |
Output is correct |
47 |
Correct |
1318 ms |
179260 KB |
Output is correct |
48 |
Correct |
1349 ms |
181196 KB |
Output is correct |
49 |
Correct |
1367 ms |
180160 KB |
Output is correct |
50 |
Correct |
1262 ms |
159676 KB |
Output is correct |
51 |
Correct |
1358 ms |
158912 KB |
Output is correct |
52 |
Correct |
1542 ms |
160736 KB |
Output is correct |
53 |
Correct |
1388 ms |
160220 KB |
Output is correct |
54 |
Correct |
1456 ms |
160924 KB |
Output is correct |
55 |
Correct |
1457 ms |
160848 KB |
Output is correct |
56 |
Correct |
1367 ms |
167092 KB |
Output is correct |
57 |
Correct |
1415 ms |
168116 KB |
Output is correct |
58 |
Correct |
1388 ms |
168760 KB |
Output is correct |
59 |
Correct |
1391 ms |
170288 KB |
Output is correct |
60 |
Correct |
1369 ms |
170424 KB |
Output is correct |
61 |
Correct |
1375 ms |
170424 KB |
Output is correct |
62 |
Correct |
1265 ms |
127260 KB |
Output is correct |
63 |
Correct |
1221 ms |
144856 KB |
Output is correct |
64 |
Correct |
1231 ms |
145236 KB |
Output is correct |
65 |
Correct |
1238 ms |
145240 KB |
Output is correct |
66 |
Correct |
1272 ms |
149448 KB |
Output is correct |
67 |
Correct |
1297 ms |
149236 KB |
Output is correct |
68 |
Correct |
1263 ms |
148820 KB |
Output is correct |
69 |
Correct |
1504 ms |
164284 KB |
Output is correct |
70 |
Correct |
1478 ms |
160648 KB |
Output is correct |
71 |
Correct |
1602 ms |
161540 KB |
Output is correct |
72 |
Correct |
1629 ms |
161756 KB |
Output is correct |