Submission #462705

# Submission time Handle Problem Language Result Execution time Memory
462705 2021-08-11T00:59:56 Z PedroBigMan Shortcut (IOI16_shortcut) C++14
0 / 100
1 ms 356 KB
#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 500000000LL
#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;}
    };
      
    class LV //lazy value
    {
        public:
        ll a;
        LV() {a=0LL;}
        LV(ll x) {a=x;}
        
        LV operator & (LV X) {LV ANS(a+X.a); return ANS;}
    };
    
    SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node
    {
        SV X(p[c].a+lazy[c].a);
        return X;
    }
    
    SV neuts; LV neutl;
    
    vector<SV> p;
    vector<LV> lazy;
    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--;
        }
        REP(i,0,2*N) {lazy.pb(neutl);}
    }
    
    void prop(ll c) //how lazy values propagate
    {
        lazy[2*c]=lazy[c]&lazy[2*c]; lazy[2*c+1]=lazy[c]&lazy[2*c+1];
        lazy[c]=neutl;
    }
    
    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 upval(c);}
        prop(c);
		p[c]=upval(c);
        SV ans = query(a,b,2*c)&query(a,b,2*c+1);
        return ans;
    }
    
    void update(LV s, ll a, ll b, 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>b) {return ;}
        if(x>=a && y<=b) 
        {
            lazy[c]=s&lazy[c]; 
            return;
        }
		prop(c);
        update(s,a,b,2*c); update(s,a,b,2*c+1);
        p[c]=upval(2*c)&upval(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.query(i,i).a;
		S2.update(d[i]+D2.query(counter[i]+1,N-1)-D(i,N-1)-val,i,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,curdiam;
	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.query(i,i).a;
			S1.update(d[a]+D1.query(a+1LL,counter[a])-D(0,a)-val,i,i);
			val = S2.query(i,i).a;
			S2.update(d[a]+D2.query(counter[a]+1,N-1)-D(a,N-1)-val,i,i);
			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));
		curdiam=max(max(curdiam1,curdiam2),max(curdiam3,curdiam4));
		ans=min(ans,curdiam);
	}
	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:214: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]
  214 |   while(counterT<ToOrder.size() && ToOrder[counterT].ff<=DD)
      |         ~~~~~~~~^~~~~~~~~~~~~~~
shortcut.cpp:208:5: warning: unused variable 'diam' [-Wunused-variable]
  208 |  ll diam=ans;
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 356 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 204 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -