#include "shortcut.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=5100;
const ll INF=1e9+10;
#define dbg(x) cout<<"[ "<<#x<<" ] : "<<x<<endl;
#define F first
#define S second
#define all(v) v.begin(),v.end()
vector<long long> pref(MAXN+1,0);
vector<int> D;
pair<long long,long long> tree[4*MAXN];
void build(int node,int l,int r){
if(l==r){
tree[node].F=pref[l]+D[l];
tree[node].S=l;
return;
}
int mid=(l+r)/2;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
tree[node].F=max(tree[node<<1].F,tree[node<<1|1].F);
if(tree[node].F==tree[node<<1].F) tree[node].S=tree[node<<1].S;
else tree[node].S=tree[node<<1|1].S;
}
int query(int node,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
return tree[node].S;
}
int mid=(l+r)/2;
int ans=0;
int index=0;
if(ql<=mid) {
int x=query(node<<1,l,mid,ql,qr);
if(pref[x]+D[x]>ans) {
ans=pref[x]+D[x];
index=x;
}
}
if(qr>mid){
int x=query(node<<1|1,mid+1,r,ql,qr);
if(pref[x]+D[x]>ans) {
ans=pref[x]+D[x];
index=x;
}
}
return index;
}
long long find_shortcut(int n, vector<int> l, vector<int> d, int c)
{
D=d;
if(n==2){
if(c<l[0])
return (long long)d[0]+c+(long long)d[1];
return (ll)d[0]+(ll)l[0]+(ll)d[1];
}
for(long long i=0;i<n-1;i++){
pref[i+1]=pref[i]+l[i];
}
build(1,0,n-1);
vector<long long> left(MAXN+1,0);
vector<long long> right(MAXN+1,0);
// vector<bool> used_left(MAXN+1,false);
// vector<bool> used_right(MAXN+1,false);
//the idea is to use pref[i-1]+l[i] or d[i]
left[0]=(ll)d[0];
long long first_node=0;
for(long long i=1;i<n;i++){
if(d[i]>left[i-1]+l[i-1]) first_node=i;
left[i]=max(left[i-1]+l[i-1],(ll)d[i]);
}
long long last_node=n-1;
right[n-1]=d[n-1];
for(long long i=n-2;i>=0;i--){
if(d[i]>right[i+1]+l[i]&&i!=first_node) last_node=i;
right[i]=max(right[i+1]+l[i],(ll)d[i]);
}
// used_left[first_node]=true;
vector<long long> nodes;//nodes in the diameter
// cout<<first_node<<" "<<last_node<<endl;
for(int i=0;i<n;i++){
// cout<<i<<" -->> "<<" "<<pref[i]<<endl;
}
long long diameter=left[first_node]+right[first_node];
for(long long i=0;i<=n-1;i++){
for(long long j=i+1;j<=n-1;j++){
long long express=left[i]+c+right[j];
long long newdiameter=0;
for(long long k=0;k<=n-1;k++){
//last diameter or the sum of values mabin j nd k+ max in right of the j.
// Hna we add a new case that is using teleporter o passing mn left dial k
// So here it's like right[j]+c... Hna wsslna l i
//then we add depth mabin i and j
newdiameter=max({newdiameter,
min(
(ll)abs(pref[j]-pref[k])+right[j]+(d[k]==right[j]&&k==j?0LL:d[k]) ,
(ll)right[j]+(ll)c+(ll)abs(pref[i]-pref[k])+(d[k]==right[j]&&k==j?0LL:d[k])
)
});
}
// dbg(newdiameter);
for(long long k=n-1;k>=0;k--){
//hna we check like wach kayn wa7d diameter starting mn dak left[i] tal k
newdiameter=max({newdiameter,
min(
(ll)abs(pref[k]-pref[i])+left[i]+(d[k]==left[i]&&k==i?0:d[k]),
(ll)left[i]+c+abs(pref[j]-pref[k])+(d[k]==left[i]&&k==i?0:d[k])
)
});
}
// cout<<newdiameter<<" "<<express<<" "<<diameter<<" "<<i<<" "<<j<<endl;
//db the problem if there is the start of the new diameter and end dialo wsst [i,j] it won't find it
//So here the idea dial n^4 but mablanch ndiruha
//Now how to make it n^3logn
//So here the idea is to use i think chi rmq table
//we will fix the start x so it makes n
//and we will try to find using bs dak node y
//So here chnu khassna 7na the node mabin i+1 and j-1
//such that
//anyway we will use a segtree
long long ans=0;
for(int x=i+1;x<j;x++){
int y=query(1,0,n-1,x,j-1);
ans=max(ans,pref[y]-pref[x]+d[y]+d[x]);
// cout<<<<endl;
// cout<<query(1,0,n-1,x,j-1)<<endl;
}
// cout<<endl<<endl;
diameter=min({max({ans,newdiameter,(ll)express}),(ll)diameter});
}
}
// cout<<c<<endl;
return diameter;
}
// int main()
// {
// int n, c;
// assert(2 == scanf("%d%d", &n, &c));
// std::vector<int> l(n - 1);
// std::vector<int> d(n);
// for (int i = 0; i < n - 1; i++)
// assert(1 == scanf("%d", &l[i]));
// for (int i = 0; i < n; i++)
// assert(1 == scanf("%d", &d[i]));
// long long t = find_shortcut(n, l, d, c);
// printf("%lld\n", t);
// return 0;
// }
Compilation message
shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:75:15: warning: variable 'last_node' set but not used [-Wunused-but-set-variable]
75 | long long last_node=n-1;
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
0 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
340 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
340 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
340 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
340 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
340 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
340 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
0 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
340 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
340 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
0 ms |
336 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
0 ms |
340 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
0 ms |
340 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
0 ms |
340 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
340 KB |
n = 10, 7000000000 is a correct answer |
20 |
Correct |
0 ms |
340 KB |
n = 5, 12 is a correct answer |
21 |
Correct |
0 ms |
340 KB |
n = 5, 25 is a correct answer |
22 |
Correct |
1 ms |
340 KB |
n = 2, 122 is a correct answer |
23 |
Correct |
0 ms |
340 KB |
n = 10, 117 is a correct answer |
24 |
Incorrect |
0 ms |
340 KB |
n = 10, incorrect answer: jury 336 vs contestant 360 |
25 |
Halted |
0 ms |
0 KB |
- |