# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1108372 |
2024-11-04T02:03:02 Z |
8pete8 |
Harvest (JOI20_harvest) |
C++17 |
|
536 ms |
175560 KB |
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=998244353,mxn=4e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m,q,c,l;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int pos[mxn+10],post[mxn+10],pref[mxn+10];
int caldist(int from,int go){
if(from>go)return from-go;
return l-go+from;
}
vector<pii>adj[mxn+10];
void addedge(int x,int y,int z){
adj[x].pb({y,z});
}
int on[mxn+10],vis[mxn+10],oncycle[mxn+10]; //on cycle
int dist[mxn+10],sz[mxn+10],tin[mxn+10],tout[mxn+10],who[mxn+10],where[mxn+10];
int compsz[mxn+10];
vector<int>cycle[mxn+10];
int T=n,cnt=0;
stack<int>st;
void getcycle(int cur){
if(vis[cur])return;
vis[cur]=on[cur]=1;
st.push(cur);
for(auto i:adj[cur]){
if(on[i.f]){
cnt++;
cycle[cnt].pb(i.f),oncycle[i.f]=cnt;
while(st.top()!=i.f)cycle[cnt].pb(st.top()),oncycle[st.top()]=cnt,st.pop();
reverse(all(cycle[cnt]));
compsz[cnt]=dist[cur]-dist[i.f]+i.s;
}
if(!vis[i.f]){
dist[i.f]=dist[cur]+i.s;
getcycle(i.f);
}
}
//999653525
//1000000000
on[cur]=0;
if(!st.empty())st.pop();
}
int curtime=0;
pii cant;
void getdist(int cur){
sz[cur]=1;
tin[cur]=++curtime;
who[tin[cur]]=cur;
for(auto i:adj[cur])if(cant.f!=cur||cant.s!=i.f){
dist[i.f]=dist[cur]+i.s;
getdist(i.f);
sz[cur]+=sz[i.f];
}
tout[cur]=curtime;
}
vector<pii>qry[mxn+10];
int ans[mxn+10];
struct fen{
int fwk[mxn+10],sz=0;
vector<pii>keep;
void update(int pos,int val){
pos++;
keep.pb({pos,val});
for(int i=pos;i<=sz;i+=(i&-i))fwk[i]+=val;
}
int qry(int pos){
int sum=0;
pos++;
pos=min(pos,sz);
for(int i=pos;i>0;i-=(i&-i))sum+=fwk[i];
return sum;
}
void re(){
for(auto i:keep)for(int j=i.f;j<=sz;j+=(j&-j))fwk[j]-=i.s;
keep.clear();
}
}t,t2;
/*
sack
*/
vector<int>have[mxn+10];
vector<int>comp;
vector<pii>extra[mxn+10];
int cc=0;
void sack(int cur,int keep){
//solving case where node is not on cycle
int hvy=0;
vis[cur]=1;
for(auto i:adj[cur])if(cur!=cant.f||i.f!=cant.s){
if(sz[i.f]>sz[hvy])hvy=i.f;
}
for(auto i:adj[cur])if((cur!=cant.f||i.f!=cant.s)&&i.f!=hvy)sack(i.f,0);
if(hvy)sack(hvy,1);
int x=0;
for(auto i:adj[cur])if(cur!=cant.f||i.f!=cant.s){
if(i.f!=hvy)for(int j=tin[i.f];j<=tout[i.f];j++)if(who[j]>n)t.update(where[who[j]],1);
if(have[i.f].size()>have[cur].size())swap(have[cur],have[i.f]);
for(auto j:have[i.f])have[cur].pb(j);
}
if(cur>n)t.update(where[cur],1),have[cur].pb(dist[cur]),sz[cur]=1;
for(auto i:qry[cur]){
auto it=upper_bound(all(comp),i.f+dist[cur])-comp.begin()-1;
if(it)ans[i.s]=t.qry(it);
}
if(!keep)t.re();
}
/*
*/
int32_t main(){
fastio
cin>>n>>m>>l>>c;
for(int i=1;i<=n;i++)cin>>pos[i];
for(int i=1;i<=m;i++)cin>>post[i];
int cur=1;
k=c/l;
c%=l;
for(int i=n+1;i<=2*n;i++)pos[i]=pos[i-n]+l;
for(int i=n+1;i<=2*n;i++){
while(cur<=i&&pos[i]-pos[cur]>=c)cur++;
addedge(cur-1-((cur-1>n)?n:0),i-n,pos[i]-pos[cur-1]+(k*l));
}
T=n;
for(int i=1;i<=n;i++)if(!vis[i])getcycle(i);
cur=1;
for(int i=1;i<=m;i++){
T++;
while(cur<=n&&post[i]>pos[cur])cur++;
pos[T]=post[i];
if(cur==1)addedge(n,T,caldist(pos[T],pos[n]));
else addedge(cur-1,T,caldist(pos[T],pos[cur-1]));
}
for(int i=1;i<=cnt;i++){
cant={cycle[i].back(),cycle[i][0]};
dist[cycle[i][0]]=0;
getdist(cycle[i][0]);
}
for(int i=1;i<=T;i++)comp.pb(dist[i]),vis[i]=0;
sort(all(comp));
comp.erase(unique(all(comp)),comp.end());
t.sz=comp.size();
for(int i=n+1;i<=T;i++)where[i]=lb(all(comp),dist[i])-comp.begin();
cin>>q;
for(int i=0;i<q;i++){
int v,t;cin>>v>>t;
qry[v].pb({t,i});
}
for(int i=1;i<=cnt;i++){
cc=i;
cant={cycle[i].back(),cycle[i][0]};
sack(cycle[i][0],0);
t.re();
}
vector<pair<pii,int>>qry2;
for(int i=1;i<=cnt;i++){
int head=cycle[i][0];
for(auto j:cycle[i])for(auto k:qry[j])qry2.pb({{k.f,j},k.s});
sort(all(qry2));
int sum=0;
comp.clear();
for(auto j:have[head])comp.pb(j%compsz[i]);
for(auto j:qry2)comp.pb(j.f.f%compsz[i]);
sort(all(have[head]));
cur=0;
/*
for(auto j:qry2){
comp.pb((j.f.f%compsz[i])-compsz[i]+dist[j.f.s]);
comp.pb((j.f.f%compsz[i])-compsz[i]+dist[j.f.s]+compsz[i]);
}*/
sort(all(comp));
comp.erase(unique(all(comp)),comp.end());
t.sz=comp.size();
for(auto j:qry2){
while(cur<have[head].size()&&j.f.f>=have[head][cur]){
t.update(lb(all(comp),have[head][cur]%compsz[i])-comp.begin(),1);
sum+=(have[head][cur])/compsz[i];
cur++;
}
ans[j.s]+=((j.f.f/compsz[i])*cur)-sum+t.qry(lb(all(comp),j.f.f%compsz[i])-comp.begin())-cur;
int val=(j.f.f%compsz[i])-compsz[i]+dist[j.f.s];
int pos=upper_bound(all(comp),val)-comp.begin()-1;
if(pos>=0)ans[j.s]+=t.qry(pos);
val+=compsz[i];
pos=upper_bound(all(comp),val)-comp.begin()-1;
if(pos>=0)ans[j.s]+=t.qry(pos)-t.qry(lb(all(comp),j.f.f%compsz[i])-comp.begin());
}
t.re();
qry2.clear();
}
for(int i=0;i<q;i++)cout<<ans[i]<<"\n";
}
/*
create weighted directed graph with cycle on top
a tree will attach to the first node that harvest it with cost of dist
tree will move along the graph. we just need to find how many times a tree passes a given node and time
for qry v,T;
if node v is not in the cycle we can just find the number of tree in the subtree where dist(tree,v)<=T
can use sack on this
else the node v is on the cycle on top
let sz=size of the cycle
we can compute the first node treei will reach in the cycle. let the node be sti
then the time left for tree to move is T-dist(sti,treei)-dist(sti,v) = xi
then we just need to find the sum of floor[xi/sz]?
floor[T-dist/sz]+1 counting the first visit
floor[t-dist/sz]= floor[T/sz]-floor[dist/sz] - (1 if T mod sz< dist mod sz)+1
=floor[T/sz]-floor[dist/sz] + (1 if T mod sz>=dist mod sz)
we can count number of dist mod sz <= T mod sz seperately
1030156923594683
5150784617973418
33230868503053
1 1 2 559424926
0
1
1
1 759338505386878650
*/
Compilation message
harvest.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
harvest.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
38 | void setIO(string name){
| ^
harvest.cpp:44:28: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
44 | int caldist(int from,int go){
| ^
harvest.cpp:49:31: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
49 | void addedge(int x,int y,int z){
| ^
harvest.cpp:58:22: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
58 | void getcycle(int cur){
| ^
harvest.cpp:83:21: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
83 | void getdist(int cur){
| ^
harvest.cpp:99:32: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
99 | void update(int pos,int val){
| ^
harvest.cpp:104:20: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
104 | int qry(int pos){
| ^
harvest.cpp:111:13: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
111 | void re(){
| ^
harvest.cpp:123:27: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
123 | void sack(int cur,int keep){
| ^
harvest.cpp: In function 'void sack(long long int, long long int)':
harvest.cpp:132:9: warning: unused variable 'x' [-Wunused-variable]
132 | int x=0;
| ^
harvest.cpp: At global scope:
harvest.cpp:147:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
147 | int32_t main(){
| ^
harvest.cpp: In function 'int32_t main()':
harvest.cpp:211:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
211 | while(cur<have[head].size()&&j.f.f>=have[head][cur]){
| ~~~^~~~~~~~~~~~~~~~~~
harvest.cpp: In function 'void setIO(std::string)':
harvest.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harvest.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
74660 KB |
Output is correct |
2 |
Correct |
14 ms |
75088 KB |
Output is correct |
3 |
Correct |
15 ms |
75344 KB |
Output is correct |
4 |
Correct |
14 ms |
74964 KB |
Output is correct |
5 |
Correct |
15 ms |
75476 KB |
Output is correct |
6 |
Correct |
14 ms |
75648 KB |
Output is correct |
7 |
Correct |
14 ms |
75344 KB |
Output is correct |
8 |
Correct |
14 ms |
75088 KB |
Output is correct |
9 |
Correct |
19 ms |
74856 KB |
Output is correct |
10 |
Correct |
15 ms |
75088 KB |
Output is correct |
11 |
Correct |
15 ms |
75088 KB |
Output is correct |
12 |
Correct |
17 ms |
75772 KB |
Output is correct |
13 |
Correct |
18 ms |
75856 KB |
Output is correct |
14 |
Correct |
17 ms |
75344 KB |
Output is correct |
15 |
Correct |
16 ms |
75344 KB |
Output is correct |
16 |
Correct |
16 ms |
75344 KB |
Output is correct |
17 |
Correct |
15 ms |
75244 KB |
Output is correct |
18 |
Correct |
15 ms |
75344 KB |
Output is correct |
19 |
Correct |
15 ms |
75344 KB |
Output is correct |
20 |
Correct |
15 ms |
75600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
94148 KB |
Output is correct |
2 |
Correct |
164 ms |
101272 KB |
Output is correct |
3 |
Correct |
152 ms |
116156 KB |
Output is correct |
4 |
Correct |
222 ms |
128580 KB |
Output is correct |
5 |
Correct |
179 ms |
141240 KB |
Output is correct |
6 |
Correct |
198 ms |
141276 KB |
Output is correct |
7 |
Correct |
188 ms |
105656 KB |
Output is correct |
8 |
Correct |
121 ms |
103096 KB |
Output is correct |
9 |
Correct |
305 ms |
152500 KB |
Output is correct |
10 |
Correct |
284 ms |
153396 KB |
Output is correct |
11 |
Correct |
356 ms |
151472 KB |
Output is correct |
12 |
Correct |
413 ms |
151360 KB |
Output is correct |
13 |
Correct |
380 ms |
151472 KB |
Output is correct |
14 |
Correct |
284 ms |
152236 KB |
Output is correct |
15 |
Correct |
253 ms |
130356 KB |
Output is correct |
16 |
Correct |
166 ms |
123196 KB |
Output is correct |
17 |
Correct |
150 ms |
120840 KB |
Output is correct |
18 |
Correct |
89 ms |
99884 KB |
Output is correct |
19 |
Correct |
97 ms |
99972 KB |
Output is correct |
20 |
Correct |
163 ms |
108216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
74660 KB |
Output is correct |
2 |
Correct |
14 ms |
75088 KB |
Output is correct |
3 |
Correct |
15 ms |
75344 KB |
Output is correct |
4 |
Correct |
14 ms |
74964 KB |
Output is correct |
5 |
Correct |
15 ms |
75476 KB |
Output is correct |
6 |
Correct |
14 ms |
75648 KB |
Output is correct |
7 |
Correct |
14 ms |
75344 KB |
Output is correct |
8 |
Correct |
14 ms |
75088 KB |
Output is correct |
9 |
Correct |
19 ms |
74856 KB |
Output is correct |
10 |
Correct |
15 ms |
75088 KB |
Output is correct |
11 |
Correct |
15 ms |
75088 KB |
Output is correct |
12 |
Correct |
17 ms |
75772 KB |
Output is correct |
13 |
Correct |
18 ms |
75856 KB |
Output is correct |
14 |
Correct |
17 ms |
75344 KB |
Output is correct |
15 |
Correct |
16 ms |
75344 KB |
Output is correct |
16 |
Correct |
16 ms |
75344 KB |
Output is correct |
17 |
Correct |
15 ms |
75244 KB |
Output is correct |
18 |
Correct |
15 ms |
75344 KB |
Output is correct |
19 |
Correct |
15 ms |
75344 KB |
Output is correct |
20 |
Correct |
15 ms |
75600 KB |
Output is correct |
21 |
Correct |
172 ms |
94148 KB |
Output is correct |
22 |
Correct |
164 ms |
101272 KB |
Output is correct |
23 |
Correct |
152 ms |
116156 KB |
Output is correct |
24 |
Correct |
222 ms |
128580 KB |
Output is correct |
25 |
Correct |
179 ms |
141240 KB |
Output is correct |
26 |
Correct |
198 ms |
141276 KB |
Output is correct |
27 |
Correct |
188 ms |
105656 KB |
Output is correct |
28 |
Correct |
121 ms |
103096 KB |
Output is correct |
29 |
Correct |
305 ms |
152500 KB |
Output is correct |
30 |
Correct |
284 ms |
153396 KB |
Output is correct |
31 |
Correct |
356 ms |
151472 KB |
Output is correct |
32 |
Correct |
413 ms |
151360 KB |
Output is correct |
33 |
Correct |
380 ms |
151472 KB |
Output is correct |
34 |
Correct |
284 ms |
152236 KB |
Output is correct |
35 |
Correct |
253 ms |
130356 KB |
Output is correct |
36 |
Correct |
166 ms |
123196 KB |
Output is correct |
37 |
Correct |
150 ms |
120840 KB |
Output is correct |
38 |
Correct |
89 ms |
99884 KB |
Output is correct |
39 |
Correct |
97 ms |
99972 KB |
Output is correct |
40 |
Correct |
163 ms |
108216 KB |
Output is correct |
41 |
Correct |
359 ms |
136632 KB |
Output is correct |
42 |
Correct |
211 ms |
130480 KB |
Output is correct |
43 |
Correct |
158 ms |
125252 KB |
Output is correct |
44 |
Correct |
300 ms |
148904 KB |
Output is correct |
45 |
Correct |
292 ms |
163760 KB |
Output is correct |
46 |
Correct |
306 ms |
163000 KB |
Output is correct |
47 |
Correct |
305 ms |
162572 KB |
Output is correct |
48 |
Correct |
278 ms |
164788 KB |
Output is correct |
49 |
Correct |
253 ms |
165860 KB |
Output is correct |
50 |
Correct |
255 ms |
132404 KB |
Output is correct |
51 |
Correct |
252 ms |
131632 KB |
Output is correct |
52 |
Correct |
436 ms |
173624 KB |
Output is correct |
53 |
Correct |
445 ms |
175560 KB |
Output is correct |
54 |
Correct |
466 ms |
173708 KB |
Output is correct |
55 |
Correct |
536 ms |
173212 KB |
Output is correct |
56 |
Correct |
333 ms |
149184 KB |
Output is correct |
57 |
Correct |
316 ms |
146360 KB |
Output is correct |
58 |
Correct |
370 ms |
151368 KB |
Output is correct |
59 |
Correct |
253 ms |
149672 KB |
Output is correct |
60 |
Correct |
251 ms |
150192 KB |
Output is correct |
61 |
Correct |
260 ms |
150848 KB |
Output is correct |
62 |
Correct |
449 ms |
152916 KB |
Output is correct |
63 |
Correct |
193 ms |
123356 KB |
Output is correct |
64 |
Correct |
189 ms |
123472 KB |
Output is correct |
65 |
Correct |
190 ms |
125092 KB |
Output is correct |
66 |
Correct |
189 ms |
122944 KB |
Output is correct |
67 |
Correct |
163 ms |
124096 KB |
Output is correct |
68 |
Correct |
192 ms |
122556 KB |
Output is correct |
69 |
Correct |
336 ms |
138676 KB |
Output is correct |
70 |
Correct |
284 ms |
134964 KB |
Output is correct |
71 |
Correct |
311 ms |
135852 KB |
Output is correct |
72 |
Correct |
304 ms |
135496 KB |
Output is correct |