Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: ohoho

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

Language: cpp

Time: 0.004 second

Submitted On: 2025-10-09 22:01:59

#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 = 0; 
    int y_last_valid = 0; 
    
    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 = 0;
        bool is_valid = false;
        
        if (x >= 1 && x <= n && y >= 1 && y <= m) {
            // 1. พิกัดถูกต้อง: ใช้ค่าใหม่
            value_to_count = arr[x-1][y-1];
            x_last_valid = x;
            y_last_valid = y;
            is_valid = true;
        } else if (x_last_valid != 0) {
            // 2. พิกัดนอกขอบเขต และมีค่า lock แล้ว: ใช้ค่า lock
            value_to_count = arr[x_last_valid - 1][y_last_valid - 1]; 
            is_valid = true;
        } 
        // 3. (Implicit) พิกัดนอกขอบเขต และยังไม่มีค่า lock (x_last_valid == 0): ไม่ทำอะไรเลย (is_valid = false)

        // **การนับจะเกิดขึ้นก็ต่อเมื่อมีค่าที่ถูกต้องให้ใช้**
        if (is_valid) {
            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;
}