This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
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>
#include "seats.h"
using namespace std;
typedef 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 50000000
#define EPS 0.00000001
#define pi 3.14159
#define VV(vvvv,NNNN,xxxx); REP(i,0,NNNN) {vvvv.pb(xxxx);}
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);}}
class ST_max
{
public:
ll N;
class SV //seg value
{
public:
ll a;
SV() {a=0LL;}
SV(ll x) {a=x;}
SV operator & (SV X) {SV ANS(max(a,X.a)); return ANS;}
};
SV neuts;
SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node
{
return p[c];
}
vector<SV> p;
vector<pl> range;
ST_max() {N=0LL;}
ST_max(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 upval(c);}
SV ans = query(a,b,2*c)&query(a,b,2*c+1);
return ans;
}
void update(SV 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]=s;
return;
}
update(s,a,2*c); update(s,a,2*c+1);
p[c]=upval(2*c)&upval(2*c+1);
}
ll First(ll val, ll c=1LL) //First index with value >=val in subtree of c
{
ll x = range[c].ff; ll y = range[c].ss;
if(x==y) {return (c-N);}
if(p[2*c].a>=val) {return First(val,2*c);} else {return First(val,2*c+1);}
}
};
class ST_min
{
public:
ll N;
class SV //seg value
{
public:
ll a;
SV() {a=INF;}
SV(ll x) {a=x;}
SV operator & (SV X) {SV ANS(min(a,X.a)); return ANS;}
};
SV neuts;
SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node
{
return p[c];
}
vector<SV> p;
vector<pl> range;
ST_min() {N=0LL;}
ST_min(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 upval(c);}
SV ans = query(a,b,2*c)&query(a,b,2*c+1);
return ans;
}
void update(SV 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]=s;
return;
}
update(s,a,2*c); update(s,a,2*c+1);
p[c]=upval(2*c)&upval(2*c+1);
}
ll First(ll val, ll c=1LL) //First index with value <=val in subtree of c
{
ll x = range[c].ff; ll y = range[c].ss;
if(x==y) {return (c-N);}
if(p[2*c].a<=val) {return First(val,2*c);} else {return First(val,2*c+1);}
}
};
ll h,w,N;
ST_max H_max,W_max; ST_min H_min,W_min;
void give_initial_chart(int hh, int ww, vector<int> RR, vector<int> CC)
{
h=hh; w=ww; N=h*w;
H_max=*(new ST_max(RR));
H_min=*(new ST_min(RR));
W_max=*(new ST_max(CC));
W_min=*(new ST_min(CC));
}
int swap_seats(int a, int b)
{
if(b<a) {swap(a,b);}
pl pa = {H_max.query(a,a).a,W_max.query(a,a).a};
pl pb = {H_max.query(b,b).a,W_max.query(b,b).a};
H_max.update(pb.ff,a); H_min.update(pb.ff,a); W_max.update(pb.ss,a); W_min.update(pb.ss,a);
H_max.update(pa.ff,b); H_min.update(pa.ff,b); W_max.update(pa.ss,b); W_min.update(pa.ss,b);
ll ans=0LL;
unordered_set<ll> index;
ll h0=H_max.query(0,0).a; ll w0=W_max.query(0,0).a;
index.insert(N-1);
ll ind;
REP(i,h0+1,h)
{
ind = H_max.First(i); index.insert(ind-1);
}
for(ll i=h0-1; i>=0; i--)
{
ind = H_min.First(i); index.insert(ind-1);
}
REP(i,w0+1,w)
{
ind = W_max.First(i); index.insert(ind-1);
}
for(ll i=w0-1; i>=0; i--)
{
ind = W_min.First(i); index.insert(ind-1);
}
unordered_set<ll>::iterator it = index.begin();
while(it!=index.end())
{
ind = *it;
if((H_max.query(0,ind).a-H_min.query(0,ind).a+1LL)*(W_max.query(0,ind).a-W_min.query(0,ind).a+1LL)==ind+1LL) {ans++;}
it++;
}
return ans;
}
Compilation message (stderr)
seats.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("O3")
|
seats.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |