# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
745706 | bgnbvnbv | Bob (COCI14_bob) | C++14 | 19 ms | 24008 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
vector<int> cc;
deque<int> dq;
ll h[1005][1005];
ll l[1005],r[1005];
ll n,m;
ll process(ll x)
{
dq.clear();
dq.pb(0);
for(int i=0;i<cc.size();i++)
{
if(i>0&&cc[i]==cc[i-1]+1)
{
while(dq.size()>0&&h[x][cc[i]]<h[x][dq.back()])
{
dq.pop_back();
}
if(dq.size()==0) l[i]=0;
else l[i]=dq.back();
}
else
{
dq.clear();
dq.pb(cc[i]-1);
l[i]=cc[i]-1;
}
dq.pb(cc[i]);
}
dq.clear();
dq.pb(m+1);
ll sum=0;
for(int i=(int)cc.size()-1;i>=0;i--)
{
if(i<cc.size()-1&&cc[i]==cc[i+1]-1)
{
while(dq.size()>0&&h[x][cc[i]]<=h[x][dq.back()]) dq.pop_back();
if(dq.size()==0) r[i]=m+1;
else r[i]=dq.back();
}
else
{
dq.clear();
dq.pb(cc[i]+1);
r[i]=dq.back();
}
dq.pb(cc[i]);
sum+=h[x][cc[i]]*(r[i]-cc[i])*(cc[i]-l[i]);
}
return sum;
}
ll a[1005][1005];
vector<ll>val;
vector<pli>vec[1005*1000];
void solve()
{
cin >> n >> m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin >> a[i][j];
val.pb(a[i][j]);
h[i][j]=0;
}
}
sort(val.begin(),val.end());
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]=lower_bound(val.begin(),val.end(),a[i][j])-val.begin();
vec[a[i][j]].pb({i,j});
}
}
ll ans=0;
ll cur;
for(int v=0;v<val.size();v++)
{
if(vec[v].size()==0) continue;
cur=ans;
for(int i=0;i<vec[v].size();i++)
{
cc.clear();
ll j=i;
while(j<vec[v].size()&&vec[v][j].fi==vec[v][i].fi)
{
ll x=vec[v][j].fi;
ll y=vec[v][j].se;
h[x][y]=h[x-1][y]+1;
cc.pb(y);
j++;
}
i=j-1;
ans+=process(vec[v][i].fi);
}
for(auto zz:vec[v])
{
h[zz.fi][zz.se]=0;
}
}
cout << ans;
}
int main()
{
fastio
freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |