Submission #464324

#TimeUsernameProblemLanguageResultExecution timeMemory
464324PedroBigManWall (IOI14_wall)C++14
100 / 100
2122 ms189864 KiB
#include "wall.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);}} 

vector<ll> op;

pl Reduce()
{
	while(op.size()>2LL)
	{
		ll a = op[op.size()-3]; ll b = op[op.size()-2]; ll c = op[op.size()-1];
		op.pop_back(); op.pop_back(); op.pop_back();
		if(a>0 && b>0) {op.pb(max(a,b)); op.pb(c); continue;}
		if(b>0 && c>0) {op.pb(a); op.pb(max(b,c)); continue;}
		if(a<0 && b<0) {op.pb(max(a,b)); op.pb(c); continue;}
		if(b<0 && c<0) {op.pb(max(a,b)); op.pb(c); continue;}
		if(a>0 && b<0 && c>0)
		{
			if(abs(b)<=abs(a))
			{
				ll endval = max(-b,c);
				op.pb(-1); op.pb(endval);
				continue;
			}
			else if(abs(b)<=abs(c))
			{
				ll endval = c;
				op.pb(-1); op.pb(endval);
				continue;
			}
			else
			{
				op.pb(b); op.pb(max(a,c));
				continue;
			}
		}
		if(a<0 && b>0 && c<0)
		{
			if(abs(b)>=abs(a))
			{
				ll endval = min(b,-c);
				op.pb(-1); op.pb(endval);
				continue;
			}
			else if(abs(b)>=abs(c))
			{
				ll endval = -c;
				op.pb(-1); op.pb(endval);
				continue;
			}
			else
			{
				op.pb(b); op.pb(max(a,c));
				continue;
			}
		}
	}
	op.pb(0LL); op.pb(0LL);
	return {op[0],op[1]};
}

ll Reduce_Max(pl opp)
{
	ll val = abs(opp.ff);
	if(opp.ss>0) {return max(val,opp.ss);}
	else {return min(val,abs(opp.ss));}
}

class ST
{
    public:
    ll N;
    
    class LV //lazy value
    {
        public:
        pl a; //elements are positive if max operation, negative if min operation. 
        LV() {a={1LL,1LL};}
        LV(pl x) {a=x;}
        
        LV operator & (LV X) 
		{
			op[0]=X.a.ff; op[1]=X.a.ss; op[2]=a.ff; op[3]=a.ss;
			LV ANS(Reduce());
			return ANS;
		}
    };
    
	LV neutl;
    
    vector<LV> lazy;
    vector<pl> range;
    
    ST() {N=0LL;}
    ST(ll n)
    {
        N = (ll) 1<<(ll) ceil(log2(n));
        REP(i,0,2*N) {range.pb(mp(0LL,0LL));}
        REP(i,0,N) {range[i+N]=mp(i,i);}
        ll cur = N-1;
        while(cur>0)
        {
            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;
    }
    
    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);
    }
	
	vector<ll> eject(ll S) //range [a,b], current node. initially: query(a,b)
    {
        REP(c,1LL,N) {prop(c);}
		vector<ll> ans; REP(i,N,N+S) {ans.pb(Reduce_Max(lazy[i].a));}
		return ans;
    }
};

void buildWall(int N, int K, int oper[], int left[], int right[], int height[], int finalHeight[])
{
	REP(i,0,4) {op.pb(0LL);}
	ST S(N); S.update((pl) {1,-1},0,N-1);
	REP(i,0,K)
	{
		if(oper[i]==1)
		{
			S.update((pl) {height[i]+1,1},left[i],right[i]);
		}
		else
		{
			S.update((pl) {-(height[i]+1),1},left[i],right[i]);
		}
	}
	vector<ll> ans = S.eject(N);
	REP(i,0,N) {finalHeight[i]=ans[i]-1;}
	return;
}

Compilation message (stderr)

wall.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O3")
      | 
wall.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("unroll-loops")
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...