이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1<<29;
const ll inf = 1ll<<60;
const ll mod = 1e9+7;
void GG(){cout<<"-1\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b) % mo;
}
const int MX = 2505;
vector<pii> hof[MX][MX], cof[MX][MX];
#define REP(i,n) for (int i = 0; i<n; ++i)
#define FOR(i,a,b) for (int i = a; i<b; ++i)
vector<int> g[MX][MX];
int s1[MX*2], s2[MX*2];
int sit = 0;
void go(vector<pii> up[MX][MX], vector<vector<int> > a) {
clock_t t2 = clock();
int n = a.size(), m = a[0].size();
REP(i,m) REP(j,m) g[i][j] . clear();
REP(i,n) {
vector<int> &b = a[i];
sit = 0;
s1[sit] = b[0]; s2[sit] = 0; ++ sit;
FOR(j,1,m) {
while (sit && s1[sit-1] < b[j]) {
g[s2[--sit]][j].pb(i);
}
if (sit) {
g[s2[sit-1]][j].pb(i);
}
while (sit && s1[sit-1] == b[j]) --sit;
s1[sit] = b[j];
s2[sit] = j;
++sit;
}
}
bug((clock() - t2) / (double) CLOCKS_PER_SEC);
t2 = clock();
REP(i,m-1) FOR(j,i+2,m) {
if (SZ(g[i][j]) == 0) continue;
int first = 0;
int sg = SZ(g[i][j]);
FOR(it,1, sg + 1) {
if ( g[i][j][it] != g[i][j][it-1]+1 ) {
FOR(nat, first, it) if (g[i][j][nat] -1 >= 0) up[g[i][j][nat]-1][i].push_back({g[i][j][it-1]+1,j});
first = it;
}
}
FOR(nat, first, sg ) if (g[i][j][nat] -1 >= 0) up[g[i][j][nat]-1][i].push_back({g[i][j][sg -1]+1,j});
// first = it;
}
bug((clock() - t2) / (double) CLOCKS_PER_SEC);
}
void go2(vector<pii> up[MX][MX], vector<vector<int> > a) {
clock_t t2 = clock();
int n = a.size(), m = a[0].size();
REP(i,m) REP(j,m) g[i][j] . clear();
REP(i,n) {
vector<int> &b = a[i];
sit = 0;
s1[sit] = b[0]; s2[sit] = 0; ++ sit;
FOR(j,1,m) {
while (sit && s1[sit-1] < b[j]) {
g[s2[--sit]][j].pb(i);
}
if (sit) {
g[s2[sit-1]][j].pb(i);
}
while (sit && s1[sit-1] == b[j]) --sit;
s1[sit] = b[j];
s2[sit] = j;
++sit;
}
}
bug((clock() - t2) / (double) CLOCKS_PER_SEC);
t2 = clock();
int cnt = 0;
REP(i,m-1) FOR(j,i+2,m) {
if (SZ(g[i][j]) == 0) continue;
int first = 0;
int sg = SZ(g[i][j]);
FOR(it,1, sg + 1) {
if ( g[i][j][it] != g[i][j][it-1]+1 ) {
FOR(nat, first, it) if (g[i][j][nat] -1 >= 0) up[i][g[i][j][nat]-1].push_back({j,g[i][j][it-1]+1}), cnt++;
first = it;
}
}
FOR(nat, first, sg ) if (g[i][j][nat] -1 >= 0) up[i][g[i][j][nat]-1].push_back({j,g[i][j][sg -1]+1}), cnt++;
// first = it;
}
bug(cnt);
bug((clock() - t2) / (double) CLOCKS_PER_SEC);
}
int s[MX];
int QU(int e) {
int re = 0;
for (e++; e>0; e-=e&-e) re += s[e];
return re;
}
void MO(int e, int v) {
for (e++; e<MX; e+=e&-e) s[e] += v;
}
ll count_rectangles(vector<vector<int> > a){
clock_t tt = clock();
go(hof,a);
int n = a.size(), m = a[0].size();
//#ifdef BALBIT
// REP(i,n) REP(j,m) {
// if (SZ(hof[i][j]) == 0) continue;
// bug(i,j);
// for (pii x : hof[i][j]) {
// cerr<<x.f<<' '<<x.s<<"\n";
// }
// cerr<<endl;
// }
//#endif
vector<vector<int> > aT(m);
REP(j,m) {
REP(i,n) {
aT[j].pb(a[i][j]);
}
}
go2(cof, aT);
//#ifdef BALBIT
// REP(i,n) REP(j,m) {
// if (SZ(cof[i][j]) == 0) continue;
// bug(i,j, j,i);
// for (pii x : cof[j][i]) {
// cerr<<x.f<<' '<<x.s<<"\n";
// }
// cerr<<endl;
// }
//#endif
bug((clock() - tt) / (double) CLOCKS_PER_SEC);
tt = clock();
ll re = 0;
REP(i,n-2) REP(j,m-2) {
if (hof[i][j].size() == 0 || cof[i][j].size() == 0) continue;
vector<pii> &ud = hof[i][j], &lr = cof[i][j];
sort(ALL(ud), greater<pii> ());
sort(ALL(lr), greater<pii> ());
int it = 0;
for (pii & q : lr) {
while (it < SZ(ud) && ud[it].f >= q.f) {
MO(ud[it].s,1);
++it;
}
re += QU(q.s);
// bug(q.s,it,QU(q.s));
}
REP(jt, it) {
MO(ud[jt].s,-1);
}
}
bug((clock() - tt) / (double) CLOCKS_PER_SEC);
return re;
}
#ifdef BALBIT
signed main(){
IOS();
// ll lol = count_rectangles({{0,5,5,0},{5,2,2,5},{5,3,4,5}});
// bug(lol);
// ll lol = count_rectangles({{4, 8, 7, 5, 6},
//{7, 4, 10, 3, 5},
//{9, 7, 20, 14, 2},
//{9, 14, 7, 3, 6},
//{5, 7, 5, 2, 7},
//{4, 5, 13, 5, 6}});
// bug(lol);
// ll lol = count_rectangles({{0,5,5,0},{5,1,2,5},{5,3,4,5},{0,5,5,0}});
int N = 2500, M = 2500;
vector<vector<int> > a(N);
REP(i,N) REP(j,M){
a[i].pb(rand() % 10000000);
}
bug("GO");
ll lol = count_rectangles(a);
bug(lol);
}
#endif
컴파일 시 표준 에러 (stderr) 메시지
rect.cpp: In function 'void go(std::vector<std::pair<int, int> > (*)[2505], std::vector<std::vector<int> >)':
rect.cpp:61:13: warning: variable 't2' set but not used [-Wunused-but-set-variable]
clock_t t2 = clock();
^~
rect.cpp: In function 'void go2(std::vector<std::pair<int, int> > (*)[2505], std::vector<std::vector<int> >)':
rect.cpp:100:13: warning: variable 't2' set but not used [-Wunused-but-set-variable]
clock_t t2 = clock();
^~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:151:13: warning: variable 'tt' set but not used [-Wunused-but-set-variable]
clock_t tt = clock();
^~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |