Submission #145524

# Submission time Handle Problem Language Result Execution time Memory
145524 2019-08-20T10:45:39 Z shashwatchandra Bob (COCI14_bob) C++17
120 / 120
598 ms 19772 KB
/*input
5 3
2 2 2
2 2 1
1 1 1
2 1 2
1 2 1
*/
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define REP(i,n)for(int i = 0;i < n;i++)
#define RE(i,n) for(int i = 1;i <= n;i++)
#define f first
#define s second
#define pii pair<int,int> 

const int N = 1001;
int a[N][N];
int eq[N][N];
int seg[4*N];
int lazy[4*N];
bool zero[4*N];
int n,m;

#define child 2*node
#define mid (l+r)/2

inline void pushdown(int node,int l,int r){
	if(zero[node]){
		seg[node] = 0;
		if(l != r){
			zero[child] = 1;
			zero[child+1] = 1;
		}
	}
	zero[node] = 0;
	seg[node] += (r-l+1)*lazy[node];
	if(l != r){
		lazy[child] += lazy[node];
		lazy[child+1] += lazy[node];
	}
	lazy[node] = 0;
	
}

void assign(int node,int l,int r,int start,int end){
	pushdown(node,l,r);
	if(start > r or l > end)return;
	if(start <= l and r <= end){
		zero[node] = 1;
		pushdown(node,l,r);
		return;
	}
	assign(child,l,mid,start,end);
	assign(child+1,mid+1,r,start,end);
	seg[node] = seg[child]+seg[child+1];
}

void add(int node,int l,int r,int start,int end){
	pushdown(node,l,r);
	if(start > r or l > end)return;
	if(start <= l and r <= end){
		lazy[node]++;
		pushdown(node,l,r);
		return;
	}
	add(child,l,mid,start,end);
	add(child+1,mid+1,r,start,end);
	seg[node] = seg[child]+seg[child+1];
}

void print(int node,int l,int r){
		cout << "FOR : " << node << " " << l << " " << r << endl;
		cout << seg[node] << " " << lazy[node] << " " << zero[node] << endl;
		if(l == r)return;
		print(child,l,mid);
		print(child+1,mid+1,r);
	
}



void solve(){
	cin >> n >> m;
	RE(i,n){
		RE(j,m)cin >> a[i][j];
	}
	RE(i,n){
		int ind = 1;
		RE(j,m){
			if(a[i][j] != a[i][ind]){
				ind = j;
			}
			eq[i][j] = ind;
		}
	}
	int ans = 0;
	RE(col,m){
		//assign(1,1,n,1,n);
		RE(row,n){
			/*//cout << "HERE : " << row << " " << col << endl;
			if(row == 4 and col == 2)print(1,1,m);
			if(a[row][col] != a[row-1][col]){
				assign(1,1,m,1,m);
				//print();
				if(row == 4 and col == 2)cout << "FINISHED\n";
			}
			if(eq[row][col] != 1)assign(1,1,m,1,eq[row][col]-1);
			add(1,1,m,eq[row][col],col);
			if(row == 4 and col == 2)print(1,1,m);
			//cout << row << " " << col << " " << seg[1] << endl;

			ans += seg[1];*/
			if(a[row][col] != a[row-1][col]){
				for(int i = 1;i <= m;i++)seg[i] = 0;
			}
			for(int i = 1;i < eq[row][col];i++)seg[i] = 0;
			for(int i = eq[row][col];i <= col;i++){
				seg[i]++;
				ans += seg[i];
			}
		}
	}
	cout << ans;
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 8824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 9080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 9336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 9400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 514 ms 19772 KB Output is correct
2 Correct 521 ms 18024 KB Output is correct
3 Correct 539 ms 17944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 520 ms 17656 KB Output is correct
2 Correct 439 ms 18024 KB Output is correct
3 Correct 547 ms 18104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 598 ms 17460 KB Output is correct
2 Correct 539 ms 18040 KB Output is correct
3 Correct 537 ms 18124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 520 ms 17152 KB Output is correct
2 Correct 545 ms 18040 KB Output is correct
3 Correct 540 ms 17912 KB Output is correct