-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB07.php
More file actions
102 lines (80 loc) · 2.06 KB
/
Copy pathB07.php
File metadata and controls
102 lines (80 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!-- この問題は最大で `T = 500000`, `N = 500000` と大きいため、**差分配列(いもす法)+ 累積和**を使って高速に処理します。
## ✅ PHPコード(型付き + 高速対応) -->
<?php
declare(strict_types=1);
/**
* メイン処理関数
*/
function main(): void {
$stdin = fopen('php://stdin', 'r');
if ($stdin === false) {
throw new RuntimeException("Failed to open stdin.");
}
/** @var int $T 営業終了時間 */
$T = (int)fgets($stdin);
/** @var int $N 従業員数 */
$N = (int)fgets($stdin);
/** @var array<int> $cnt 差分配列 */
$cnt = array_fill(0, $T + 1, 0);
// 差分配列への反映
for ($i = 0; $i < $N; $i++) {
$line = fgets($stdin);
if ($line === false) {
throw new RuntimeException("Failed to read line.");
}
[$L, $R] = array_map('intval', explode(' ', trim($line)));
$cnt[$L] += 1;
$cnt[$R] -= 1;
}
// 累積和と出力
$current = 0;
for ($t = 0; $t < $T; $t++) {
$current += $cnt[$t];
echo $current . "\n";
}
fclose($stdin);
}
main();
// ## 📥 入力例
// ```
// 10
// 7
// 0 3
// 2 4
// 1 3
// 0 3
// 5 6
// 5 6
// 5 6
// ```
// ## 📤 出力例
// ```
// 2
// 3
// 4
// 1
// 0
// 3
// 0
// 0
// 0
// 0
// ```
// ## 🧠 解説
// ### 主な変数と型
// | 変数名 | 型 | 内容 |
// | ---------- | ------------ | ------------- |
// | `$T` | `int` | 営業終了時刻(閉店時刻) |
// | `$N` | `int` | 従業員数 |
// | `$cnt` | `array<int>` | 差分配列(サイズ T+1) |
// | `$current` | `int` | 累積和で現在店内にいる人数 |
// | `$stdin` | `resource` | 標準入力リソース |
// ## 🧮 計算量
// * 差分更新:O(N)
// * 累積和:O(T)
// * 合計:**O(N + T)**(最大でも 1,000,000 程度)→ 制約内で余裕
// ## ✅ 実行方法
// ローカルでテストする場合:
// ```bash
// php main.php < input.txt
// ```