Submission #1013561

#TimeUsernameProblemLanguageResultExecution timeMemory
1013561AverageAmogusEnjoyerToll (BOI17_toll)C++17
100 / 100
62 ms31324 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> using namespace std; using ll = long long; template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; } template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; } mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); int rng() { return abs((int)mrand()); } // Current challenge: finish CSES problemset!! constexpr int nax = 50500; constexpr ll inf = 1e18; ll c[nax][5][5]; int n,M,q,k; int L[nax],R[nax]; // queries ll ans[nax]; ll lef[nax][5][5],rig[nax][5][5]; void solve(int l,int r,vector<int> &v) { if (l == r) { return; } for (int i=l;i<=r;i++) { for (int j=0;j<k;j++) { for (int z=0;z<k;z++) { lef[i][j][z] = rig[i][j][z] = inf; } } } int m = (l + r) / 2; for (int i=0;i<k;i++) lef[m][i][i] = 0; for (int w=0;w<k;w++) { for (int i=m-1;i>=l;i--) { for (int j=0;j<k;j++) { for (int z=0;z<k;z++) { cmin(lef[i][j][w],lef[i+1][z][w]+c[i][j][z]); } } } } for (int i=0;i<k;i++) rig[m+1][i][i] = 0; for (int w=0;w<k;w++) { for (int i=m+1;i<r;i++) { for (int j=0;j<k;j++) { for (int z=0;z<k;z++) { cmin(rig[i+1][w][z],rig[i][w][j]+c[i][j][z]); } } } } vector<vector<int>> vv(2); for (int &idx: v) { int lg = L[idx] / k, rg = R[idx] / k; int lk = L[idx] % k, rk = R[idx] % k; if (lg <= m && m < rg) { ll res = inf; for (int j=0;j<k;j++) { for (int z=0;z<k;z++) { cmin(res,lef[lg][lk][j]+c[m][j][z]+rig[rg][z][rk]); } } ans[idx] = res; } else { vv[lg > m].push_back(idx); } } solve(l,m,vv[0]); solve(m+1,r,vv[1]); } void solve() { cin >> k >> n >> M >> q; memset(c,0x3f,sizeof(c)); memset(ans,0x3f,sizeof(ans)); for (int x,y;M--;) { cin >> x >> y; cin >> c[x/k][x%k][y%k]; } for (int i=0;i<q;i++) { cin >> L[i] >> R[i]; } vector<int> v(q); iota(v.begin(),v.end(),0); solve(0,(n-1)/k,v); for (int i=0;i<q;i++) { cout << (ans[i] > (int)1e9 ? -1 : ans[i]) << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int _t = 1; // cin >> _t; for (int i=1;i<=_t;i++) { solve(); } }
#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...