#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// #define int long long
const int N=5e4+1000;
vector<pair<int,int>> ma[N];
// int ans[6][6];
// int ans_[6][6];
int n,k;
struct Data
{
int mat[6][6];
int block=0;
Data()
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
mat[i][j]=1e9;
}
}
}
Data(int x)
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
mat[i][j]=x*(i!=j);
}
}
}
void print()
{
cout<<"Start\n";
for(int i=0;i<6;i++)
{
for(int l=0;l<6;l++)
{
cout<<mat[i][l]<<' ';
}
cout<<endl;
}
cout<<"Finish\n";
cout<<"Block "<<block<<endl;
}
};
Data* seg[N*8];
Data* merge(Data* a,Data* b)
{
Data* ans=new Data();
// cout<<"Merge "<<a->block<<' '<<b->block<<endl;
for(int l=0;l<6;l++)
{
for(int i=0;i<6;i++)
{
// for(int j=0;j<6;j++)
// {
// for(auto& [l,w]:ma[(a->block*k)+j])
// {
// ans->mat[j][l%k]=min(ans->mat[j][l%k],a->mat[i][j]+b->mat[j][l%k]+w);
// }
// }
// cout<<"From "<<l<<" to "<<i<<' '<<a->mat[l][i]<<endl;
for(auto& [j,w]:ma[(a->block*k)+i])
{
for(int p=0;p<6;p++)
{
// cout<<"For "<<
ans->mat[l][p]=min(ans->mat[l][p],a->mat[l][i]+w+b->mat[j%k][p]);
}
}
}
}
ans->block=b->block;
// cout<<"APDS\n";
return ans;
}
void build(int v=1,int s=0,int e=(n-1)/k)
{
// cout<<s<<' '<<e<<' '<<v<<endl;
if(s==e)
{
seg[v]=new Data(1e9);
seg[v]->block=s;
// cout<<"Leaf "<<s<<endl;
// seg[v]->print();
return;
}
int lc=(2*v);
int rc=lc+1;
int mid=(s+e)/2;
build(lc,s,mid);
build(rc,mid+1,e);
seg[v]=new Data();
seg[v]=merge(seg[lc],seg[rc]);
// cout<<"For range "<<s<<' '<<e<<endl;
// seg[v]->print();
}
Data* get(int l,int r,int v=1,int s=0,int e=(n-1)/k)
{
// if(r<s or e<l)
// {
// Data* ap=new Data(0);
// return ap;
// }
Data* ap;
if(l<=s and e<=r)
{
ap=seg[v];
}
else
{
int lc=(2*v);
int rc=lc+1;
int mid=(s+e)/2;
if(s<=r and mid>=l)
{
ap=get(l,r,lc,s,mid);
if((mid+1)<=r and e>=l)
{
ap=merge(ap,get(l,r,rc,mid+1,e));
}
}
else
{
if((mid+1)<=r and e>=l)
{
ap=get(l,r,rc,mid+1,e);
}
}
}
if(ap==NULL)
exit(-1);
return ap;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int m,q;
cin>>k>>n>>m>>q;
for(int j=0;j<m;j++)
{
int a,b,t;
cin>>a>>b>>t;
ma[a].push_back({b,t});
}
// for(int lp=0;lp<q;lp++)
// {
// int x,y;
// cin>>x>>y;
// query[x].push_back({y,lp});
// }
// for(int i=0;i<n;i++)
// {
// index[i]=block[i/k].size();
// block[(i/k)].push_back(i);
// }
// for(int l=0;l<n;l++)
// for(int ind=0;ind<k;ind++)
// for(int p=0;p<k;p++)
// Dist[l][ind][p]=1e9;
// for(int i=0;i<n;i++)
// Dist[(i/k)][i%k][i%k]=we;
build();
for(int lp=0;lp<q;lp++)
{
int x,y;
cin>>x>>y;
int cur_block=x/k;
int block_to=y/k;
Data* apl=get(cur_block,block_to);
int ansp=apl->mat[x%k][y%k];
if(ansp==1e9)
{
cout<<-1<<'\n';
}
else
{
cout<<ansp<<'\n';
}
}
return 0;
}
Compilation message
toll.cpp: In function 'Data* get(int, int, int, int, int)':
toll.cpp:139:12: warning: 'ap' may be used uninitialized in this function [-Wmaybe-uninitialized]
139 | return ap;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
49736 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
5 ms |
4248 KB |
Output is correct |
6 |
Correct |
5 ms |
4184 KB |
Output is correct |
7 |
Correct |
5 ms |
4184 KB |
Output is correct |
8 |
Correct |
130 ms |
50628 KB |
Output is correct |
9 |
Correct |
119 ms |
50432 KB |
Output is correct |
10 |
Correct |
41 ms |
48208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
27560 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
20 ms |
9296 KB |
Output is correct |
8 |
Correct |
27 ms |
8416 KB |
Output is correct |
9 |
Correct |
82 ms |
41004 KB |
Output is correct |
10 |
Correct |
116 ms |
26708 KB |
Output is correct |
11 |
Correct |
119 ms |
29476 KB |
Output is correct |
12 |
Correct |
73 ms |
24544 KB |
Output is correct |
13 |
Correct |
71 ms |
13004 KB |
Output is correct |
14 |
Correct |
53 ms |
15188 KB |
Output is correct |
15 |
Correct |
43 ms |
10324 KB |
Output is correct |
16 |
Correct |
40 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2624 KB |
Output is correct |
4 |
Correct |
1 ms |
2596 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3060 KB |
Output is correct |
8 |
Correct |
3 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
47 ms |
30804 KB |
Output is correct |
11 |
Correct |
46 ms |
19796 KB |
Output is correct |
12 |
Correct |
55 ms |
17236 KB |
Output is correct |
13 |
Correct |
58 ms |
17748 KB |
Output is correct |
14 |
Correct |
61 ms |
16716 KB |
Output is correct |
15 |
Correct |
26 ms |
8124 KB |
Output is correct |
16 |
Correct |
26 ms |
8128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2624 KB |
Output is correct |
4 |
Correct |
1 ms |
2596 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3060 KB |
Output is correct |
8 |
Correct |
3 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
47 ms |
30804 KB |
Output is correct |
11 |
Correct |
46 ms |
19796 KB |
Output is correct |
12 |
Correct |
55 ms |
17236 KB |
Output is correct |
13 |
Correct |
58 ms |
17748 KB |
Output is correct |
14 |
Correct |
61 ms |
16716 KB |
Output is correct |
15 |
Correct |
26 ms |
8124 KB |
Output is correct |
16 |
Correct |
26 ms |
8128 KB |
Output is correct |
17 |
Correct |
74 ms |
25116 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
11 ms |
6236 KB |
Output is correct |
24 |
Correct |
15 ms |
5808 KB |
Output is correct |
25 |
Correct |
20 ms |
4948 KB |
Output is correct |
26 |
Correct |
19 ms |
5212 KB |
Output is correct |
27 |
Correct |
69 ms |
36688 KB |
Output is correct |
28 |
Correct |
99 ms |
22356 KB |
Output is correct |
29 |
Correct |
107 ms |
23116 KB |
Output is correct |
30 |
Correct |
76 ms |
21676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
49736 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
5 ms |
4248 KB |
Output is correct |
6 |
Correct |
5 ms |
4184 KB |
Output is correct |
7 |
Correct |
5 ms |
4184 KB |
Output is correct |
8 |
Correct |
130 ms |
50628 KB |
Output is correct |
9 |
Correct |
119 ms |
50432 KB |
Output is correct |
10 |
Correct |
41 ms |
48208 KB |
Output is correct |
11 |
Correct |
101 ms |
27560 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
20 ms |
9296 KB |
Output is correct |
18 |
Correct |
27 ms |
8416 KB |
Output is correct |
19 |
Correct |
82 ms |
41004 KB |
Output is correct |
20 |
Correct |
116 ms |
26708 KB |
Output is correct |
21 |
Correct |
119 ms |
29476 KB |
Output is correct |
22 |
Correct |
73 ms |
24544 KB |
Output is correct |
23 |
Correct |
71 ms |
13004 KB |
Output is correct |
24 |
Correct |
53 ms |
15188 KB |
Output is correct |
25 |
Correct |
43 ms |
10324 KB |
Output is correct |
26 |
Correct |
40 ms |
10488 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2624 KB |
Output is correct |
30 |
Correct |
1 ms |
2596 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
3164 KB |
Output is correct |
33 |
Correct |
2 ms |
3060 KB |
Output is correct |
34 |
Correct |
3 ms |
2904 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
47 ms |
30804 KB |
Output is correct |
37 |
Correct |
46 ms |
19796 KB |
Output is correct |
38 |
Correct |
55 ms |
17236 KB |
Output is correct |
39 |
Correct |
58 ms |
17748 KB |
Output is correct |
40 |
Correct |
61 ms |
16716 KB |
Output is correct |
41 |
Correct |
26 ms |
8124 KB |
Output is correct |
42 |
Correct |
26 ms |
8128 KB |
Output is correct |
43 |
Correct |
74 ms |
25116 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2392 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
11 ms |
6236 KB |
Output is correct |
50 |
Correct |
15 ms |
5808 KB |
Output is correct |
51 |
Correct |
20 ms |
4948 KB |
Output is correct |
52 |
Correct |
19 ms |
5212 KB |
Output is correct |
53 |
Correct |
69 ms |
36688 KB |
Output is correct |
54 |
Correct |
99 ms |
22356 KB |
Output is correct |
55 |
Correct |
107 ms |
23116 KB |
Output is correct |
56 |
Correct |
76 ms |
21676 KB |
Output is correct |
57 |
Correct |
237 ms |
33392 KB |
Output is correct |
58 |
Correct |
114 ms |
50516 KB |
Output is correct |
59 |
Correct |
164 ms |
38036 KB |
Output is correct |