Submission #654116

#TimeUsernameProblemLanguageResultExecution timeMemory
654116jiahngRectangles (IOI19_rect)C++14
0 / 100
4 ms1088 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pi; typedef vector <int> vi; typedef vector <pi> vpi; typedef pair<pi, ll> pii; typedef pair <pi,pi> pipi; typedef set <ll> si; typedef long double ld; #define f first #define s second #define mp make_pair #define FOR(i,s,e) for(int i=s;i<=int(e);++i) #define DEC(i,s,e) for(int i=s;i>=int(e);--i) #define pb push_back #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) #define aFOR(i,x) for (auto i: x) #define mem(x,i) memset(x,i,sizeof x) #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define maxn 2501 #define INF (ll)1e9 #define MOD 1000000007 typedef pair <vi, int> pvi; typedef pair <int,pi> ipi; typedef vector <pii> vpii; int N,M; set <pi> hpairs[maxn], vpairs[maxn]; bool intersect(pi x, pi y){ return max(x.f, y.f) <= min(x.s, y.s); } struct FW{ int n; unordered_map <int,int> fw; FW(){ n = max(N, M); } void upd(int p,int v){ p++; for (int i = p; i <= n; i += i & (-i)) fw[i] += v; } int qry(int p){ p++; int ans = 0; for (int i = p; i > 0; i -= i & (-i)) ans += fw[i]; return ans; } }; struct Group{ set <pi> st, st2; FW fw; Group(){ fw = FW(); } void insert(pi x){ st.insert(x); st2.insert(pi(x.s, x.f)); fw.upd(x.f, 1); } int qry(pi x){ auto it = st.upper_bound(pi(x.f, INF)); auto it2 = st2.lower_bound(pi(x.s, -1)); if (it == st.begin() || it2 == st2.end()) return 0; return fw.qry(min((--it)->f, it2->s)); } }; struct RangeContainer{ set <pii> st; vector <Group> grps; RangeContainer(){ } void insert(pi x){ int grp = 0; auto it = st.lower_bound(pii(x, -1)); if (it != st.end() && intersect(x, it->f)){ grp = it->s; }else if (it != st.begin()){ it--; if (intersect(x, it->f)) grp = it->s; }else{ grp = grps.size(); grps.pb(Group()); } st.insert(pii(x, grp)); grps[grp].insert(x); } int qry(pi x){ int grp = 0; auto it = st.lower_bound(pii(x, -1)); if (it != st.end() && intersect(x, it->f)){ grp = it->s; }else if (it != st.begin()){ it--; if (intersect(x, it->f)) grp = it->s; }else return 0; return grps[grp].qry(x); } }; vector <pii> B[maxn]; vi C[maxn]; int co[maxn*maxn]; long long count_rectangles(std::vector<std::vector<int> > a) { N = a.size(); M = a[0].size(); FOR(i,0,N-1){ stack <pi> st; FOR(j,0,M-1){ while (!st.empty() && st.top().f < a[i][j]) st.pop(); if (!st.empty() && j - st.top().s + 1 >= 3) hpairs[i].insert(pi(st.top().s+1, j-1)); st.push(pi(a[i][j], j)); } while (!st.empty()) st.pop(); DEC(j,M-1,0){ while (!st.empty() && st.top().f < a[i][j]) st.pop(); if (!st.empty() && st.top().s - j + 1 >= 3) hpairs[i].insert(pi(j+1, st.top().s-1)); st.push(pi(a[i][j], j)); } } FOR(j,0,M-1){ stack <pi> st; FOR(i,0,N-1){ while (!st.empty() && st.top().f < a[i][j]) st.pop(); if (!st.empty() && i - st.top().s + 1 >= 3) vpairs[j].insert(pi(st.top().s+1, i-1)); st.push(pi(a[i][j], i)); } while (!st.empty()) st.pop(); DEC(i,N-1,0){ while (!st.empty() && st.top().f < a[i][j]) st.pop(); if (!st.empty() && st.top().s - i + 1 >= 3) vpairs[j].insert(pi(i+1, st.top().s-1)); st.push(pi(a[i][j], i)); } } /* cout << "HPAIRS: \n"; FOR(i,0,N-1){ cout << "ROW " << i << '\n'; aFOR(j, hpairs[i]) cout << j.f << ' ' << j.s << '\n'; } cout << "VPAIRS: \n"; FOR(i,0,M-1){ cout << "COL " << i << '\n'; aFOR(j, vpairs[i]) cout << j.f << ' ' << j.s << '\n'; } */ vector <pipi> hrects, vrects; FOR(i,0,N-1){ aFOR(j, hpairs[i]){ int a = i, b = j.f, c = N - 1, d = j.s; FOR(k,i+1,N-1){ if (hpairs[k].count(j)){ hpairs[k].erase(j); }else{ c = k - 1; break; } } hrects.pb(pipi(pi(a,b), pi(c,d))); } } FOR(i,0,M-1){ aFOR(j, vpairs[i]){ int a = j.f, b = i, c = j.s, d = M - 1; FOR(k,i+1,M-1){ if (vpairs[k].count(j)){ vpairs[k].erase(j); }else{ d = k - 1; break; } } vrects.pb(pipi(pi(a,b), pi(c,d))); } } /* cout << "HRECTS: \n"; aFOR(i, hrects) cout << i.f.f << ' ' << i.f.s << ' ' << i.s.f << ' ' << i.s.s << '\n'; cout << "VRECTS: \n"; aFOR(i, vrects) cout << i.f.f << ' ' << i.f.s << ' ' << i.s.f << ' ' << i.s.s << '\n'; */ int ans = 0; aFOR(i, hrects){ if (i.f.s > 0) B[i.f.s-1].pb(pii(pi(i.f.f, i.s.f), -1)); B[i.s.s].pb(pii(pi(i.f.f, i.s.f), 1)); } FOR(i,0,vrects.size() - 1){ C[vrects[i].f.s].pb(i); C[vrects[i].s.s].pb(i); } RangeContainer cont = RangeContainer(); FOR(i,0,M-1){ aFOR(j,C[i]){ co[j]++; if (co[j] == 2){ cont.insert(pi(vrects[j].f.f, vrects[j].s.f)); } } aFOR(j, B[i]){ ans += cont.qry(j.f) * j.s; } } 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...