Submission #1107596

# Submission time Handle Problem Language Result Execution time Memory
1107596 2024-11-01T16:31:37 Z Bananabread Harvest (JOI20_harvest) C++17
100 / 100
352 ms 118196 KB
#include<bits/stdc++.h>
#define ll long long
#define ntr "\n"
#define mod (ll)(1e9+7)
#define taskname "temp"
#define frep freopen(taskname".inp","r",stdin); freopen(taskname".out","w",stdout);
using namespace std;
ll a[200001],b[200001];
ll n,m,l,c,q;
vector<pair<ll,ll>> adj[400001],adjrev[400001];
ll vis[400001];
ll dist[200001];
ll distrev[400001];
ll pos[200001];
ll sz[400001];
ll euler[400001];
ll fenwick[200001];
ll line[200001];
ll ans[200001];
vector<array<ll,3>> sweep;
ll ones[200001];
ll tin;
ll ord;
ll cnt;
vector<vector<pair<ll,ll>>> compart;
vector<ll> st,ed;
vector<ll> len;
ll num[400001];
ll insub[400001];
bool inloop[400001];
ll root;
void update(ll x,ll val){
    for(;x<=n+m;x+=x&-x){
        insub[x]+=val;
    }
}
ll query(ll x){
    ll s=0;
    for(;x>0;x-=x&-x){
        s+=insub[x];
    }
    return s;
}
void upd(ll x,ll val,ll type){
    if(type){
        for(;x<=m;x+=x&-x){
            fenwick[x]+=val;
        }
    }
    else{
        for(;x<=m;x+=x&-x){
            ones[x]+=val;
        }
    }
}
ll get(ll x,ll type){
    ll s=0;
    for(;x;x-=x&-x){
        if(type) s+=fenwick[x];
        else s+=ones[x];
    }
    return s;
}
void dfs(ll u){
    if(root) return ;
    if(vis[u]){
        root=u;
        return ;
    }
    vis[u]=1;
    for(auto [v,w]:adj[u]){
        dfs(v);
    }
}
void dfs1(ll u=root,ll d=0,ll par=0){
    distrev[u]=d;
    vis[u]=1;
    sz[u]=1;
    euler[u]=++tin;
    num[u]=cnt;
    if(u>n){
        compart[cnt].push_back({d%len[cnt],u-n});
        line[u-n]=d/len[cnt];
    }
    for(auto [v,w]:adjrev[u]){
        if(v==root) continue;
        dfs1(v,d+w,u);
        sz[u]+=sz[v];
    }
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    //frep;
    cin>>n>>m>>l>>c;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) cin>>b[i];
    for(int i=1;i<=n;i++){
        ll t=((a[i]-c)%l+l)%l;
        ll pos=upper_bound(a+1,a+n+1,t)-a-1;
        if(pos==0) pos=n;
        ll v=((a[i]-a[pos])%l+l)%l;
        ll cost=(c-v+l-1)/l*l+v;
        adj[i].push_back({pos,cost});
        adjrev[pos].push_back({i,cost});
    }
    for(int i=1;i<=m;i++){
        ll v=upper_bound(a+1,a+n+1,b[i])-a-1;
        if(v==0) v=n;
        ll cost=((b[i]-a[v])%l+l)%l;
        adj[i+n].push_back({v,cost});
        adjrev[v].push_back({i+n,cost});
    }
    compart.push_back({});
    len.push_back(0);
    st.push_back(-1);
    ed.push_back(-1);
    for(int i=1;i<=m;i++){
        if(vis[i+n]) continue;
        root=0,dfs(i+n);
        cnt++;
        compart.push_back({});
        st.push_back(1e18);
        ed.push_back(0);
        ll p=root;
        dist[root]=0;
        while(true){
            inloop[p]=1;
            auto [v,w]=adj[p][0];
            if(v==root){
                len.push_back(dist[p]+w);
                break;
            }
            dist[v]=dist[p]+w;
            p=v;
        }
        dfs1();
        sort(compart[cnt].begin(),compart[cnt].end());
        for(auto i:compart[cnt]){
            pos[i.second]=++ord;
            st[cnt]=min(st[cnt],ord);
            ed[cnt]=max(ed[cnt],ord);
        }
    }
    for(int i=1;i<=m;i++){
        sweep.push_back({distrev[i+n],0,i});
    }
    cin>>q;
    for(int i=1;i<=q;i++){
        ll u,t;
        cin>>u>>t;
        sweep.push_back({t+distrev[u],u+1,i});
    }
    sort(sweep.begin(),sweep.end());
    for(auto i:sweep){
        if(i[1]==0){
            upd(pos[i[2]],line[i[2]],1);
            upd(pos[i[2]],1,0);
            update(euler[i[2]+n],1);
        }
        else{
            ll u=i[1]-1;
            ll t=i[0]-distrev[u];
            t-=dist[u];
            ll id=i[2];
            if(!inloop[u]){
                ans[id]=query(euler[u]+sz[u]-1)-query(euler[u]-1);
            }
            else{
                ans[id]+=(query(euler[u]+sz[u]-1)-query(euler[u]-1))*(dist[u]>0);
                ll p=t/len[num[u]];
                ll q=t%len[num[u]];
                ll lo=0,hi=compart[num[u]].size()-1,pos=-1;
                while(lo<=hi){
                    ll mid=(lo+hi)/2;
                    if(compart[num[u]][mid].first<=q) pos=mid,lo=mid+1;
                    else hi=mid-1;
                }
                ans[id]+=(get(ed[num[u]],0)-get(st[num[u]]-1,0))*p-(get(ed[num[u]],1)-get(st[num[u]]-1,1));
                if(pos>=0)
                    ans[id]+=get(st[num[u]]+pos,0)-get(st[num[u]]-1,0);
            }
        }
    }
    for(int i=1;i<=q;i++){
        cout<<ans[i]<<ntr;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 43600 KB Output is correct
2 Correct 10 ms 44568 KB Output is correct
3 Correct 9 ms 44352 KB Output is correct
4 Correct 9 ms 44368 KB Output is correct
5 Correct 11 ms 44608 KB Output is correct
6 Correct 10 ms 44776 KB Output is correct
7 Correct 11 ms 44640 KB Output is correct
8 Correct 9 ms 44096 KB Output is correct
9 Correct 10 ms 42320 KB Output is correct
10 Correct 10 ms 44364 KB Output is correct
11 Correct 8 ms 44096 KB Output is correct
12 Correct 9 ms 44608 KB Output is correct
13 Correct 10 ms 44608 KB Output is correct
14 Correct 10 ms 44584 KB Output is correct
15 Correct 10 ms 42560 KB Output is correct
16 Correct 11 ms 44352 KB Output is correct
17 Correct 9 ms 44352 KB Output is correct
18 Correct 9 ms 44364 KB Output is correct
19 Correct 10 ms 44352 KB Output is correct
20 Correct 10 ms 44352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 53440 KB Output is correct
2 Correct 135 ms 69928 KB Output is correct
3 Correct 119 ms 74044 KB Output is correct
4 Correct 128 ms 66272 KB Output is correct
5 Correct 145 ms 90588 KB Output is correct
6 Correct 137 ms 86748 KB Output is correct
7 Correct 108 ms 68080 KB Output is correct
8 Correct 112 ms 74144 KB Output is correct
9 Correct 185 ms 94944 KB Output is correct
10 Correct 138 ms 94968 KB Output is correct
11 Correct 206 ms 90332 KB Output is correct
12 Correct 254 ms 93916 KB Output is correct
13 Correct 247 ms 90480 KB Output is correct
14 Correct 184 ms 93916 KB Output is correct
15 Correct 196 ms 85444 KB Output is correct
16 Correct 149 ms 82908 KB Output is correct
17 Correct 145 ms 79132 KB Output is correct
18 Correct 97 ms 59176 KB Output is correct
19 Correct 95 ms 59164 KB Output is correct
20 Correct 134 ms 73180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 43600 KB Output is correct
2 Correct 10 ms 44568 KB Output is correct
3 Correct 9 ms 44352 KB Output is correct
4 Correct 9 ms 44368 KB Output is correct
5 Correct 11 ms 44608 KB Output is correct
6 Correct 10 ms 44776 KB Output is correct
7 Correct 11 ms 44640 KB Output is correct
8 Correct 9 ms 44096 KB Output is correct
9 Correct 10 ms 42320 KB Output is correct
10 Correct 10 ms 44364 KB Output is correct
11 Correct 8 ms 44096 KB Output is correct
12 Correct 9 ms 44608 KB Output is correct
13 Correct 10 ms 44608 KB Output is correct
14 Correct 10 ms 44584 KB Output is correct
15 Correct 10 ms 42560 KB Output is correct
16 Correct 11 ms 44352 KB Output is correct
17 Correct 9 ms 44352 KB Output is correct
18 Correct 9 ms 44364 KB Output is correct
19 Correct 10 ms 44352 KB Output is correct
20 Correct 10 ms 44352 KB Output is correct
21 Correct 94 ms 53440 KB Output is correct
22 Correct 135 ms 69928 KB Output is correct
23 Correct 119 ms 74044 KB Output is correct
24 Correct 128 ms 66272 KB Output is correct
25 Correct 145 ms 90588 KB Output is correct
26 Correct 137 ms 86748 KB Output is correct
27 Correct 108 ms 68080 KB Output is correct
28 Correct 112 ms 74144 KB Output is correct
29 Correct 185 ms 94944 KB Output is correct
30 Correct 138 ms 94968 KB Output is correct
31 Correct 206 ms 90332 KB Output is correct
32 Correct 254 ms 93916 KB Output is correct
33 Correct 247 ms 90480 KB Output is correct
34 Correct 184 ms 93916 KB Output is correct
35 Correct 196 ms 85444 KB Output is correct
36 Correct 149 ms 82908 KB Output is correct
37 Correct 145 ms 79132 KB Output is correct
38 Correct 97 ms 59176 KB Output is correct
39 Correct 95 ms 59164 KB Output is correct
40 Correct 134 ms 73180 KB Output is correct
41 Correct 245 ms 98704 KB Output is correct
42 Correct 262 ms 101240 KB Output is correct
43 Correct 116 ms 65500 KB Output is correct
44 Correct 194 ms 96812 KB Output is correct
45 Correct 231 ms 113072 KB Output is correct
46 Correct 246 ms 111020 KB Output is correct
47 Correct 244 ms 110956 KB Output is correct
48 Correct 216 ms 112560 KB Output is correct
49 Correct 222 ms 118196 KB Output is correct
50 Correct 189 ms 98552 KB Output is correct
51 Correct 218 ms 97836 KB Output is correct
52 Correct 252 ms 113840 KB Output is correct
53 Correct 301 ms 113848 KB Output is correct
54 Correct 260 ms 113800 KB Output is correct
55 Correct 352 ms 113840 KB Output is correct
56 Correct 244 ms 107180 KB Output is correct
57 Correct 256 ms 106612 KB Output is correct
58 Correct 243 ms 100520 KB Output is correct
59 Correct 220 ms 100020 KB Output is correct
60 Correct 197 ms 103564 KB Output is correct
61 Correct 219 ms 109168 KB Output is correct
62 Correct 257 ms 102924 KB Output is correct
63 Correct 148 ms 87344 KB Output is correct
64 Correct 155 ms 84272 KB Output is correct
65 Correct 150 ms 84272 KB Output is correct
66 Correct 138 ms 83120 KB Output is correct
67 Correct 141 ms 83024 KB Output is correct
68 Correct 139 ms 82988 KB Output is correct
69 Correct 237 ms 95404 KB Output is correct
70 Correct 222 ms 95152 KB Output is correct
71 Correct 229 ms 95160 KB Output is correct
72 Correct 235 ms 95412 KB Output is correct