Submission #1023286

# Submission time Handle Problem Language Result Execution time Memory
1023286 2024-07-14T14:58:25 Z Chris_Black Toll (BOI17_toll) C++17
17 / 100
54 ms 15684 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct custom_hash_pair {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}

	size_t operator()(pair<uint64_t,uint64_t> x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1);
	}
};

mt19937 rnd();

#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
using ull = unsigned long long;
using ll =  long long;
using ld = double;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<ld>;
using si = set<int>;
using ssi = set<si>;
using sl = set<ll>;
using ssl = set<sl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vs = vector<string>;
using vpi = vector<pii>;
using vvpi = vector<vpi>;
using vpl = vector<pll>;
using vvpl = vector<vpl>;
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define FORR(i, a, b) for(int i = a; i >= b; --i)
#define umap unordered_map
#define eb emplace_back
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define ff first
#define ss second
#define ar array
inline void yn(bool b)
{
    if(b==true)
        cout<<"Yes\n";
    else
        cout<<"No\n";
}
  inline void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}

ll Mod = 1e9 + 7;

template <int MOD=998'244'353>
struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

template <typename T> struct Fenwick {
    vector<T> a;
    int n;
    Fenwick(): a(),n(0) {}
    Fenwick(int _n){
        n = _n;
        a = vector<T>(n, T());
    }
    void clear(){
    a = vector<T>(n,T());
    }
    void add(int p, T x){
    for(;p < n ; p |= p+1)
        a[p] += x;
    }
    T get(int p){
    if(p < 0 ) return T();
    p = min(p,n-1);
    T res = T();
    for(;p >= 0 ; p  = (p & (p+1)) - 1)
        res += a[p];
    return res;
    }
    T getSum(int l,int r){
    return get(r-1) - get(l-1);
    }
};

template<typename T> struct SegmentProd{
    vector<T> adi;
    int n;
    SegmentProd(): adi(), n(0) {}
    SegmentProd(int _n){
        n=_n;
        adi = vector<T>(4*n, (T)1);
    }
    void clear(){
    adi = vector<T>(4*n,(T)1);
    }
    void update(int poz,ll val){
        updateintern(1,1,n,poz,val);
    }
    ll query(int st,int dr){
        return (queryintern(1,1,n,st,dr) % Mod);
    }

    ll queryintern(int nod,int st,int dr,int ST,int DR)
    {
        if(st>=ST && dr<=DR) return adi[nod] % Mod;
        int mij = (st+dr)/2;
        ll a=1,b=1;
        if(mij >= ST)
            a = queryintern(2*nod,st,mij,ST,DR);
        if(mij<DR)
            b = queryintern(2*nod+1,mij+1,dr,ST,DR);
        return (a*b)%Mod;
    }

    void updateintern(int nod,int st,int dr,int poz,ll val)
    {
        if(st==dr)
            {adi[nod] = val;
            return;}
        ll mij = (st+dr)/2;
        if(poz <= mij)
            updateintern(2*nod,st,mij,poz,val);
        else updateintern(2*nod+1,mij+1,dr,poz,val);
        if(st!=dr)
        adi[nod] = (adi[2*nod] * adi[2*nod+1]) % Mod;
    }
};

template <typename T> struct SegmentSum{
    vector<T> adi;
    int n;
    SegmentSum(): adi(), n(0) {}
    SegmentSum(int _n){
    n = _n;
    adi = vector<T>(4*n,(T)0);
    }
    void clear(){
    adi = vector<T>(4*n,0);
    }
    void update(int poz,int val){
    updateintern(1,1,n,poz,val);
    }
    void updateintern(int nod,int st,int dr,int poz,int val)
    {
        if(st==dr){
            adi[nod] = val;
            return;
        }
        int mij = (st+dr)/2;
        if(poz<=mij)
            updateintern(2*nod,st,mij,poz,val);
        else updateintern(2*nod+1,mij+1,dr,poz,val);
        if(st!=dr)
        adi[nod] = (adi[2*nod] + adi[2*nod+1]) % Mod;
    }
    ll query(int st,int dr){
    return queryintern(1,1,n,st,dr);
    }
    ll queryintern(int nod,int st,int dr,int ST,int DR){
    if(st>=ST && dr<=DR) return adi[nod];
    ll a = 0, b = 0;
    ll mij = (st+dr)/2;
    if(mij >= ST) a = queryintern(2*nod,st,mij,ST,DR);
    if(mij < DR) b = queryintern(2*nod+1,mij+1,dr,ST,DR);
    return a+b;
    }
};

#define int long long
const string TASK("trie");
ifstream fin(TASK + ".in");
ofstream fout(TASK + ".out");
//#define cin fin
//#define cout fout
int di[]={0,0,-1,1},dj[]={-1,1,0,0};
int d8i[]={-1,-1,-1,0,0,1,1,1},d8j[]={-1,0,1,-1,1,-1,0,1};
const ll Inf = LLONG_MAX >> 2;
bool testCase = false;
const ll N = 50009, LG = 21;

