이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include "seats.h"
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=1e6+50;
const int mod=1e9+7;
using namespace std;
int h,w,i,r[nmax],c[nmax],lzy[4*nmax],x,y,p[nmax],q;
pair<int,int>st[4*nmax];
pair<int,int> merge(pair<int,int> x,pair<int,int> y)
{
if(x.fr<y.fr)return x;
if(x.fr>y.fr)return y;
return make_pair(x.fr,x.sc+y.sc);
}
void build(int x,int l,int r)
{
if(l==r)
{
st[x]=make_pair(0,1);
return;
}
int mid=(l+r)/2;
build(2*x,l,mid);
build(2*x+1,mid+1,r);
st[x]=merge(st[2*x],st[2*x+1]);
}
void push(int nod,int l,int r)
{
if(!lzy[nod])return;
st[nod].fr+=lzy[nod];
if(l!=r)
{
lzy[nod*2]+=lzy[nod];
lzy[nod*2+1]+=lzy[nod];
}
lzy[nod]=0;
}
void upd(int nod,int l,int r,int tl,int tr,int v)
{
push(nod,l,r);
if(tl<=l && r<=tr)
{
lzy[nod]+=v;
push(nod,l,r);
return;
}
int mid=(l+r)/2;
if(tl<=mid)upd(nod*2,l,mid,tl,tr,v);
if(mid<tr)upd(nod*2+1,mid+1,r,tl,tr,v);
push(nod*2,l,mid);
push(nod*2+1,mid+1,r);
st[nod]=merge(st[2*nod],st[2*nod+1]);
}
pair<int,int> qry(int nod,int l,int r,int tl,int tr)
{
push(nod,l,r);
if(l>tr || r<tl)return make_pair(inf,0);
if(tl<=l && r<=tr)return st[nod];
int mid=(l+r)/2;
return merge(qry(nod*2,l,mid,tl,tr),qry(nod*2+1,mid+1,r,tl,tr));
}
void give_initial_chart(int H,int W,vector<int>R,vector<int>C)
{
h=H,w=W;
for(i=1;i<=h*w;i++)
{
r[i]=R[i-1]+1;
c[i]=C[i-1]+1;
}
if(h==1)
{
for(i=1;i<=w;i++)p[c[i]]=i;
p[0]=p[w+1]=w+1;
build(1,1,w);
for(i=1;i<=w;i++)
{
if(p[c[i]-1]>i && p[c[i]+1]>i)upd(1,1,w,i,w,2);
else if(p[c[i]-1]<i && p[c[i]+1]<i)upd(1,1,w,i,w,-2);
}
}
}
void add(int x,int ps,int cf)
{
if(p[ps-1]<x)upd(1,1,w,p[ps-1],x-1,cf);
else upd(1,1,w,x,p[ps-1]-1,cf);
if(p[ps+1]<x)upd(1,1,w,p[ps+1],x-1,cf);
else upd(1,1,w,x,p[ps+1]-1,cf);
}
int swap_seats(int x,int y)
{
if(x>y)swap(x,y);
x++,y++;
add(x,c[x],-1);
add(y,c[y],-1);
swap(p[c[x]],p[c[y]]);
swap(c[x],c[y]);
add(x,c[x],1);
add(y,c[y],1);
pair<int,int>pr=qry(1,1,w,1,w);
if(pr.fr==2)return pr.sc;
return 0;
}
# | 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... |