Submission #1268815

#TimeUsernameProblemLanguageResultExecution timeMemory
1268815monaxiaTrains (BOI24_trains)C++20
16 / 100
867 ms360984 KiB
#include <bits/stdc++.h>
#include <ext/random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define vektor vector

using namespace std;
using namespace __gnu_pbds; 

using ll = long long;
using ull = unsigned long long;
using ld = long double;

constexpr ull Mod = 1e9 + 7;
constexpr ull sqr = 227;
constexpr ld eps = 1e-9;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(ll l, ll r) {if(l <= r) return uniform_int_distribution <ll> (l, r)(rng); return -1;} 


void solve(){ 
    int n;
    cin >> n;

    vector <int> d(n + 1), x(n + 1);

    vector <vector <int>> calc(1), ref(1), tree(n + 1);
    vector <int> ans(n + 1, 0);

    vector <vector <int>> ref2(sqr + 5, vector <int> (sqr + 5));

    ans[1] = 1;

    int timer = 0;

    for(int i = 1; i <= min(n, (int)sqr); i ++){
        for(int j = 1; j <= i; j ++){
            timer ++;

            ref2[j % i][i] = timer;

            ref.pb({0});
            calc.pb({0});

            for(int k = j; k <= n; k += i){
                ref[timer].pb(k);
                calc[timer].pb(0);

                tree[k].pb(timer);
            }
        }
    }

    vector <int> curptr(timer + 1, 1);
    for(int i = 1; i <= timer; i ++) calc[i].pb(INT_MAX);

    for(int i = 1; i <= n; i ++){
        int x, d;
        cin >> d >> x;


        for(auto& tr : tree[i]){
            (calc[tr][curptr[tr]] += calc[tr][curptr[tr] - 1]) %= Mod;
            (ans[i] += calc[tr][curptr[tr]]) %= Mod;

            curptr[tr] ++;
        }

        if(!d) continue;

        if(min(x * d, (n - i) / d) <= sqr){
            for(int j = 1; j <= x && i + j * d <= n; j ++)
                (ans[i + j * d] += ans[i]) %= Mod;

            continue;
        }

        else{
            int tr = ref2[i % d][d];

            // cout << i << " " << tr << "\n";

            int r = upper_bound(all(ref[tr]), i + x * d) - ref[tr].begin();

            (calc[tr][curptr[tr]] += ans[i]) %= Mod;
            calc[tr][r] = (calc[tr][r] - ans[i] + Mod) % Mod;
        }
    }

    ll res = 0;

    for(int i = 1; i <= n; i ++) (res += ans[i]) %= Mod;

    // for(int i = 1; i <= n; i ++) cout << ans[i] << " "; return;

    cout << res;
} 

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 

    if(fopen("CUUHO.inp", "r")){
        freopen("CUUHO.inp", "r", stdin);
        freopen("CUUHO.out", "w", stdout);
    }

    int t = 1;

    // cin >> t;

    while(t --){
        solve();
        cout << "\n";
    }
}

Compilation message (stderr)

Main.cpp:110:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  110 | main(){
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |         freopen("CUUHO.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen("CUUHO.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...