Submission

Status:

----------

Subtask/Task Score:

0/100

Score: 0

User: ohoho

Problemset: อะนาแกรม 2

Language: cpp

Time: 0.002 second

Submitted On: 2025-10-09 21:52:51

#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 จะถูกใช้แทน xlock/ylock 
    // และจะถูกกำหนดค่าเริ่มต้นให้ทันทีที่พิกัดแรกที่ถูกต้องถูกอ่าน
    // กำหนดค่าเริ่มต้นเป็น 0 เพื่อความปลอดภัย แต่จะถูกแทนที่ด้วยค่า 1-based index ทันที
    int x_last_valid = 0, y_last_valid = 0; 
    
    if (!(cin >> n >> m)) return 0;
    
    // FIX 1: แก้ไขปัญหา VLA และการกำหนดขนาด 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;
        
        // **FIX 2: ตรรกะการจัดการพิกัดที่ปลอดภัย**
        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)
            // Note: โจทย์รับประกันว่าพิกัดแรกจะถูกต้อง, ดังนั้น x_last_valid/y_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++;
        
        // ตรวจสอบเลขคี่ (ใช้ != 0 ปลอดภัยและครอบคลุมที่สุด)
        if(value_to_count % 2 != 0) odd++;
    }
    
    cout << positive << " " << negative << " " << even << " " << odd << endl;
    return 0;
}