Submission #712289

#TimeUsernameProblemLanguageResultExecution timeMemory
712289mars4Toll (BOI17_toll)C++17
100 / 100
152 ms16176 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ff first #define ss second #define ll int64_t #define ld long double #define nl cout<<"\n" #define i128 __int128_t #define all(v) v.begin(),v.end() #define mset(a,v) memset((a),(v),sizeof(a)) #define forn(i,a,b) for(int64_t i=int64_t(a);i<int64_t(b);++i) #define forb(i,a,b) for(int64_t i=int64_t(a);i>=int64_t(b);--i) #define fastio() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mod 1'000'000'007 #define mod2 998'244'353 #define inf 1'000'000'000'000'007 #define pi 3.14159265358979323846 template<class key,class cmp=std::less<key>> using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>; template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p) {return out<<"("<<p.ff<<", "<<p.ss<<")";} template<class T> ostream& operator<<(ostream& out,vector<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";} template<class T> ostream& operator<<(ostream& out,deque<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";} template<class T> ostream& operator<<(ostream& out,set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";} template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";} template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m) {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";} void dbg_out() {cerr<<"]\n";} template<typename Head,typename... Tail> void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);} #ifdef LOCAL #define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__) #else #define dbg(...) #endif //---------------------------------mars4--------------------------------- vector<vector<vector<ll>>> a; vector<vector<vector<ll>>> lft; vector<vector<vector<ll>>> rht; vector<pair<ll,ll>> query; vector<ll> ans; ll K; vector<vector<ll>> combine(vector<vector<ll>> a,vector<vector<ll>> b) { vector<vector<ll>> res(K,vector<ll>(K,inf)); forn(i,0,K) { forn(j,0,K) { forn(k,0,K) { res[i][k]=min(res[i][k],a[i][j]+b[j][k]); } } } return res; } void calc(ll st,ll ed,vector<ll> ind) { if(st==ed or ind.empty()) { return; } ll mid=(st+ed)>>1; lft[mid]=a[mid]; rht[mid+1]=a[mid+1]; forb(i,mid-1,st) { lft[i]=combine(a[i],lft[i+1]); } forn(i,mid+2,ed) { rht[i]=combine(rht[i-1],a[i]); } vector<vector<ll>> nind(2); for(ll i:ind) { auto [L,R]=query[i]; ll l=L/K; ll r=R/K; if(l<=mid and r>mid+1) { ans[i]=combine(lft[l],rht[r-1])[L%K][R%K]; } else if(l<=mid and r==mid+1) { ans[i]=lft[l][L%K][R%K]; } else { nind[l>mid].push_back(i); } } calc(st,mid,nind[0]); calc(mid+1,ed,nind[1]); } int main() { fastio(); ll z,n,m,t,k,i,j,l,d,h,r; ll q; cin>>K>>n>>m>>q; ll N=(n+K-1)/K; a=vector<vector<vector<ll>>>(N,vector<vector<ll>>(K,vector<ll>(K,inf))); lft=vector<vector<vector<ll>>>(N,vector<vector<ll>>(K,vector<ll>(K,inf))); rht=vector<vector<vector<ll>>>(N,vector<vector<ll>>(K,vector<ll>(K,inf))); query=vector<pair<ll,ll>>(q); ans=vector<ll>(q,inf); forn(i,0,m) { cin>>l>>r>>d; a[l/K][l%K][r%K]=d; } for(auto &[l,r]:query) { cin>>l>>r; } vector<ll> ind(q); iota(all(ind),(ll)0); calc(0,N-1,ind); for(ll i:ans) { if(i==inf) { cout<<"-1\n"; } else { cout<<i<<"\n"; } } cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n"; return 0; }

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:112:5: warning: unused variable 'z' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
toll.cpp:112:11: warning: unused variable 't' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
toll.cpp:112:13: warning: unused variable 'k' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
toll.cpp:112:15: warning: unused variable 'i' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
toll.cpp:112:17: warning: unused variable 'j' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
toll.cpp:112:23: warning: unused variable 'h' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
#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...