シリーズ: Algorithms
c
62 行
· 更新日 2026-02-09
sub_array_count.c
Algorithms/sub_array_count.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int h; // hight
int cnt; // how many subarrayse ending here have min = h
} Node;
int main() {
int n;
printf("Enter the size of the matrix: ");
if (scanf("%d", &n) != 1) return 0;
char *row = (char *) malloc( (n + 5) * sizeof(char)); // memory allocate from heap
int *height = (int *) malloc (n * sizeof(int));
Node *st = (Node *) malloc (n * sizeof(Node)); // stack for monotonic stack
long long ans = 0;
for (int i = 0; i < n; i++) {
printf("Enter the row %d: ", i + 1);
scanf("%s", row);
// update histogram heights of consecutive zeros
for (int j = 0; j < n; j++) {
if (row[j] == '0') height[j] += 1;
else height[j] = 0;
}
// monotonic stack to compute sum of subarray minimums
int top = 0; // stack size
long long cur = 0; // sum of mins for subarrays ending at current j
for (int j = 0; j < n; j++) {
int h = height[j];
int cnt = 1;
while (top > 0 && st[top - 1].h >= h) {
cnt += st[top - 1].cnt;
cur -= (long long) st[top - 1].h * st[top - 1].cnt;
top --;
}
st[top].h = h;
st[top].cnt = cnt;
top ++;
cur += (long long) h * cnt;
ans += cur;
}
}
printf("Subarray acount: %lld\n", ans);
free(row);
free(height);
free(st);
return 0;
}
関連記事
Algorithms
java
更新日 2026-03-02
#include <iostream>.java
#include <iostream>.java — java source code from the Algorithms learning materials (Algorithms/#include <iostream>.java).
記事を読む →
Algorithms
cpp
更新日 2026-04-07
748.cpp
748.cpp — cpp source code from the Algorithms learning materials (Algorithms/748.cpp).
記事を読む →
Algorithms
cpp
更新日 2026-04-07
827.cpp
827.cpp — cpp source code from the Algorithms learning materials (Algorithms/827.cpp).
記事を読む →
Algorithms
cpp
更新日 2026-04-07
827_best_greedy.cpp
827_best_greedy.cpp — cpp source code from the Algorithms learning materials (Algorithms/827_best_greedy.cpp).
記事を読む →
Algorithms
cpp
更新日 2026-04-07
8402.cpp
8402.cpp — cpp source code from the Algorithms learning materials (Algorithms/8402.cpp).
記事を読む →
Algorithms
cpp
更新日 2026-04-07
860.cpp
860.cpp — cpp source code from the Algorithms learning materials (Algorithms/860.cpp).
記事を読む →