Submission #1094318

# Submission time Handle Problem Language Result Execution time Memory
1094318 2024-09-29T10:33:39 Z vjudge1 Harvest (JOI20_harvest) C++17
100 / 100
341 ms 184428 KB
#include<bits/stdc++.h>
using namespace std;
#define int ll
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(),x.end()
#define for1(i,x,n) for(int i=x;i<=n;i++)
#define for2(i,x,n) for(int i=x;i>=n;i--)

typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 1e6 + 69;
const ll mod  = 1e9 + 7;
const ll inf  = 1e18;

int n,m,l,c,a[maxn],b[maxn],q;
int in[maxn],out[maxn],Time,cnt;
bool vis[maxn],cycle[maxn];
int st[maxn];
int w[maxn],id[maxn],sum[maxn];
vector<int> adj[maxn],nen[maxn],L[maxn],Q;
int dep[maxn],deg[maxn],up[maxn],res[maxn];
struct qr
{
    int id,u,v,w;
    bool operator <(const qr& o) const
    {
        return make_pair(w,id) < make_pair(o.w,o.id) ;
    }
};
vector<qr> query1,query2[maxn];

void Update(int u,int v)
{
    for(;u<=4e5;u+=u&-u) st[u]+=v;
}
int Get1(int u)
{
    int r=0;
    for(;u>0;u-=u&-u) r+=st[u];
    return r;
}
int Get(int l,int r) {return Get1(r) - Get1(l-1);}

void dfs(int u)
{
    in[u]=++Time;
    if (u>n) L[cnt].pb(dep[u]);
    vis[u]=1;
//    cerr<<u<<' '<<dep[u]<<'\n';
    id[u]=cnt;
    for(int v:adj[u]) if (!vis[v])
    {
        dep[v]=dep[u]+w[v];
        dfs(v);
    }
    out[u]=Time;
}

void sol()
{
    cin >> n >> m >> l >> c;
    for1(i,1,n) cin >> a[i];
    for1(i,1,m) cin >> b[i];
    for1(i,1,n)
    {
        int j=upper_bound(a+1,a+1+n,(a[i]-(c%l)+l)%l)-a-1;
        if (j==0) j=n;
        adj[j].pb(i);
        deg[j]++;
        up[i]=j;
        w[i]=c + ((a[i]-(c%l)+l)%l-a[j]+l)%l;
//        cerr<< i<<" <- "<<j<<' '<<w[i]<<'\n';
    }
    for1(i,1,m)
    {
        int j=upper_bound(a+1,a+1+n,b[i])-a-1;
        if (j==0) j=n;
        adj[j].pb(i+n);
        deg[j]++;
        up[i+n]=j;
        w[i+n]=(b[i]-a[j]+l)%l;
//        cerr<< i+n<<" <- "<<j<<' '<<w[i+n]<<'\n';
    }
    for1(i,1,n+m)
    {
        if (!deg[i]) Q.pb(i);
        cycle[i]=1;
    }
    while (!Q.empty())
    {
        int u=Q.back();
        Q.pop_back();
        cycle[u]=0;
        if (!(--deg[up[u]]))  Q.pb(up[u]);
    }
    for1(i,1,n+m)
    {
        if (!vis[i] && cycle[i])
        {
            cnt++;
            dfs(i);
            sum[cnt]=dep[up[i]]+w[i];
//            cerr<<"ca "<<sum[cnt]<<'\n';
            sort(all(L[cnt]));
            for(int x:L[cnt]) nen[cnt].pb(x%sum[cnt]);
            sort(all(nen[cnt]));
            nen[cnt].resize(unique(all(nen[cnt]))-nen[cnt].begin());
            for(int x:L[cnt]) query2[cnt].pb({0,lower_bound(all(nen[cnt]),x%sum[cnt])-nen[cnt].begin()+1,-1,x});
        }
    }
    for1(i,n+1,n+m)
    {
        query1.pb({0,in[i],-1,dep[i]});
    }
    cin >> q;
    for1(i,1,q)
    {
        int u,x;cin >>u>> x;
        int uu=id[u];
        x+=dep[u];
        query1.pb({i,in[u],out[u],x});
        if (cycle[u] && x>=sum[uu])
        {
            x-=sum[uu];
//            cerr<< x<<'\n';
            int j=upper_bound(all(nen[uu]),x%sum[uu])-nen[uu].begin();
            query2[uu].pb({i,1,j,x});
        }
    }
    sort(all(query1));
    for(qr x:query1)
    {
        int id=x.id,u=x.u,v=x.v,w=x.w;
        if (id==0) Update(u,1);
        else res[id]+=Get(u,v);
    }
    for(qr x:query1) if (x.id==0) Update(x.u,-1);
//    cerr<< res[1]<<" po\n";
    for1(i,1,cnt)
    {
        sort(all(query2[i]));
        int pos=0,pre=0;
        for(qr x:query2[i])
        {
            int id=x.id,u=x.u,v=x.v,w=x.w;
//            cerr << id<<' '<<u<<' '<<v<<' '<<w<<'\n';
            if (id==0)
            {
                pos++;
                pre+=w/sum[i];
                Update(u,1);
            }
            else
            {
                res[id]+=(w/sum[i])*pos-pre;
                if (v>=u) res[id]+=Get(u,v);
            }
        }
        for(qr x:query2[i]) if (x.id==0) Update(x.u,-1);
    }
    for1(i,1,q) cout << res[i]<<'\n';
}


