제출 #1227063

#제출 시각아이디문제언어결과실행 시간메모리
1227063LemserRectangles (IOI19_rect)C++20
10 / 100
538 ms430248 KiB
#include "rect.h"

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
 
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
using vlld = vector<lld>;
 
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
 
#define fore(i, l, r) for (auto i = l; i < r; i++)
#define fo(i, n) fore (i, 0, n)
#define forex(i, r, l) for (auto i = r-1; i >= l; i--)
#define ffo(i, n) forex (i, n, 0)
 
bool cmin(ll &a, ll b) { if (b < a) { a=b; return 1; } return 0; }
bool cmax(ll &a, ll b) { if (b > a) { a=b; return 1; } return 0; }
 
const ll INF = 1e18;
const int N = 2505;

struct Fenwick {
	vll ft;
	ll n;
	
	Fenwick () {  }
	Fenwick (ll n): n(n+2), ft (n+4, 0) {  }
 
	void update (ll i, ll v) {
		i++;
		for (; i <= n; i += lsb(i)) 
			ft[i] += v;
	}
 
	ll query (ll i) {
		i++;
		ll r = 0;
		for (; i > 0; i -= lsb(i))
			r += ft[i];
		return r;
	}
 
	void update (ll l, ll r, ll v) { update(l, +v); update(r+1, -v); }
	ll query (ll l, ll r) { if (l>r) return 0ll; return query(r) - query(l-1); }

};

vector<array<int, 3>> updates[N][N], qrys[N][N];


ll count_rectangles(vector<vector<int> > a) {
	ll n = a.size(), m = a[0].size(), ans = 0;
	vector<vll> down(n, vll(m)), up(n, vll(m));
	fo (j, m) {
		stack<ll> stk;
		stk.push(-1);
		fo (i, n) {
			while (stk.top() != -1 && a[stk.top()][j] < a[i][j]) stk.pop();
			up[i][j] = stk.top();
			stk.push(i);
		}
		while (stk.size()) stk.pop();
		stk.push(n);
		ffo (i, n) {
			while (stk.top() != n && a[stk.top()][j] < a[i][j]) stk.pop();
			down[i][j] = stk.top();
			stk.push(i);
		}
	}
	forex (i, n-1, 1) {
		vll L(m), R(m); 
		stack<ll> stk;
		stk.push(-1);
		fo (j, m) {
			while (stk.top() != -1 && a[i][stk.top()] <= a[i][j]) stk.pop();
			L[j] = stk.top() + 1;
			stk.push(j);
		}
		while (stk.size()) stk.pop();
		stk.push(m);
		ffo (j, m) {
			while (stk.top() != m && a[i][stk.top()] < a[i][j]) stk.pop();
			R[j] = stk.top() - 1;
			stk.push(j);
		}
		fo (j, m) {
			if (L[j] < 1 || m-2 < R[j] || a[i][R[j]+1] == a[i][j]) continue;
			ll mx;
			if (updates[L[j]][R[j]].empty() || updates[L[j]][R[j]].back()[0] > i+1) {
				mx = 0;
				fore (j2, L[j], R[j]+1) 
					mx = max(mx, up[i+1][j2]);
				updates[L[j]][R[j]].pb(array<int, 3>{int(mx+1), -1, int(i+1)});
				updates[L[j]][R[j]].pb(array<int, 3>{int(i+1), +1, int(i+1)});
			}
			mx = 0;
			fore (j2, L[j], R[j]+1) 
				mx = max(mx, up[i][j2]);
			updates[L[j]][R[j]].pb(array<int, 3>{int(mx+1), -1, int(i)});
			updates[L[j]][R[j]].pb(array<int, 3>{int(i), +1, int(i)});
			ll k = n-1;
			fore (j2, L[j], R[j]+1) k = min(k, down[i-1][j2]);
			qrys[L[j]][R[j]].pb(array<int, 3>{int(i+1), int(k), -1});

		}
	}
	Fenwick ft(n+1);
	fo (l, m) {
		fo (r, m) {
			if (qrys[l][r].empty()) continue;
			sort(all(updates[l][r]));
			int ls = n+5;
			for (auto [lb, rb, _]: qrys[l][r]) {
				while (updates[l][r].size() && updates[l][r].back()[0] >= lb) {
					ft.update(updates[l][r].back()[2], updates[l][r].back()[1]);
					updates[l][r].pop_back();
				}
				int i = lb-1;
				if (i+1 != ls ) ls = i;
				ans += ft.query(lb, min(rb, ls+1));
			}
			while (updates[l][r].size()) {
				ft.update(updates[l][r].back()[2], updates[l][r].back()[1]);
				updates[l][r].pop_back();
			}
		}
	}
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...