2

どこかでセグメンテーション フォールトが発生しているのですが、ケース テスト(プログラミング コンテストの Web サイトで自動判定によって判定されます) にアクセスできないため、どこが.

#include <iostream>
#include <stdio.h>
#include <map>
#include <algorithm>

using namespace std;

typedef pair<unsigned short int, unsigned short int> par;

int main(){
    int x, y, p, q, a1, b1, a2, b2, val;
    int cont, lin_min, lin_max, col_min, col_max;
    char cmd;
    while((cin >> x >> y >> p) && !(x==0 && y==0 && p==0)){
        cin >> q;
        //Using "getchar ()" to avoid reading line end, because below I'm reading a character
        getchar();
        map<par, int> matriz;
        map<par, int>::iterator it;
        for(int i=0;i<q;i++){
            cin >> cmd;
            //if command 'A' insert in matriz or incremente if already exists
            if(cmd=='A'){
                cin >> val >> a1 >> b1;
                //insert into map (my implementation for sparse matrix)
                matriz[par(a1, b1)]+=val;
            }else 
            //if command 'P' performs a search in the rectangle (a1, b1 to a2, b2)
            if(cmd=='P'){
                cont=0;
                cin >> a1 >> b1 >> a2 >> b2;
                lin_min = min(a1,a2);
                lin_max = max(a1,a2);
                col_min = min(b1,b2);
                col_max = max(b1,b2);
                //traverses the sparse matrix by summing the values that belong to the rectangle
                //I STRONGLY believe that the error is somewhere around here
                for(it=matriz.begin(); it!=matriz.end(); ++it){
                    if((it->first.first>=lin_min && it->first.first<=lin_max)&&
                        (it->first.second>=col_min && it->first.second<=col_max)){
                        cont+=it->second;
                    }
                    if (it->first.first>lin_max && it->first.second>col_max) break;
                }
                cout << cont*p << "\n";
            }
        }
    }
    return 0;
}

入力例:

2 2 10
7
A 11 1 1
A 4 1 0
A 20 0 0
P 1 0 1 1
A 1 0 1
A 6 1 0
P 0 1 1 0
0 0 0

2 日間、無効なメモリ アクセスの可能性がある場所を探していましたが、見つけることも考えることもできませんでした。

実行時エラーを引き起こす可能性のあるこのコードの行は何ですか?

4

2 に答える 2

4

あなたのコードには多くの改善の可能性がありますが、実行時エラーの理由はありません。ただし、多くのジャッジ システムでは、メモリ制限が実行時エラーとして表示されるのを見てきました。これは、コードにも当てはまると思います。

ヒント: バイナリ インデックス ツリーとネストされたバイナリ インデックス ツリーをググってみてください。Google 翻訳は、ステートメントの翻訳において完璧ではありませんが、これにより、漸近的により優れたソリューションにつながる可能性があると思います.

于 2013-11-06T16:51:08.167 に答える