Submission
Status:
PPPPPPxPPP
Subtask/Task Score:
90/100
Score: 90
User: ohoho
Problemset: สำรวจอาเรย์ 2
Language: cpp
Time: 0.005 second
Submitted On: 2025-10-09 21:57:12
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// ตั้งค่า I/O ให้เร็วขึ้น
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, h, i, j;
int positive = 0, negative = 0, even = 0, odd = 0;
int x, y;
// x_last_valid, y_last_valid จะถูกใช้เก็บพิกัดที่ถูกต้องล่าสุด
// เราจะยังไม่กำหนดค่าเริ่มต้นที่ 0 แต่จะจัดการในลูปแรก
int x_last_valid;
int y_last_valid;
if (!(cin >> n >> m)) return 0;
// ใช้ vector ขนาด N x M ที่ถูกต้อง
vector<vector<int>> arr(n, vector<int>(m));
// อ่านข้อมูลตาราง
for(i=0; i<n; i++) {
for(j=0; j<m; j++) {
cin >> arr[i][j];
}
}
if (!(cin >> h)) return 0;
// ลูปประมวลผลพิกัด
for(i=0; i<h; i++) {
cin >> x >> y;
int value_to_count;
if (x >= 1 && x <= n && y >= 1 && y <= m) {
// 1. พิกัดถูกต้อง: ดึงค่าและอัปเดตค่า lock
value_to_count = arr[x-1][y-1];
// **FIX ที่สำคัญที่สุด:** กำหนดค่าเริ่มต้นให้กับ lock variables ทันที
// นี่คือค่าแรกที่ถูกรับประกันว่าถูกต้อง
x_last_valid = x;
y_last_valid = y;
} else {
// 2. พิกัดนอกขอบเขต: ใช้ค่าจากพิกัดที่ถูกต้องล่าสุด (lock)
// **การันตีความปลอดภัย:** เมื่อมาถึง else นี้ x_last_valid จะต้องถูกกำหนดค่าแล้ว
value_to_count = arr[x_last_valid - 1][y_last_valid - 1];
}
// การนับค่า
if(value_to_count > 0) positive++;
if(value_to_count < 0) negative++;
if(value_to_count % 2 == 0) even++;
if(value_to_count % 2 != 0) odd++;
}
cout << positive << " " << negative << " " << even << " " << odd << endl;
return 0;
}