Submission #946322

# Submission time Handle Problem Language Result Execution time Memory
946322 2024-03-14T13:47:38 Z guagua0407 Harvest (JOI20_harvest) C++17
100 / 100
410 ms 158188 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

struct BIT{
    vector<int> bit;
    int n;
    BIT(int N){
        n=N;
        bit=vector<int>(n,0);
    }
    void update(int pos,int val){
        for(;pos<n;pos+=(pos&-pos)){
            bit[pos]+=val;
        }
    }
    int query(int pos){
        int ans=0;
        for(;pos>0;pos-=(pos&-pos)){
            ans+=bit[pos];
        }
        return ans;
    }
};

const int mxn=400000+5;
const int inf=1e18;
int a[mxn];
int b[mxn];
int nxt[mxn*2];
bool visited[mxn*2];
bool is_cycle[mxn*2];
vector<pair<int,ll>> adj[mxn*2];
int belong[mxn*2];
ll depth[mxn*2];
ll d[mxn*2];
ll sum[mxn*2];
int rev[mxn*2];
int st[mxn*2],en[mxn*2];
vector<pair<int,int>> sta;
vector<pair<int,int>> js[mxn];
vector<pair<pair<int,int>,int>> is[mxn];
vector<vector<ll>> pre;
int n,m,l,c;
int timer=0;

void dfs(int id,int p,int v){
    st[v]=++timer;
    if(v>=n){
        sta.push_back({depth[v]-pre[id][rev[p]],p});
    }
    for(auto u:adj[v]){
        if(is_cycle[u.f]) continue;
        depth[u.f]=depth[v]+u.s;
        dfs(id,p,u.f);
    }
    en[v]=timer;
}

signed main() {_
    cin>>n>>m>>l>>c;
    ll C=c;
    c%=l;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<m;i++){
        cin>>b[i];
    }
    for(int i=0;i<n;i++){
        int pos=upper_bound(a,a+n,(a[i]-c+l)%l)-a-1;
        if(pos==-1) pos=n-1;
        nxt[i]=pos;
        d[i]=((a[i]-c+l)%l-a[pos]+l)%l+C;
    }
    for(int i=0;i<m;i++){
        int pos=upper_bound(a,a+n,b[i])-a-1;
        if(pos==-1) pos=n-1;
        nxt[i+n]=pos;
        d[i+n]=(b[i]-a[pos]+l)%l;
    }
    for(int i=0;i<n+m;i++){
        adj[nxt[i]].push_back({i,d[i]});
    }
    vector<vector<int>> cycle;
    for(int i=0;i<n+m;i++){
        if(visited[i]) continue;
        int x=i;
        vector<int> tmp;
        while(!visited[x]){
            visited[x]=true;
            tmp.push_back(x);
            x=nxt[x];
        }
        bool ok=false;
        for(auto v:tmp){
            ok|=(v==x);
            is_cycle[v]=ok;
        }
        if(!ok) continue;
        vector<int> curcycle;
        for(auto v:tmp){
            if(is_cycle[v]){
                curcycle.push_back(v);
                belong[v]=(int)cycle.size();
            }
        }
        cycle.push_back(curcycle);
        vector<ll> dis;
        dis.push_back(0);
        for(int i=1;i<(int)curcycle.size();i++){
            dis.push_back(dis.back()+d[curcycle[i-1]]);
        }
        pre.push_back(dis);
        for(int i=0;i<(int)curcycle.size();i++){
            rev[curcycle[i]]=i;
        }
    }
    for(int i=0;i<(int)cycle.size();i++){
        int cnt=0;
        for(auto v:cycle[i]){
            sta.clear();
            dfs(i,v,v);
            js[i].insert(js[i].end(),all(sta));
            sum[i]+=d[v];
        }
    }
    int q;
    cin>>q;
    vector<pair<ll,pair<int,int>>> qys;
    vector<int> res(q);
    for(int i=0;i<q;i++){
        int v;
        ll t;
        cin>>v>>t;
        v--;
        if(!is_cycle[v]){
            qys.push_back({t+depth[v],{i,v}});
            continue;
        }
        int id=belong[v];
        is[id].push_back({{t-pre[id][rev[v]],v},i});
    }
    // tree
    {
        for(int i=n;i<n+m;i++){
            qys.push_back({depth[i],{-1,i}});
        }
        sort(qys.begin(),qys.end());
        BIT T(mxn);
        for(auto query:qys){
            int id=query.s.f;
            int v=query.s.s;
            if(id==-1){
                T.update(st[v],1);
            }
            else{
                res[id]=T.query(en[v])-T.query(st[v]);
            }
        }
    }
    // cycle
    for(int t=0;t<(int)cycle.size();t++){
        {
            vector<pair<pair<int,int>,int>> vec;
            vector<int> xs;
            xs.push_back(-inf);
            for(auto v:is[t]){
                int a=v.f.f/sum[t];
                assert(sum[t]!=0);
                if(a*sum[t]>v.f.f) a--;
                int b=v.f.f-a*sum[t];
                xs.push_back(b);
                vec.push_back({{a,v.s},b});
            }
            for(auto v:js[t]){
                int a=v.f/sum[t];
                assert(sum[t]!=0);
                if(a*sum[t]>v.f) a--;
                int b=v.f-a*sum[t];
                xs.push_back(b);
                vec.push_back({{a,-1},b});
            }
            sort(all(xs));
            xs.resize(unique(all(xs))-xs.begin());
            sort(all(vec));
            int sum2=0;
            int cnt=0;
            BIT T(xs.size());
            for(auto v:vec){
                int pos=lower_bound(all(xs),v.s)-xs.begin();
                if(v.f.s==-1){
                    sum2+=v.f.f;
                    cnt++;
                    T.update(pos,1);
                }
                else{
                    int i=v.f.s;
                    res[i]+=cnt*v.f.f;
                    res[i]-=sum2;
                    res[i]+=T.query(pos);
                }
            }
        }
        {
            vector<pair<pair<int,int>,int>> vec;
            vector<int> xs;
            xs.push_back(-inf);
            for(auto v:is[t]){
                xs.push_back(v.f.f);
                vec.push_back({{rev[v.f.s],v.s},v.f.f});
            }
            for(auto v:js[t]){
                xs.push_back(v.f);
                vec.push_back({{rev[v.s],-1},v.f});
            }
            sort(all(xs));
            xs.resize(unique(all(xs))-xs.begin());
            sort(all(vec));
            reverse(all(vec));
            BIT T(xs.size());
            for(auto v:vec){
                int pos=lower_bound(all(xs),v.s)-xs.begin();
                if(v.f.s==-1){
                    T.update(pos,1);
                }
                else{
                    int i=v.f.s;
                    res[i]-=T.query(pos);
                }
            }
        }
    }
    for(int i=0;i<q;i++){
        //assert(res[i]>=0);
        cout<<res[i]<<'\n';
    }
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
/*
1 1 2 5
0
1
10
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
*/

