#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
//#define int long long
using namespace std;
const int mod=1e9+7,mxn=1e6+5,Mxn=1e8,inf=1e9,minf=-1e9,lg=30;
int n,m,r,k,p;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
struct seg{
vector<int>v;
void init(int n){v.resize(2*n+4,minf);}
void update(int pos,int val,int n){
pos+=n+1;
v[pos]=val;
for(int i=pos;i>0;i>>=1)v[i>>1]=max(v[i],v[i^1]);
}
int qry(int l,int r,int n){
int ans=minf;
for(l+=n+1,r+=n+1;l<=r;l>>=1,r>>=1){
if(l&1)ans=max(ans,v[l++]);
if(!(r&1))ans=max(ans,v[r--]);
}
return ans;
}
}t[2][mxn+10];
int get1(int d,int h,int n,int p,int last){
int l=last+1,r=n,mid=0,ans=inf;
while(l<=r){
mid=l+(r-l)/2;
if(t[d][p].qry(last+1,mid,n)>=h)ans=min(ans,mid),r=mid-1;
else l=mid+1;
}
return ans;
}
int get2(int d,int h,int n,int p,int last){
int l=1,r=last-1,mid=0,ans=minf;
while(l<=r){
mid=l+(r-l)/2;
if(t[d][p].qry(mid,last-1,n)>=h)ans=max(ans,mid),l=mid+1;
else r=mid-1;
}
return ans;
}
int32_t main(){
fastio
cin>>n>>m>>r>>k>>p;
vector<vector<int>>grid(n+1,vector<int>(m+1,0));
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>grid[i][j];
for(int i=1;i<=n;i++){
t[0][i].init(m);
for(int j=1;j<=m;j++)t[0][i].v[j+m+1]=grid[i][j];
for(int j=m+1;j>0;j--)t[0][i].v[j]=max(t[0][i].v[j<<1],t[0][i].v[(j<<1)^1]);
}
for(int j=1;j<=m;j++){
t[1][j].init(n);
for(int i=1;i<=n;i++)t[1][j].v[i+n+1]=grid[i][j];
for(int i=n+1;i>0;i--)t[1][j].v[i]=max(t[1][j].v[i<<1],t[1][j].v[(i<<1)^1]);
}
int d=0,sz=0,cnt,last,x,y;
bool yes=false;
while(k--){
char g;cin>>g;
int p,h;cin>>p>>h;
if(g=='W'||g=='E')d=0,sz=m;
else d=1,sz=n;
if(g=='W'||g=='N')yes=true,last=0;
else yes=false,last=sz+1;
cnt=r;
while(cnt--){
last=((yes)?get1(d,h,sz,p,last):get2(d,h,sz,p,last));
if(last==inf||last==minf)break;
x=p,y=last;
if(d)swap(x,y);
grid[x][y]--;
t[0][x].update(y,grid[x][y],m);
t[1][y].update(x,grid[x][y],n);
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)grid[i][j]+=grid[i][j-1]+grid[i-1][j]-grid[i-1][j-1];
int ans=0;
for(int i=p;i<=n;i++)for(int j=p;j<=m;j++)ans=max(ans,grid[i][j]-grid[i-p][j]-grid[i][j-p]+grid[i-p][j-p]);
cout<<ans;
}
Compilation message
ufo.cpp:17:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
17 | #pragma GCC optimize ("03,unroll-lopps")
| ^
ufo.cpp:22:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
22 | void setIO(string name){
| ^
ufo.cpp:29:20: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
29 | void init(int n){v.resize(2*n+4,minf);}
| ^
ufo.cpp:30:38: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
30 | void update(int pos,int val,int n){
| ^
ufo.cpp:35:30: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
35 | int qry(int l,int r,int n){
| ^
ufo.cpp:44:42: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
44 | int get1(int d,int h,int n,int p,int last){
| ^
ufo.cpp:53:42: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
53 | int get2(int d,int h,int n,int p,int last){
| ^
ufo.cpp:62:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
62 | int32_t main(){
| ^
ufo.cpp: In function 'void setIO(std::string)':
ufo.cpp:24:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ufo.cpp:25:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
47192 KB |
Output is correct |
2 |
Correct |
15 ms |
47192 KB |
Output is correct |
3 |
Correct |
14 ms |
47452 KB |
Output is correct |
4 |
Correct |
30 ms |
47452 KB |
Output is correct |
5 |
Correct |
118 ms |
48556 KB |
Output is correct |
6 |
Correct |
187 ms |
57036 KB |
Output is correct |
7 |
Correct |
280 ms |
70548 KB |
Output is correct |
8 |
Correct |
238 ms |
70544 KB |
Output is correct |
9 |
Correct |
748 ms |
68944 KB |
Output is correct |
10 |
Correct |
1138 ms |
70544 KB |
Output is correct |
11 |
Correct |
753 ms |
70612 KB |
Output is correct |
12 |
Correct |
1259 ms |
70540 KB |
Output is correct |
13 |
Correct |
1311 ms |
74840 KB |
Output is correct |
14 |
Correct |
889 ms |
70468 KB |
Output is correct |
15 |
Correct |
1812 ms |
70544 KB |
Output is correct |
16 |
Correct |
298 ms |
70264 KB |
Output is correct |
17 |
Execution timed out |
2066 ms |
74708 KB |
Time limit exceeded |
18 |
Correct |
293 ms |
71852 KB |
Output is correct |
19 |
Correct |
608 ms |
72484 KB |
Output is correct |
20 |
Correct |
1432 ms |
94396 KB |
Output is correct |