Submission #313646

# Submission time Handle Problem Language Result Execution time Memory
313646 2020-10-16T13:49:10 Z nathanlee726 Strah (COCI18_strah) C++14
110 / 110
592 ms 44840 KB
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
 
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
 
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod; 
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
int tab[2005][2005],wd1[2005];
queue<int> q[2005],wd[2005];
signed main(){
	IOS();
	int n,m;
	cin>>n>>m;
	F(n){
		Fi(j,m){
			char c;
			cin>>c;
			tab[i][j]=(c=='#');
		}
	}
	Fi(j,m)F(n){
		if(tab[i][j])q[j].push(i);
	}
	F(m){
		if(!q[i].empty()){
			wd[q[i].front()].push(i);
			wd1[i]=q[i].front();
			q[i].pop();
		}
		else wd1[i]=-1;
	}
	int ans=0;
	F(n){
		stack<pii> tq1,tq2;
		vector<pii> tv(m);
		tq1.push(mp(-1,-1));
		tq2.push(mp(m,-1));
		for(int j=0;j<m;j++){
			if(wd1[j]!=-1){
				while(tq1.top().Y>wd1[j])tq1.pop();
				tv[j].X=(j-tq1.top().X-1);
				tq1.push(mp(j,wd1[j]));
			}
		}
		for(int j=m-1;j>=0;j--){
			if(wd1[j]!=-1){
				while(tq2.top().Y>=wd1[j])tq2.pop();
				//cout<<tq2.top().X<<" "<<j<<endl;
				tv[j].Y=(tq2.top().X-j-1);
				tq2.push(mp(j,wd1[j]));
			}
		}
		int s=(m)*(m+1)*(m+2)/6;
		for(int j=i;j<n;j++){
			queue<int> twd=wd[j];
			while(!twd.empty()){
				int k=twd.front();
				twd.pop();
				int tt=tv[k].X+tv[k].Y+1,t1=tv[k].X,t2=tv[k].Y;
				//cout<<t1<<" "<<t2<<" "<<tt<<endl;
				s-=(tt)*(tt+1)*(tt+2)/6;
				s+=(t1)*(t1+1)*(t1+2)/6;
				s+=(t2)*(t2+1)*(t2+2)/6;
			}
			//cout<<s<<endl;
			ans+=s*(j-i+1);	
		}
		while(!wd[i].empty()){
			int j=wd[i].front();
			wd[i].pop();
			if(!q[j].empty()){
				wd[q[j].front()].push(j);
				wd1[j]=q[j].front();
				q[j].pop();
			}
			else wd1[j]=-1;
		}
	}
	cout<<ans<<endl;
return 0;
}
/*
i*(n+1-i)
(n(n+1)(n+2))/6
2 3
.#.
..#
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4992 KB Output is correct
2 Correct 13 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4992 KB Output is correct
2 Correct 13 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 4992 KB Output is correct
2 Correct 13 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 15864 KB Output is correct
2 Correct 300 ms 30072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 317 ms 27640 KB Output is correct
2 Correct 476 ms 38136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 16504 KB Output is correct
2 Correct 329 ms 31736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 12800 KB Output is correct
2 Correct 380 ms 32584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 424 ms 34424 KB Output is correct
2 Correct 592 ms 44840 KB Output is correct