답안 #364840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
364840 2021-02-10T08:49:06 Z tengiz05 Maxcomp (info1cup18_maxcomp) C++17
60 / 100
500 ms 4204 KB
#include <bits/stdc++.h>
using namespace std;
template <class T> inline void read(T &x) {
	x = 0; register char ch = getchar();
	for (; ch<'0' || ch>'9';) ch = getchar();
	for (; ch>='0' && ch<='9';) x = x*10 + ch-'0', ch = getchar();
}

//~ #define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
template<class T> bool chmin(T& a, const T& b) {return a>b? a=b, true:false;}
template<class T> bool chmax(T& a, const T& b) {return a<b? a=b, true:false;}
const int mod = 1e9+7, N = 1005;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
int n, m, k;
int a[N][N];
struct segtree{
	vector<int> t,lz;
	int sz;
	void init(int row){
		sz=1;
		while(sz < m)sz<<=1;
		t.assign(sz*2,-mod);
		lz.assign(sz*2,0);
		for(int j=0;j<m;j++){
			t[j+sz] = a[row][j] - j - 1;
		}
		for(int j=sz-1;j>0;j--){
			t[j] = max(t[j*2],t[j*2+1]);
		}
	}
	inline void push(int x){
		if(lz[x] == 0)return;
		t[x] += lz[x];
		if(x < sz){
			lz[x*2] += lz[x];
			lz[x*2+1] += lz[x];
		}lz[x] = 0;
		return;
	}
	void update(int l, int r, int L, int R, int x, int val){
		push(x);
		if(R <= l || L >= r)return;
		if(L >= l && R <= r){
			lz[x] += val;
			push(x);
			return;
		}int mid = (L+R)/2;
		update(l,r,L,mid,x*2,val);
		update(l,r,mid,R,x*2+1,val);
		t[x] = max(t[x*2], t[x*2+1]);
	}
	inline void chm(int pos, int val){
		update(pos,pos+1,0,sz,1,0);
		pos += sz;
		for(chmax(t[pos],val);pos>1;pos>>=1)t[pos>>1] = max(t[pos],t[pos^1]);
		return;
	}
	inline void update(int l, int r, int val){
		update(l,r,0,sz,1,val);
		return;
	}
	int get(){
		return t[1];
	}
};
void solve(int test_case){
	int i, j;
	cin >> n >> m;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			read(a[i][j]);
		}
	}
	int ans = -2;
	{
		segtree seg;
		seg.init(0);
		//~ for(i=0;i<m;i++){
			//~ seg.update(0,m,0);
			//~ cout << seg.t[i+seg.sz] << ' ';
		//~ }cout << '\n';
		for(i=0;i<n;i++){
			//~ for(j=0;j<m;j++){
				//~ seg.update(j,j+1,0);
				//~ cout << seg.t[j+seg.sz] << ' ';
			//~ }cout << '\n';
			chmax(ans, seg.get() - a[i][0]);
			for(j=1;j<m;j++){
				seg.update(0,j,-1);
				seg.update(j,m,1);
				chmax(ans, seg.get()-a[i][j]);
			}
			for(j=m-1;j>0;j--){
				seg.update(0,j,1);
				seg.update(j,m,-1);
			}
			seg.update(0,m,-1);
			for(j=0;j<m;j++){
				seg.chm(j,a[i+1][j]-j-1);
			}
		}
	}
	for(int l=0,r=n-1;l<r;l++,r--){
		for(j=0;j<m;j++){
			swap(a[l][j],a[r][j]);
		}
	}
	
	{
		segtree seg;
		seg.init(0);
		//~ for(i=0;i<m;i++){
			//~ seg.update(0,m,0);
			//~ cout << seg.t[i+seg.sz] << ' ';
		//~ }cout << '\n';
		for(i=0;i<n;i++){
			//~ for(j=0;j<m;j++){
				//~ seg.update(j,j+1,0);
				//~ cout << seg.t[j+seg.sz] << ' ';
			//~ }cout << '\n';
			chmax(ans, seg.get() - a[i][0]);
			for(j=1;j<m;j++){
				seg.update(0,j,-1);
				seg.update(j,m,1);
				chmax(ans, seg.get()-a[i][j]);
			}
			for(j=m-1;j>0;j--){
				seg.update(0,j,1);
				seg.update(j,m,-1);
			}
			seg.update(0,m,-1);
			for(j=0;j<m;j++){
				seg.chm(j,a[i+1][j]-j-1);
			}
		}
	}
	//~ for(i=0;i<n;i++){
		//~ for(j=0;j<m;j++){
			//~ cout << a[i][j] << ' ';
		//~ }cout << '\n';
	//~ }
	cout << ans << '\n';
	return;
}
/*

2 3
5 7 5
2 4 3

*/
signed main(){
	//~ FASTIO;
//~ #define MULTITEST 1
#if MULTITEST
	int _T;
	cin >> _T;
	for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
		solve(T_CASE);
#else
	solve(1);
#endif
	return 0;
}




Compilation message

maxcomp.cpp: In function 'void read(T&)':
maxcomp.cpp:4:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    4 |  x = 0; register char ch = getchar();
      |                       ^~
maxcomp.cpp: In instantiation of 'void read(T&) [with T = int]':
maxcomp.cpp:79:16:   required from here
maxcomp.cpp:4:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 3 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 3 ms 492 KB Output is correct
10 Correct 3 ms 492 KB Output is correct
11 Correct 3 ms 492 KB Output is correct
12 Correct 4 ms 492 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
14 Correct 3 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 368 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 3 ms 364 KB Output is correct
10 Correct 3 ms 364 KB Output is correct
11 Correct 3 ms 364 KB Output is correct
12 Correct 3 ms 492 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
14 Correct 3 ms 492 KB Output is correct
15 Correct 4 ms 492 KB Output is correct
16 Correct 3 ms 492 KB Output is correct
17 Correct 3 ms 492 KB Output is correct
18 Execution timed out 1099 ms 4204 KB Time limit exceeded
19 Halted 0 ms 0 KB -