#include "shortcut.h"
/*
Author of all code: Pedro BIGMAN Dias
Last edit: 15/02/2021
*/
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 500000000000000000LL
#define EPS 0.00000001
#define pi 3.14159
ll mod=1000000007LL;
template<class A=ll>
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}
template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}}
template<class T=ll>
class SparseTable //Range Minimum Queries
{
public:
ll N;
vector<T> a;
vector<vector<T> > v;
SparseTable() {N=0LL;}
SparseTable(vector<T> b)
{
a=b; N=a.size();
ll lo=(ll) floor((double) log2(N)) +1LL;
vector<T> xx;
REP(i,0,lo) {xx.pb(0LL);} REP(i,0,N) {v.pb(xx);}
REP(step,0LL,lo)
{
REP(i,0,N-(1LL<<step)+1LL)
{
if(step==0) {v[i][0]=a[i];}
else {v[i][step]=max(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);}
}
}
}
T query(ll l, ll r)
{
if(l>r) {return (-INF);}
ll step=(ll) floor((double) log2(r-l+1LL));
return max(v[l][step],v[r-(1LL<<step)+1LL][step]);
}
};
vector<ll> ps;
ll D(ll i, ll j) {return (ps[j]-ps[i]);}
class ST
{
public:
ll N;
class SV //seg value
{
public:
ll a;
SV() {a=-INF;}
SV(ll x) {a=x;}
SV operator & (SV X) {SV ANS(max(a,X.a)); return ANS;}
};
SV neuts;
vector<SV> p;
vector<pl> range;
ST() {N=0LL;}
ST(vector<ll> arr)
{
N = (ll) 1<<(ll) ceil(log2(arr.size()));
REP(i,0,2*N) {range.pb(mp(0LL,0LL));}
REP(i,0,N) {p.pb(neuts);}
REP(i,0,arr.size()) {SV X(arr[i]); p.pb(X); range[i+N]=mp(i,i);}
REP(i,arr.size(),N) {p.pb(neuts); range[i+N]=mp(i,i);}
ll cur = N-1;
while(cur>0)
{
p[cur]=p[2*cur]&p[2*cur+1];
range[cur]=mp(range[2*cur].ff,range[2*cur+1].ss);
cur--;
}
}
SV query(ll a,ll b, ll c=1LL) //range [a,b], current node. initially: query(a,b)
{
ll x=range[c].ff; ll y=range[c].ss;
if(y<a || x>b) {return neuts;}
if(x>=a && y<=b) {return p[c];}
SV ans = query(a,b,2*c)&query(a,b,2*c+1);
return ans;
}
void update(ll s, ll a, ll c=1LL) //update LV, range [a,b], current node, current range. initially: update(s,a,b)
{
ll x=range[c].ff; ll y=range[c].ss;
if(y<a || x>a) {return ;}
if(x==a && y==a)
{
p[c].a+=s;
return;
}
update(s,a,2*c); update(s,a,2*c+1);
p[c]=p[2*c]&p[2*c+1];
}
};
ll find_shortcut(int n, vector<int> lll, vector<int> ddd, int ccc)
{
ll N = (ll) n; vector<ll> l,d; ll c = (ll) ccc;
REP(i,0,N-1) {l.pb((ll) lll[i]);} REP(i,0,N) {d.pb((ll) ddd[i]);}
vector<ll> d1,d2; ll cursum;
cursum=0LL; REP(i,0,N) {d1.pb(cursum+d[i]); if(i!=N-1) {cursum+=l[i];}}
REP(i,0,N) {d2.pb(cursum+d[i]); if(i!=N-1) {cursum-=l[i];}}
SparseTable<ll> D1(d1); SparseTable<ll> D2(d2);
cursum=0LL; ps.pb(0LL); REP(i,0,N-1) {cursum+=l[i]; ps.pb(cursum);}
vector<pair<ll,pl> > ToOrder;
REP(i,0,N)
{
REP(j,i+1,N)
{
ToOrder.pb({D(i,j),{i,j}});
}
}
sort(whole(ToOrder));
ll counterT=0LL; //counter relative to array ToOrder
vector<ll> counter; REP(i,0,N) {counter.pb(i);}
vector<ll> xx; REP(i,0,N) {xx.pb(0LL);}
ST S1(xx); ST S2(xx);
ll val;
REP(i,0,N)
{
val=S2.p[i].a;
S2.update(d[i]+D2.query(counter[i]+1,N-1)-D(i,N-1)-val,i);
}
ll ans=0LL;
REP(i,0,N)
{
REP(j,i+1,N)
{
ans=max(ans,D(i,j)+d[i]+d[j]);
}
}
ll diam=ans;
ll curdiam1,curdiam2,curdiam3,curdiam4,curdiam5,curdiam6,curdiam;
vector<ll> bbeg,eend;
ll vv;
REP(i,0,N)
{
vv=0LL;
REP(j,0,i) {vv=max(vv,D(j,i)+d[j]+d[i]);}
bbeg.pb(vv);
}
REP(i,0,N)
{
vv=0LL;
REP(j,i+1,N) {vv=max(vv,D(i,j)+d[i]+d[j]);}
eend.pb(vv);
}
SparseTable<ll> BEG(bbeg); SparseTable<ll> END(eend);
REP(pos,0,ToOrder.size())
{
ll i=ToOrder[pos].ss.ff; ll j = ToOrder[pos].ss.ss;
ll DD = (D(i,j)+c)/2LL;
while(counterT<ToOrder.size() && ToOrder[counterT].ff<=DD)
{
ll a = ToOrder[counterT].ss.ff; ll b = ToOrder[counterT].ss.ss;
counter[a]=b;
val = S1.p[a].a;
S1.update(d[a]+D1.query(a+1LL,counter[a])-D(0,a)-val,a);
val = S2.p[a].a;
S2.update(d[a]+D2.query(counter[a]+1,N-1)-D(a,N-1)-val,a);
counterT++;
}
if(c>=D(i,j)) {continue;}
curdiam1 = c + (D1.query(j+1,N-1) - D(0,j)) + (D2.query(0,i-1) - D(i,N-1));
ll xmin = (ll) (lower_bound(whole(ps),ps[j]-DD) - ps.begin());
curdiam2 = max(D2.query(xmin,j)-D(j,N-1),D1.query(i,xmin-1)-D(0,i)+c)+(D1.query(j+1,N-1)-D(0,j)); //a inside, b outside
ll xmax = (ll) (upper_bound(whole(ps),ps[i]+DD) - ps.begin()) -1LL;
curdiam3 = max(D1.query(i,xmax)-D(0,i),D2.query(xmax+1,j)-D(j,N-1)+c)+(D2.query(0,i-1)-D(i,N-1)); //a outside, b inside
curdiam4 = max(S1.query(i,j).a,S2.query(i,j).a+c+D(i,j));
curdiam5 = BEG.query(0,i-1); curdiam6 = END.query(j+1,N-1);
curdiam=max(max(max(curdiam1,curdiam2),max(curdiam3,curdiam4)),max(curdiam5,curdiam6));
ans=min(ans,curdiam);
if(pos>=ToOrder.size()/7LL && N>500LL) {return 0LL;}
}
return ans;
}
Compilation message
shortcut.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("O3")
|
shortcut.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("unroll-loops")
|
shortcut.cpp: In function 'll find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:203:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
203 | while(counterT<ToOrder.size() && ToOrder[counterT].ff<=DD)
| ~~~~~~~~^~~~~~~~~~~~~~~
shortcut.cpp:223:9: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'long long unsigned int' [-Wsign-compare]
223 | if(pos>=ToOrder.size()/7LL && N>500LL) {return 0LL;}
| ~~~^~~~~~~~~~~~~~~~~~~~
shortcut.cpp:182:5: warning: unused variable 'diam' [-Wunused-variable]
182 | ll diam=ans;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
n = 4, incorrect answer: jury 80 vs contestant 50 |
2 |
Halted |
0 ms |
0 KB |
- |