Submission #285265

#TimeUsernameProblemLanguageResultExecution timeMemory
285265tmwilliamlin168Rectangles (IOI19_rect)C++14
72 / 100
5124 ms857016 KiB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;

#define ar array

const int mxN=2.5e3;
int n, m, l[mxN], r[mxN], ft[mxN+1];
vector<ar<int, 2>> p[mxN][mxN][2];

void upd(int i, int x) {
	for(++i; i<=m; i+=i&-i)
		ft[i]+=x;
}

int qry(int i) {
	int r=0;
	for(; i; i-=i&-i)
		r+=ft[i];
	return r;
}

void gr(vector<vector<int>> a, int b) {
	int n=a.size(), m=a[0].size();
	vector<ar<int, 3>> s;
	for(int i=0; i<n; ++i) {
		for(int j=0; j<m; ++j) {
			l[j]=j-1;
			while(~l[j]&&a[i][l[j]]<=a[i][j])
				l[j]=l[l[j]];
		}
		for(int j=m-1; ~j; --j) {
			r[j]=j+1;
			while(r[j]<m&&a[i][r[j]]<=a[i][j])
				r[j]=r[r[j]];
			if(~l[j]&&r[j]<m)
				s.push_back({l[j]+1, r[j]-1, i});
		}
	}
	sort(s.begin(), s.end());
	for(int i=0, j; i<s.size(); i=j+1) {
		for(j=i; j+1<s.size()&&s[j+1][0]==s[i][0]&&s[j+1][1]==s[i][1]&&s[j+1][2]<=s[j][2]+1; ++j);
		int l1=s[i][0], r1=s[i][1], l2=s[i][2], r2=s[j][2], x2=r2, y2=r1;
//		if(b&&x2==4&&y2==2)
//			cout << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
		if(b)
			swap(x2, y2);
		for(int k=l2; k<=r2; ++k) {
			int x=k, y=l1;
			if(b)
				swap(x, y);
			p[x][y][b].push_back({-x2, y2});
		}
	}
}

long long count_rectangles(vector<vector<int>> a) {
	n=a.size();
	m=a[0].size();
	vector<vector<int>> b(m, vector<int>(n));
	for(int i=0; i<n; ++i)
		for(int j=0; j<m; ++j)
			b[j][i]=a[i][j];
	gr(a, 0);
	gr(b, 1);
	int ans=0;
	for(int i=0; i<n; ++i) {
		for(int j=0; j<m; ++j) {
			for(int k : {0, 1})
				sort(p[i][j][k].begin(), p[i][j][k].end());
			int d=0;
			for(int c=0; c<p[i][j][1].size(); ++c) {
				while(d<p[i][j][0].size()&&p[i][j][0][d]<=p[i][j][1][c])
					upd(p[i][j][0][d++][1], 1);
				ans+=qry(p[i][j][1][c][1]+1);
			}
			while(d)
				upd(p[i][j][0][--d][1], -1);
//			cout << i << " " << j << " " << ans << endl;
//			if(i==1&&j==3) {
//				for(int k : {0, 1})
//					for(auto a : p[i][j][k])
//						cout << k << " " << a[0] << " " << a[1] << endl;
//			}
		}
	}
	return ans;
}

Compilation message (stderr)

rect.cpp: In function 'void gr(std::vector<std::vector<int> >, int)':
rect.cpp:41:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for(int i=0, j; i<s.size(); i=j+1) {
      |                  ~^~~~~~~~~
rect.cpp:42:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for(j=i; j+1<s.size()&&s[j+1][0]==s[i][0]&&s[j+1][1]==s[i][1]&&s[j+1][2]<=s[j][2]+1; ++j);
      |            ~~~^~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |    for(int c=0; c<p[i][j][1].size(); ++c) {
      |                 ~^~~~~~~~~~~~~~~~~~
rect.cpp:73:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     while(d<p[i][j][0].size()&&p[i][j][0][d]<=p[i][j][1][c])
      |           ~^~~~~~~~~~~~~~~~~~
#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...