Submission #468222

# Submission time Handle Problem Language Result Execution time Memory
468222 2021-08-27T08:33:12 Z Nihal_9936 Toll (BOI17_toll) C++17
0 / 100
333 ms 361780 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds; 
 
template <typename num_t>
using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>;
 
#ifdef  pikachu
   #include  "welcome_to_python_is_slower_than_c++_club.h"
#else
    #include <bits/stdc++.h>
     using namespace std;
    #define debug(...) 69
#endif
 
 
template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;}
template<typename T> void print(T t) { cout << t <<' '; }
template<typename T, typename... Args> void print(T t, Args... args) { print(t);print(args...); }
string operator*=(string& s, int cnt) { string t = s;for (size_t i = 1; i < cnt; i++)s += t;return s; }
string operator*(string s, int cnt) { return s *= cnt; }
 
 
#define int               long long 
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define allr(x)           (x).rbegin(),(x).rend()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define len(x)             (int)((x).size())
#define bk                back()
#define elif              else if
#define add               insert
#define append            push_back
#define pop               pop_back 
#define str               string
#define in                :
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define vi                vector<int>
#define vii               vector<pii>
#define mi                map<int,int>
#define mii               map<pii,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define rrep(i,a,b)       for(int i=a;i>b;i--)
#define el                 '\n'
#define printl(arg) cout << arg << endl
// #define print(arg) cout << arg
#define inputa(arg) for (auto& e : arg) cin >> e
#define printa(arg) for (auto& e : arg) print(e);
#define printr(arg) { printl(arg);return; }
#define printd(arg) printf("%0.3lf\n", arg)
 
 
const int mod=1e9+7;
const int INF=1e18;
const int MAX_N= 5e4+10;
const double PI=3.14159265358979323846;
 
int n,m,k,x,y,z,t,q,counter;
vector<vector<int>> a[MAX_N][5];
int dat[17][MAX_N][5][5], mask[MAX_N];// 18 = ceil(log2(n))
int cc[5][5];
vector<set<int>> L(18);


int fun1(int x1,int y1,int lvl,int id){//associative
    rep(i,0,5){
        int j=i;
        for(auto& vl in a[y1-1][j]){
            rep(k,0,5){
                dat[lvl][id][i][k]=min(dat[lvl][id][i][k],vl[1]+dat[lvl][y1][vl[0]][k]);
            }
        }
    }
}

int fun2(int x1,int y1,int lvl,int id){//associative
    rep(i,0,5){
        rep(j,0,5){
            if(dat[lvl][x1][i][j]!=mod){
                for(auto& vl in a[y1-1][j]){
                    // debug(x1,y1-1,j,vl);
                    rep(k,0,5){
                        dat[lvl][id][i][k]=min(dat[lvl][id][i][k],dat[lvl][x1][i][j]+vl[1]+cc[vl[0]][k]);
                    }
                }
            }
        }
    }
}
int fun3(int x1,int y1,int lvl,int z){//associative
    rep(i,0,5){
        rep(j,0,5){
            if(dat[lvl][x1][i][j]!=mod){
                for(auto& vl in a[z][j]){ 
                    // debug(vl);
                    rep(k,0,5){
                        cc[i][k]=min(cc[i][k],dat[lvl][x1][i][j]+vl[1]+dat[lvl][y1][vl[0]][k]);
                    }
                }
            }
        }
    }

}


void divi(int l, int r, int lev) { // generate dat and mask
    if (l == r) return;
    int m = (l+r)/2;
    // debug(lev,l,m,r);
    L[lev].add(m);

    rep(i,0,5) rep(j,0,5) dat[lev][m][i][j]=((i==j)?0:mod);
    rrep(i,m-1,l-1) fun1(i,i+1,lev,i);

    // dat[lev][m+1] = a[m+1];
    rep(i,0,5) rep(j,0,5) dat[lev][m+1][i][j]=((i==j)?0:mod);

    rep(i,m+2,r+1) fun2(i-1,i,lev,i);

    rep(i,m+1,r+1) mask[i] ^= 1<<lev;
    divi(l,m,lev+1); divi(m+1,r,lev+1);
}
void query(int x,int y){

    int t = __builtin_ctz(mask[x]^mask[y]);
    // debug(x,y,t);
    // debug(dat[t][x],5,5);
    // debug(dat[t][y],5,5);
    z=*(L[t].upper_bound(x-1));
    fun3(x,y,t,z);//operations
    // debug(cc,5,5);
}

void code(){
            cin>>q>>n>>m>>counter;
            rep(lvl,0,17) rep(k,0,MAX_N) rep(i,0,5) rep(j,0,5)
                dat[lvl][k][i][j]=mod;

            rep(i,0,5) rep(j,0,5) cc[i][j]=((i==j)?0:mod);

            while(m--){
                cin>>x>>y>>z;
                a[x/q][x%q].append({y%q,z});
            }
            // rep(i,0,10) rep(j,0,5) debug(a[i][j],i,j);
            
            divi(0,(n+q-1)/q-1,0);
            while(counter--){
                cin>>x>>y;
                // debug(x,y);
                rep(i,0,5) rep(j,0,5) cc[i][j]=mod;
                if(x/q==y/q) cout<<-1<<el;
                else{
                    query(x/q,y/q);
                    // debug(cc,5,5);
                    if(cc[x%q][y%q]==mod) cout<<-1<<el;
                    else cout<<cc[x%q][y%q]<<el;
                }
            }
            // rep(i,0,10) debug(dat[0][i],5,5);
            
            

            
            
 
}
 
 
signed main()
{
 
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
 
    // freopen("cowjog.in", "r", stdin);
    // freopen("cowjog.out", "w", stdout);
 
    int t = 1;
    // cin>>t;
    while(t--) code();
    return 0;
}

Compilation message

toll.cpp: In function 'std::string operator*=(std::string&, int)':
toll.cpp:24:75: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   24 | string operator*=(string& s, int cnt) { string t = s;for (size_t i = 1; i < cnt; i++)s += t;return s; }
      |                                                                         ~~^~~~~
toll.cpp: In function 'long long int fun1(long long int, long long int, long long int, long long int)':
toll.cpp:83:1: warning: no return statement in function returning non-void [-Wreturn-type]
   83 | }
      | ^
toll.cpp: In function 'long long int fun2(long long int, long long int, long long int, long long int)':
toll.cpp:98:1: warning: no return statement in function returning non-void [-Wreturn-type]
   98 | }
      | ^
toll.cpp: In function 'long long int fun3(long long int, long long int, long long int, long long int)':
toll.cpp:113:1: warning: no return statement in function returning non-void [-Wreturn-type]
  113 | }
      | ^
# Verdict Execution time Memory Grader output
1 Runtime error 313 ms 355960 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 333 ms 361780 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 273 ms 349568 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 273 ms 349568 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 313 ms 355960 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -