Submission

Status:

PPPPPP-PPP

Subtask/Task Score:

90/100

Score: 90

User: ohoho

Problemset: สำรวจอาเรย์ 2

Language: cpp

Time: 0.005 second

Submitted On: 2025-10-09 21:58:10

#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;
    
    // FINAL FIX: กำหนดค่าเริ่มต้นให้กับ lock variables เป็นพิกัดที่ถูกต้อง (1, 1) ทันที
    // เพื่อป้องกัน Undefined Behavior หากพิกัดแรกที่อ่านมาเป็น 1 1 
    // และถูกใช้เป็นค่า lock ทันทีโดยไม่มีการอัปเดต
    int x_last_valid = 1; 
    int y_last_valid = 1; 
    
    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];
            x_last_valid = x;
            y_last_valid = y;
        } else {
            // 2. พิกัดนอกขอบเขต: ใช้ค่าจากพิกัดที่ถูกต้องล่าสุด (lock)
            
            // ใช้ x_last_valid, y_last_valid ที่ถูกกำหนดค่าแล้ว (อย่างน้อย 1, 1)
            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;
}