使用地图找不到可能的运行时错误
Can't find possible run time error using maps
我在某处遇到了分割错误,但由于我无法访问案例测试(这是由编程竞赛网站上的自动判断判断的),我找不到在哪里。
#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
两天来,我一直在寻找可能具有无效内存访问权限的地方,但我找不到或想不出任何内存。
此代码中有哪些可能导致运行时错误的行?
我看到您的代码中有很多可能的改进,但没有运行时错误的原因。但是,在许多判断系统上,我看到内存限制显示为运行时错误,我相信您的代码也是如此。
提示:尝试谷歌二叉索引树和嵌套二元索引树。谷歌翻译在翻译声明方面并不完美,但我相信这可能会引导你找到一个渐近更好的解决方案。
首先,请尝试在代码中使用typedef,以便于阅读。其次,我在您的代码中没有看到任何错误,并且运行良好,根据给定输入给出输出 150,420。我在许多在线裁判测试平台上运行它,没有运行时错误。在 www.ideone.com 上试一试
相关文章:
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 从子类访问模板类成员时出现"找不到标识符"错误
- 静态链接库时,收到链接器错误:找不到 -lgcc_s
- C++ ZBar cmake 错误"找不到文件"时包括
- 如何修复使用VScode调试器gcc调试时的"找不到文件"错误
- GTK+3 编译错误"找不到信号处理程序",您是否使用 -rdynamic 进行了编译?
- SWIG c++ 到 C# 错误找不到入口点SWIGRegisterExceptionCallbacks_xxxx
- NDK R20 LD:LD:错误:找不到-lpthread
- 双重释放或损坏错误(找不到错误?
- 错误找不到-lglu
- 错误:找不到匹配的超载函数
- 使用自定义iocontext时,找不到FFMPEG库M4A MOOV原子
- 生成文件错误 - 找不到文件 - *.cpp
- Qt 创建器错误:找不到 -lopencv_imgcodecs
- 尝试包含id3lib标头时出现找不到文件错误
- 运行时错误:找不到与 ImageSensor 匹配的 Python 模块
- 为什么它会导致运行时错误,字符到整数而不初始化
- 程序从IDE运行时找不到映像目录,但从文件夹运行时却找不到CAN
- 编译 llvm/clang 时出现致命错误 - 找不到'cstddef'文件
- 当调用不在函数内时出现"找不到类型"错误