Submission #1006250

# Submission time Handle Problem Language Result Execution time Memory
1006250 2024-06-23T15:25:03 Z Unforgettablepl Harvest (JOI20_harvest) C++17
5 / 100
5000 ms 96592 KB
#include <bits/stdc++.h>
using namespace std;


#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<pair<long long,long long>,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> orderedset;

#define int long long

const int INF = 1e18;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,m,l,c;
    cin >> n >> m >> l >> c;
    vector<int> employee(n+1);
    vector<int> apple(m);
    vector<pair<int,int>> adj(n+1);
    vector<vector<int>> revadj(n+1);
    for(int i=1;i<=n;i++)cin>>employee[i];
    for(int&i:apple)cin>>i;
    for(int i=1;i<=n;i++){
        int modded = c%l;
        int iter = 0;
        for(int jump=131072;jump;jump/=2){
            if(iter+jump>n)continue;
            if((l+employee[i]-employee[(n-iter-jump+i)%n +1])%l < modded)iter+=jump;
        }
        iter++;
        if(iter==n+1){
            adj[i] = {i,(c/l)*l + l};
            continue;
        }
        adj[i] = {(n-iter+i)%n +1,(c/l)*l + (l+employee[i]-employee[(n-iter+i)%n +1])%l};
        revadj[adj[i].first].emplace_back(i);
    }
    vector<int> depth(n+1);
    vector<bool> visited(n+1);
    vector<int> heads;
    function<void (int)> front_dfs = [&](int x){
        if(visited[x])return;
        visited[x]=true;
        front_dfs(adj[x].first);
        if(depth[adj[x].first]==0)heads.emplace_back(x);
        depth[x] = depth[adj[x].first]+adj[x].second;
    };
    for(int i=1;i<=n;i++)if(!visited[i])front_dfs(i);
    vector<vector<int>> apples(n+1);
    vector<vector<pair<int,int>>> queries(2e5+10);
    for(int&i:apple){
        int start = -1;
        if(i<employee[1])start = n;
        else {
            start = upper_bound(employee.begin()+1,employee.end(),i)-employee.begin()-1;
        }
        apples[start].emplace_back((l+i-employee[start])%l);
    }
    int q;
    cin >> q;
    vector<int> ans(q+1);
    for(int i=1;i<=q;i++){
        int v,t;
        cin >> v >> t;
        queries[v].emplace_back(t,i);
    }
    visited = vector<bool>(n+1);
    int uid = 0;
    function<orderedset(int)> calc_linear_dfs = [&](int x){
        orderedset curr;
        if(visited[x])return curr;
        visited[x] = true;
        for(int&i:revadj[x]){
            auto t = calc_linear_dfs(i);
            if(curr.size()<t.size())swap(curr,t);
            for(auto j:t)curr.insert(j);
        }
        for(int&i:apples[x])curr.insert({i+depth[x],++uid});
        for(auto[t,idx]:queries[x]){
            ans[idx] = curr.order_of_key({t+depth[x],INF});
        }
        return curr;
    };
    for(int&i:heads){
        auto t = calc_linear_dfs(i);
        orderedset curr;
        int cycle_len = depth[adj[i].first];
        int offset = 0;
        for(auto [x,y]:t)curr.insert({(x-adj[i].second)%cycle_len,y});
        for(auto [x,y]:t)offset-=(x-adj[i].second)/cycle_len;
        function<void(int,int)> calc = [&](int x,int len){
            for(auto[t,idx]:queries[x]){
                t-=len;
                if(t<0)continue;
                ans[idx]+=curr.size()*(t/cycle_len);
                t%=cycle_len;
                ans[idx]+=curr.order_of_key({t,INF});
                ans[idx]+=offset;
            }
            if(x==i)return;
            calc(adj[x].first,len+adj[x].second);
        };
        calc(adj[i].first,adj[i].second);
    }
    for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 3 ms 5720 KB Output is correct
3 Correct 5 ms 5952 KB Output is correct
4 Correct 13 ms 5980 KB Output is correct
5 Correct 106 ms 6700 KB Output is correct
6 Correct 122 ms 6700 KB Output is correct
7 Correct 105 ms 6740 KB Output is correct
8 Correct 5 ms 5976 KB Output is correct
9 Correct 4 ms 5980 KB Output is correct
10 Correct 4 ms 5980 KB Output is correct
11 Correct 4 ms 5976 KB Output is correct
12 Correct 108 ms 6732 KB Output is correct
13 Correct 112 ms 6736 KB Output is correct
14 Correct 51 ms 6232 KB Output is correct
15 Correct 52 ms 6228 KB Output is correct
16 Correct 53 ms 6248 KB Output is correct
17 Correct 52 ms 6284 KB Output is correct
18 Correct 59 ms 6256 KB Output is correct
19 Correct 61 ms 6236 KB Output is correct
20 Correct 64 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 13500 KB Output is correct
2 Correct 381 ms 34644 KB Output is correct
3 Correct 147 ms 36092 KB Output is correct
4 Correct 151 ms 36440 KB Output is correct
5 Execution timed out 5028 ms 96592 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 3 ms 5720 KB Output is correct
3 Correct 5 ms 5952 KB Output is correct
4 Correct 13 ms 5980 KB Output is correct
5 Correct 106 ms 6700 KB Output is correct
6 Correct 122 ms 6700 KB Output is correct
7 Correct 105 ms 6740 KB Output is correct
8 Correct 5 ms 5976 KB Output is correct
9 Correct 4 ms 5980 KB Output is correct
10 Correct 4 ms 5980 KB Output is correct
11 Correct 4 ms 5976 KB Output is correct
12 Correct 108 ms 6732 KB Output is correct
13 Correct 112 ms 6736 KB Output is correct
14 Correct 51 ms 6232 KB Output is correct
15 Correct 52 ms 6228 KB Output is correct
16 Correct 53 ms 6248 KB Output is correct
17 Correct 52 ms 6284 KB Output is correct
18 Correct 59 ms 6256 KB Output is correct
19 Correct 61 ms 6236 KB Output is correct
20 Correct 64 ms 6488 KB Output is correct
21 Correct 77 ms 13500 KB Output is correct
22 Correct 381 ms 34644 KB Output is correct
23 Correct 147 ms 36092 KB Output is correct
24 Correct 151 ms 36440 KB Output is correct
25 Execution timed out 5028 ms 96592 KB Time limit exceeded
26 Halted 0 ms 0 KB -