题解:AT_abc179_f [ABC179F] Simplified Reversi
Expert_Dream · · 题解
对于这一道题你需要发现一些性质。
首先,行和列相交便无法往后面延伸。
对于已经有第
那么对于比他之后加进来的行,并且位于
可以发现新加入的行长度必然是至少为
因为上面有一行阻挡住了,没有列可以从他身上插进来。
反过来考虑加入的这一行,我们发现加入的这一行长度取决于在他上面的所有行的长度最大值。
上面最大的那一行可以阻挡住所有列插进来。于是新加入的这一行便可以跟他的长度一样。
但是还有一个问题,那就是这一行在最上面。那最大值不就是
显然不是。
我们又发现,这一行一直往右走总会碰到最前面的那个列然后停止。
因为最前面的那一个列不可能被已有的行所挡住。
因为已有的行都在下面,即使挡住了,对新加入的行也没有影响。
所以此时将这一个行贴紧离
于是行和列各两种情况讨论。
最大值用两个树状数组维护即可。
也不知道哪个堂食考场想要手写线段树结果挂飞了。
警钟长鸣,能树状数组就树状数组,别写线段树。
// #pragma GCC optimize(2,3,"Ofast","inline")
#include<bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define i128 __int128
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fs first
#define sc second
#define x0 _x0_
#define y1 _y1_
#define endl '\n'
#define re register
#define pb push_back
#define vi vector<int>
#define pq priority_queue
#define mem(a,x) memset((a),(x),sizeof(a))
#define debug(x) cout<<"debug:"<<x<<endl;
#define sz(s) (int)(s.size())
#define all(a) a.begin(),a.end()
#define rd(l,r) (myrand()%(r-l+1)+l)
#define print(x) {cout<<x<<endl;return;}
#define For(x,y,z) for(int x = y;x <= z;x++)
#define For_(x,y,z) for(int x = y;x >= z;x--)
#define between(x, flo, top) (flo <= x && x <= top)
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
#define em(x) x.empty()
// #define double long double
using namespace std;
// using namespace atcoder;
mt19937 myrand(time(0));
template <typename Type> void get_min(Type &w1,const Type w2) { if(w2<w1) w1=w2; }
template <typename Type> void get_max(Type &w1,const Type w2) { if(w2>w1) w1=w2; }
template <typename Type> Type up_div(Type w1,Type w2) { return (w1/w2+(w1%w2?1:0)); }//????
template <typename Type> Type gcd(Type a,Type b) {long long i=__builtin_ctz(a),j=__builtin_ctz(b),x=i>j?j:i,y;b>>=j;while(a){a>>=i;y=b-a;i=__builtin_ctz(y);b=a<b?a:b;a=y<0?-y:y;}return b<<x;}
template <typename Type> Type lcm(Type a,Type b) {return a/gcd(a,b)*b;}
template <typename Type> Type ksm(Type a,Type b) {int res=1;while(b){if(b&1) res=res*a;a=a*a;b>>=1;}return res;}
template <typename Type> Type ksm(Type a,Type b,Type mod) {int res=1;while(b){if(b&1) res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res%mod;}
namespace FastIO {template <typename T = int>inline T read() {T s=0,w=1;char c=getchar();while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}while(isdigit(c))s=(s<<1)+(s<<3)+(c^48),c=getchar();return s*w;}template <typename T>inline void read(T &s){s=0;int w=1;char c=getchar();while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}while(isdigit(c))s=(s<<1)+(s<<3)+(c^48),c=getchar();s*=w;}template <typename T, typename... Args> inline void read(T &x, Args &...args) {read(x), read(args...);}template <typename T>inline void write(T x,char ch = '\n') {if(x<0)x=-x,putchar('-');static char stk[200];int top=0;do{stk[top++]=x%10+'0',x/=10;}while(x);while(top)putchar(stk[--top]);putchar(ch);return;}template <typename T, typename... Args> inline void write(T x, Args ...args) {write(x,' '), write(args...);}template <typename Type> void Write(vector<Type> v) {for(auto it:v)write(it,' ');puts("");}}
using namespace FastIO;
#define int long long
const int mod = 998244353,inf = 0x3f3f3f3f,N = 2e5+5,M = 1e6+5,K = 3000+5;// 1000000007
const long long linf = 0x3f3f3f3f3f3f3f3f;
int n,q;
struct bit{
int t[N];
void modify(int x,int val){
for(int i = x;i <= n;i += (i & -i)){
t[i] = max(t[i],val);
}return ;
}int query(int x){
int ans=0;
for(int i = x;i > 0;i -= (i&-i)){
ans = max(ans,t[i]);
}
return ans;
}
}a,b;
int ans=0;
int mna,mnb;
void solve(){
cin >> n >> q;
ans = (n-2)*(n-2);
mna=mnb=n;
while(q--){
int op,x;
cin >> op >> x;
if(op == 1){ // shu
mna=min(mna,x);
if(a.query(x) == 0){ // 最上端
int tmp = mnb - 1;
ans -= tmp - 1;
a.modify(x,tmp);
}else{
int tmp = a.query(x);
ans -= tmp - 1;
a.modify(x,tmp);
}
}else{
mnb=min(mnb,x);
if(b.query(x) == 0){ // 最上端
int tmp = mna - 1;
ans -= tmp - 1;
b.modify(x,tmp);
}else{
int tmp = b.query(x);
ans -= tmp - 1;
b.modify(x,tmp);
}
}
}cout<<ans<<endl;
}signed main(){
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
// ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T=1;
// read(T);
For(_,1,T) solve();
return 0;
}