Submission #378745

#TimeUsernameProblemLanguageResultExecution timeMemory
378745autumn_eelRectangles (IOI19_rect)C++14
37 / 100
5110 ms164204 KiB
#include "rect.h"
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<int(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

struct BIT{
	vector<int>bit;

	BIT(int n){
		bit=vector<int>(n+10);
	}

	void add(int k,int x){
		k++;
		while(k<bit.size()){
			bit[k]+=x;
			k+=k&-k;
		}
	}

	int sum(int k){
		k++;
		int res=0;
		while(k){
			res+=bit[k];
			k-=k&-k;
		}
		return res;
	}
};

struct st{
	int a,b,c;
};

struct Segtree{
	int N;
	vector<int>dat;
	function<int(int,int)>f;
	int I;

	Segtree(){}
	Segtree(int n,int ini,function<int(int,int)>f):f(f),I(ini){
		N=1;while(N<n)N<<=1;
		dat=vector<int>(2*N,ini);
	}

	void update(int k,int x){
		k+=N;dat[k]=x;
		while(k>1){
			k>>=1;
			dat[k]=f(dat[k*2],dat[k*2+1]);
		}
	}

	int query(int l,int r){ // [l,r]
		r++;
		int res=I;
		for(l+=N,r+=N;l<r;l>>=1,r>>=1){
			if(l&1)res=f(res,dat[l++]);
			if(r&1)res=f(res,dat[--r]);
		}
		return res;
	}
};

static int r[3000][3000],l[3000][3000];
Segtree L[3000],R[3000],M[3000];

long long count_rectangles(std::vector<std::vector<int>> a) {
	int n=a.size(),m=a[0].size();
	rep(i,n){
		stack<P>sa;
		for(int j=m-1;j>=0;j--){
			while(!sa.empty()&&sa.top().first<a[i][j])sa.pop();
			r[i][j]=(sa.empty()?m:sa.top().second)-1;
			sa.push(P(a[i][j],j));
		}
		while(!sa.empty())sa.pop();
		rep(j,m){
			while(!sa.empty()&&sa.top().first<a[i][j])sa.pop();
			l[i][j]=(sa.empty()?-1:sa.top().second)+1;
			sa.push(P(a[i][j],j));
		}
	}

	rep(i,m){
		R[i]=Segtree(n,m-1,[](int a,int b){return min(a,b);});
		L[i]=Segtree(n,0,[](int a,int b){return max(a,b);});
		M[i]=Segtree(n,0,[](int a,int b){return max(a,b);});
	}

	rep(i,m){
		rep(j,n){
			R[i].update(j,r[j][i]);
			L[i].update(j,l[j][i]);
			M[i].update(j,a[j][i]);
		}
	}
	
	ll ans=0;
	for(int sx=0;sx<n;sx++){
		for(int gx=sx+2;gx<n;gx++){
			BIT bit(m);
			vector<st>v;
			int K=0;
			rep(j,m){
				int end=max(L[j].query(sx+1,gx-1)-1,K);
				if(end<=j-2){
					v.push_back({j-2,j,1});
					v.push_back({end-1,j,-1});
				}
				if(M[j].query(sx+1,gx-1)>=min(a[sx][j],a[gx][j])){
					K=j;
				}
			}
			sort(v.begin(),v.end(),[](st a,st b){return a.a>b.a;});
			int s=0;
			for(int j=m-1;j>=0;j--){
				while(s<v.size()&&v[s].a>=j){
					bit.add(v[s].b,v[s].c);
					s++;
				}
				ans+=bit.sum(R[j].query(sx+1,gx-1)+1);
			}
		}
	}
	return ans;
}

Compilation message (stderr)

rect.cpp: In member function 'void BIT::add(int, int)':
rect.cpp:17:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |   while(k<bit.size()){
      |         ~^~~~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:122:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     while(s<v.size()&&v[s].a>=j){
      |           ~^~~~~~~~~
#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...