Submission #583561

#TimeUsernameProblemLanguageResultExecution timeMemory
583561LucppRectangles (IOI19_rect)C++17
72 / 100
3312 ms1048576 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; #define sz(x) ((int)(x).size()) typedef long long ll; vector<int> fen; void add(int i, int x){ for(i++; i < sz(fen); i+=i&-i) fen[i] += x; } int qry(int i){ int ans = 0; for(i++; i > 0; i-=i&-i) ans += fen[i]; return ans; } int qry(int i, int j){ if(i > 0) return qry(j)-qry(i-1); else return qry(j); } long long count_rectangles(vector<vector<int>> a) { int n = sz(a), m = sz(a[0]); vector<vector<int>> r1(n, vector<int>(m, -1)), r2(n, vector<int>(m, -1)); for(int i = 1; i < n-1; i++){ stack<pair<int, int>> st({{a[i][0], 0}}); for(int j = 1; j < m; j++){ while(!st.empty() && a[i][j] > st.top().first){ auto [h, k] = st.top(); st.pop(); r2[i][k] = j-1; } if(!st.empty()) { if(st.top().first > a[i][j]) r1[i][j] = st.top().second+1; else r1[i][j] = r1[i][st.top().second]; } st.emplace(a[i][j], j); } } vector<vector<int>> c1(n, vector<int>(m, -1)), c2(n, vector<int>(m, -1)); for(int j = 1; j < m-1; j++){ stack<pair<int, int>> st({{a[0][j], 0}}); for(int i = 1; i < n; i++){ while(!st.empty() && a[i][j] > st.top().first){ auto [h, k] = st.top(); st.pop(); c2[k][j] = i-1; } if(!st.empty()){ if(st.top().first > a[i][j]) c1[i][j] = st.top().second+1; else c1[i][j] = c1[st.top().second][j]; } st.emplace(a[i][j], i); } } int mx = max(n, m); fen.resize(mx+1); vector<bool> valid; vector<tuple<short, short, short, short>> queries; vector<vector<int>> byI(mx*mx), byJ(mx*mx); unordered_set<ll> noDupl; vector<vector<int>> goodI(mx*mx), goodJ(mx*mx); int q = 0; for(int i = 1; i < n-1; i++){ for(int j = 1; j < m-1; j++){ // cerr << "\n" << i << " " << j << " " << r1[i][j] << " " << r2[i][j] << " " << c1[i][j] << " " << c2[i][j] << "\n"; if(r1[i][j] != -1 && r2[i][j] != -1 && c1[i][j] != -1 && c2[i][j] != -1){ int x = c1[i][j]*mx + c2[i][j], y = r1[i][j]*mx + r2[i][j]; goodJ[x].push_back(j); goodI[y].push_back(i); ll t = (ll)x*mx*mx+y; if(noDupl.count(t)) continue; noDupl.insert(t); valid.push_back(true); queries.emplace_back(r1[i][j], r2[i][j], c1[i][j], c2[i][j]); byI[x].push_back(q); byJ[y].push_back(q); q++; } } } for(int id = 1; id < mx*mx; id++){ sort(goodJ[id].begin(), goodJ[id].end()); int last = -1; for(int j : goodJ[id]){ if(j != last) add(j, 1); last = j; } for(int ind : byI[id]){ auto [l1, r1, l2, r2] = queries[ind]; if(qry(l1, r1) != r1-l1+1) valid[ind] = false; } last = -1; for(int j : goodJ[id]){ if(j != last) add(j, -1); last = j; } } for(int id = 1; id < mx*mx; id++){ sort(goodI[id].begin(), goodI[id].end()); int last = -1; for(int i : goodI[id]){ if(i != last) add(i, 1); last = i; } for(int ind : byJ[id]){ auto [l1, r1, l2, r2] = queries[ind]; if(qry(l2, r2) != r2-l2+1) valid[ind] = false; } last = -1; for(int i : goodI[id]){ if(i != last) add(i, -1); last = i; } } int ans = 0; for(bool b : valid) ans += b; 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...