int k, n, m, o, ql[N], qr[N], ans[N], d[30][N];
vvpi G(N), GI(N);

void divide(int l, int r, vector<int>& ids)
{
    if(r - l + 1 <= 2 * k)///poate fi doar un drum
    {
//        for(auto i : ids)cout << "Answering " << i << '\n';

        for(auto i : ids)
            for(auto [y, c] : G[ql[i]])
                if(y == qr[i])
                    ans[i] = c;

        return;
    }

    int m = (l + r) >> 1;
    vector<int> st, dr, mj;
    for(auto i : ids)
    {
        if(qr[i] <= m)st.pb(i);
        else if(ql[i] > m)dr.pb(i);
        else mj.pb(i);
    }

    divide(l, m, st);
    divide(m + 1, r, dr);

    if(mj.empty())return;

    ///calc fiecare numar intre m - 2 * k + 2 si m + 2 * k - 1
    for(int i = max(l, m - 2 * k); i <= min(m + 2 * k, r); ++i)
        for(int j = l; j <= r; ++j)
            d[m - i + 10][j] = Inf;

    for(int i = max(l, m - 2 * k); i <= m; ++i)
    {
        d[m - i + 10][i] = 0;
        for(int x = i; x >= l; --x)
            for(auto [y, c] : GI[x])
                if(y >= l)
                    d[m - i + 10][y] = min(d[m - i + 10][y], d[m - i + 10][x] + c);
    }

    for(int i = m + 1; i <= min(r, m + 2 * k); ++i)
    {
        d[m - i + 10][i] = 0;
        for(int x = i; x <= r; ++x)
            for(auto [y, c] : G[x])
                if(y <= r)
                    d[m - i + 10][y] = min(d[m - i + 10][y], d[m - i + 10][x] + c);
    }

//    cout << l << ' ' << m << ' ' << r << '\n';

    for(auto i : mj)
        for(int x = max(l, m - 2 * k); x <= m; ++x)
            for(auto [y, c] : G[x])
                if(y > m && y <= r)
                {
//                    cout << "Answering " << i << ' ' << c << ' ' << d[m - x + 10][ql[i]] << ' ' << d[m - y + 10][qr[i]] << '\n';
                    ans[i] = min(ans[i], c + d[m - x + 10][ql[i]] + d[m - y + 10][qr[i]]);
                }

//    cout << '\n';
}

void solve_testcase()
{
    cin >> k >> n >> m >> o;

    int a, b, t;
    FOR(i, 1, m)
    {
        cin >> a >> b >> t;
        G[a].pb({b, t});
        GI[b].pb({a, t});
    }

    vector<int> qids;
    FOR(i, 0, o - 1)
    {
        cin >> ql[i] >> qr[i];
        ans[i] = Inf;
        qids.pb(i);
    }

    divide(0, n - 1, qids);

    FOR(i, 0, o - 1)
        if(ans[i] == Inf)cout << "-1\n";
        else cout << ans[i] << '\n';
}

/// Check for interruption of input!!

signed main() {
    fastio();
    int t=1;
    if (testCase) cin >> t;
    while (t--) solve_testcase();
      return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 9304 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 23 ms 9192 KB Output is correct
9 Correct 20 ms 9052 KB Output is correct
10 Correct 6 ms 5436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 11612 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2904 KB Output is correct
4 Correct 1 ms 2840 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 3 ms 3420 KB Output is correct
8 Correct 4 ms 3608 KB Output is correct
9 Correct 21 ms 9312 KB Output is correct
10 Correct 48 ms 15664 KB Output is correct
11 Correct 37 ms 11604 KB Output is correct
12 Correct 29 ms 12632 KB Output is correct
13 Correct 54 ms 15684 KB Output is correct
14 Correct 44 ms 10580 KB Output is correct
15 Correct 33 ms 11856 KB Output is correct
16 Correct 31 ms 11860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2908 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2908 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 9304 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 23 ms 9192 KB Output is correct
9 Correct 20 ms 9052 KB Output is correct
10 Correct 6 ms 5436 KB Output is correct
11 Correct 31 ms 11612 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2904 KB Output is correct
14 Correct 1 ms 2840 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 3 ms 3420 KB Output is correct
18 Correct 4 ms 3608 KB Output is correct
19 Correct 21 ms 9312 KB Output is correct
20 Correct 48 ms 15664 KB Output is correct
21 Correct 37 ms 11604 KB Output is correct
22 Correct 29 ms 12632 KB Output is correct
23 Correct 54 ms 15684 KB Output is correct
24 Correct 44 ms 10580 KB Output is correct
25 Correct 33 ms 11856 KB Output is correct
26 Correct 31 ms 11860 KB Output is correct
27 Correct 1 ms 2648 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Incorrect 1 ms 2908 KB Output isn't correct
30 Halted 0 ms 0 KB -