int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;//cin >> t;
    while (t--) sol();
}
/*
3 2 7 3
1 4 6
0 5
1
1 100
*/

Compilation message

harvest.cpp: In function 'void sol()':
harvest.cpp:136:33: warning: unused variable 'w' [-Wunused-variable]
  136 |         int id=x.id,u=x.u,v=x.v,w=x.w;
      |                                 ^
# Verdict Execution time Memory Grader output
1 Correct 42 ms 94804 KB Output is correct
2 Correct 46 ms 95568 KB Output is correct
3 Correct 43 ms 95580 KB Output is correct
4 Correct 44 ms 95500 KB Output is correct
5 Correct 43 ms 95696 KB Output is correct
6 Correct 46 ms 95712 KB Output is correct
7 Correct 54 ms 95676 KB Output is correct
8 Correct 47 ms 95536 KB Output is correct
9 Correct 44 ms 95440 KB Output is correct
10 Correct 46 ms 95448 KB Output is correct
11 Correct 46 ms 95484 KB Output is correct
12 Correct 45 ms 95696 KB Output is correct
13 Correct 46 ms 95816 KB Output is correct
14 Correct 68 ms 95708 KB Output is correct
15 Correct 56 ms 95572 KB Output is correct
16 Correct 39 ms 95568 KB Output is correct
17 Correct 59 ms 95696 KB Output is correct
18 Correct 46 ms 95620 KB Output is correct
19 Correct 44 ms 95580 KB Output is correct
20 Correct 55 ms 95572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 112572 KB Output is correct
2 Correct 175 ms 130268 KB Output is correct
3 Correct 246 ms 139732 KB Output is correct
4 Correct 210 ms 138164 KB Output is correct
5 Correct 179 ms 142500 KB Output is correct
6 Correct 183 ms 142560 KB Output is correct
7 Correct 158 ms 129852 KB Output is correct
8 Correct 146 ms 129848 KB Output is correct
9 Correct 227 ms 150656 KB Output is correct
10 Correct 187 ms 150664 KB Output is correct
11 Correct 228 ms 149628 KB Output is correct
12 Correct 230 ms 149792 KB Output is correct
13 Correct 239 ms 149552 KB Output is correct
14 Correct 197 ms 149592 KB Output is correct
15 Correct 216 ms 147604 KB Output is correct
16 Correct 170 ms 136104 KB Output is correct
17 Correct 176 ms 135848 KB Output is correct
18 Correct 147 ms 114784 KB Output is correct
19 Correct 126 ms 114640 KB Output is correct
20 Correct 149 ms 129972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 94804 KB Output is correct
2 Correct 46 ms 95568 KB Output is correct
3 Correct 43 ms 95580 KB Output is correct
4 Correct 44 ms 95500 KB Output is correct
5 Correct 43 ms 95696 KB Output is correct
6 Correct 46 ms 95712 KB Output is correct
7 Correct 54 ms 95676 KB Output is correct
8 Correct 47 ms 95536 KB Output is correct
9 Correct 44 ms 95440 KB Output is correct
10 Correct 46 ms 95448 KB Output is correct
11 Correct 46 ms 95484 KB Output is correct
12 Correct 45 ms 95696 KB Output is correct
13 Correct 46 ms 95816 KB Output is correct
14 Correct 68 ms 95708 KB Output is correct
15 Correct 56 ms 95572 KB Output is correct
16 Correct 39 ms 95568 KB Output is correct
17 Correct 59 ms 95696 KB Output is correct
18 Correct 46 ms 95620 KB Output is correct
19 Correct 44 ms 95580 KB Output is correct
20 Correct 55 ms 95572 KB Output is correct
21 Correct 164 ms 112572 KB Output is correct
22 Correct 175 ms 130268 KB Output is correct
23 Correct 246 ms 139732 KB Output is correct
24 Correct 210 ms 138164 KB Output is correct
25 Correct 179 ms 142500 KB Output is correct
26 Correct 183 ms 142560 KB Output is correct
27 Correct 158 ms 129852 KB Output is correct
28 Correct 146 ms 129848 KB Output is correct
29 Correct 227 ms 150656 KB Output is correct
30 Correct 187 ms 150664 KB Output is correct
31 Correct 228 ms 149628 KB Output is correct
32 Correct 230 ms 149792 KB Output is correct
33 Correct 239 ms 149552 KB Output is correct
34 Correct 197 ms 149592 KB Output is correct
35 Correct 216 ms 147604 KB Output is correct
36 Correct 170 ms 136104 KB Output is correct
37 Correct 176 ms 135848 KB Output is correct
38 Correct 147 ms 114784 KB Output is correct
39 Correct 126 ms 114640 KB Output is correct
40 Correct 149 ms 129972 KB Output is correct
41 Correct 288 ms 169632 KB Output is correct
42 Correct 281 ms 168728 KB Output is correct
43 Correct 137 ms 128432 KB Output is correct
44 Correct 228 ms 163440 KB Output is correct
45 Correct 250 ms 174720 KB Output is correct
46 Correct 250 ms 175504 KB Output is correct
47 Correct 247 ms 176788 KB Output is correct
48 Correct 236 ms 176380 KB Output is correct
49 Correct 223 ms 176644 KB Output is correct
50 Correct 227 ms 169120 KB Output is correct
51 Correct 225 ms 168436 KB Output is correct
52 Correct 301 ms 179324 KB Output is correct
53 Correct 307 ms 184428 KB Output is correct
54 Correct 310 ms 179368 KB Output is correct
55 Correct 304 ms 175248 KB Output is correct
56 Correct 275 ms 167552 KB Output is correct
57 Correct 293 ms 168376 KB Output is correct
58 Correct 286 ms 170132 KB Output is correct
59 Correct 263 ms 168772 KB Output is correct
60 Correct 271 ms 169432 KB Output is correct
61 Correct 269 ms 169612 KB Output is correct
62 Correct 341 ms 178820 KB Output is correct
63 Correct 216 ms 146260 KB Output is correct
64 Correct 216 ms 146308 KB Output is correct
65 Correct 211 ms 146928 KB Output is correct
66 Correct 220 ms 154680 KB Output is correct
67 Correct 214 ms 154744 KB Output is correct
68 Correct 209 ms 154160 KB Output is correct
69 Correct 269 ms 171416 KB Output is correct
70 Correct 244 ms 167580 KB Output is correct
71 Correct 258 ms 160916 KB Output is correct
72 Correct 254 ms 160876 KB Output is correct