Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: ohoho
Problemset: สำรวจอาเรย์ 2
Language: cpp
Time: 0.005 second
Submitted On: 2025-10-12 17:06:42
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// ใช้ ios_base/cin.tie เพื่อความเร็ว
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, k, i, j;
int positive = 0, negative = 0, even = 0, odd = 0;
int x, y;
// **FIX 1:** กำหนดค่าเริ่มต้นเป็น 0 เพื่อความปลอดภัย
int last_x = 0;
int last_y = 0;
if (!(cin >> n >> m)) return 0;
// **FIX 2:** ใช้ vector เพื่อแก้ปัญหา VLA และ Stack Overflow
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 >> k)) return 0;
// ลูปประมวลผลพิกัด
for(i=0; i<k; i++) {
cin >> x >> y;
int value_to_count;
bool should_count = false;
if (x >= 1 && x <= n && y >= 1 && y <= m) {
// 1. พิกัดถูกต้อง: ใช้ค่าใหม่, อัปเดตค่า lock
value_to_count = arr[x-1][y-1];
last_x = x;
last_y = y;
should_count = true;
} else if (last_x != 0) { // ตรวจสอบว่ามีค่า lock ที่ถูกต้องแล้ว
// 2. พิกัดนอกขอบเขต: ใช้ค่า lock เดิม
value_to_count = arr[last_x - 1][last_y - 1];
should_count = true;
}
// **ทำการนับเมื่อมีค่าที่ถูกต้อง**
if (should_count) {
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;
}