Compilation message

harvest.cpp: In function 'int main()':
harvest.cpp:135:13: warning: unused variable 'cnt' [-Wunused-variable]
  135 |         int cnt=0;
      |             ^~~
harvest.cpp: In function 'void setIO(std::string)':
harvest.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harvest.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 61276 KB Output is correct
2 Correct 16 ms 61884 KB Output is correct
3 Correct 17 ms 61900 KB Output is correct
4 Correct 15 ms 61808 KB Output is correct
5 Correct 15 ms 62108 KB Output is correct
6 Correct 14 ms 61992 KB Output is correct
7 Correct 15 ms 61992 KB Output is correct
8 Correct 15 ms 61736 KB Output is correct
9 Correct 16 ms 61736 KB Output is correct
10 Correct 15 ms 61732 KB Output is correct
11 Correct 15 ms 61736 KB Output is correct
12 Correct 16 ms 61784 KB Output is correct
13 Correct 16 ms 61788 KB Output is correct
14 Correct 16 ms 61788 KB Output is correct
15 Correct 15 ms 61992 KB Output is correct
16 Correct 15 ms 61800 KB Output is correct
17 Correct 15 ms 61988 KB Output is correct
18 Correct 14 ms 61992 KB Output is correct
19 Correct 15 ms 61936 KB Output is correct
20 Correct 15 ms 61852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 77904 KB Output is correct
2 Correct 143 ms 85232 KB Output is correct
3 Correct 249 ms 117136 KB Output is correct
4 Correct 289 ms 120500 KB Output is correct
5 Correct 137 ms 109964 KB Output is correct
6 Correct 151 ms 109460 KB Output is correct
7 Correct 115 ms 87844 KB Output is correct
8 Correct 114 ms 89016 KB Output is correct
9 Correct 260 ms 101956 KB Output is correct
10 Correct 239 ms 105604 KB Output is correct
11 Correct 273 ms 104668 KB Output is correct
12 Correct 260 ms 103204 KB Output is correct
13 Correct 263 ms 101848 KB Output is correct
14 Correct 260 ms 106944 KB Output is correct
15 Correct 234 ms 95640 KB Output is correct
16 Correct 139 ms 101900 KB Output is correct
17 Correct 132 ms 100500 KB Output is correct
18 Correct 96 ms 80364 KB Output is correct
19 Correct 96 ms 80440 KB Output is correct
20 Correct 122 ms 91472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 61276 KB Output is correct
2 Correct 16 ms 61884 KB Output is correct
3 Correct 17 ms 61900 KB Output is correct
4 Correct 15 ms 61808 KB Output is correct
5 Correct 15 ms 62108 KB Output is correct
6 Correct 14 ms 61992 KB Output is correct
7 Correct 15 ms 61992 KB Output is correct
8 Correct 15 ms 61736 KB Output is correct
9 Correct 16 ms 61736 KB Output is correct
10 Correct 15 ms 61732 KB Output is correct
11 Correct 15 ms 61736 KB Output is correct
12 Correct 16 ms 61784 KB Output is correct
13 Correct 16 ms 61788 KB Output is correct
14 Correct 16 ms 61788 KB Output is correct
15 Correct 15 ms 61992 KB Output is correct
16 Correct 15 ms 61800 KB Output is correct
17 Correct 15 ms 61988 KB Output is correct
18 Correct 14 ms 61992 KB Output is correct
19 Correct 15 ms 61936 KB Output is correct
20 Correct 15 ms 61852 KB Output is correct
21 Correct 197 ms 77904 KB Output is correct
22 Correct 143 ms 85232 KB Output is correct
23 Correct 249 ms 117136 KB Output is correct
24 Correct 289 ms 120500 KB Output is correct
25 Correct 137 ms 109964 KB Output is correct
26 Correct 151 ms 109460 KB Output is correct
27 Correct 115 ms 87844 KB Output is correct
28 Correct 114 ms 89016 KB Output is correct
29 Correct 260 ms 101956 KB Output is correct
30 Correct 239 ms 105604 KB Output is correct
31 Correct 273 ms 104668 KB Output is correct
32 Correct 260 ms 103204 KB Output is correct
33 Correct 263 ms 101848 KB Output is correct
34 Correct 260 ms 106944 KB Output is correct
35 Correct 234 ms 95640 KB Output is correct
36 Correct 139 ms 101900 KB Output is correct
37 Correct 132 ms 100500 KB Output is correct
38 Correct 96 ms 80364 KB Output is correct
39 Correct 96 ms 80440 KB Output is correct
40 Correct 122 ms 91472 KB Output is correct
41 Correct 283 ms 123452 KB Output is correct
42 Correct 341 ms 135944 KB Output is correct
43 Correct 282 ms 122812 KB Output is correct
44 Correct 384 ms 158188 KB Output is correct
45 Correct 249 ms 138972 KB Output is correct
46 Correct 262 ms 140400 KB Output is correct
47 Correct 255 ms 139680 KB Output is correct
48 Correct 265 ms 138984 KB Output is correct
49 Correct 235 ms 141164 KB Output is correct
50 Correct 213 ms 117544 KB Output is correct
51 Correct 240 ms 118148 KB Output is correct
52 Correct 410 ms 132648 KB Output is correct
53 Correct 399 ms 134424 KB Output is correct
54 Correct 392 ms 132700 KB Output is correct
55 Correct 400 ms 132004 KB Output is correct
56 Correct 303 ms 130104 KB Output is correct
57 Correct 303 ms 130312 KB Output is correct
58 Correct 305 ms 131792 KB Output is correct
59 Correct 261 ms 130848 KB Output is correct
60 Correct 259 ms 131772 KB Output is correct
61 Correct 258 ms 132340 KB Output is correct
62 Correct 384 ms 121224 KB Output is correct
63 Correct 190 ms 116556 KB Output is correct
64 Correct 197 ms 117944 KB Output is correct
65 Correct 191 ms 115292 KB Output is correct
66 Correct 222 ms 115616 KB Output is correct
67 Correct 211 ms 117044 KB Output is correct
68 Correct 227 ms 114716 KB Output is correct
69 Correct 264 ms 123272 KB Output is correct
70 Correct 256 ms 116560 KB Output is correct
71 Correct 263 ms 119984 KB Output is correct
72 Correct 263 ms 123092 KB Output is correct