WA WA MLE RE 大佬快来诊断!!!

P3613 【深基15.例2】寄包柜

HardBlackPencil @ 2023-09-02 20:29:35

#include <bits/stdc++.h>
#include <vector>
using namespace std;

const int N = 1e5 + 10;
int n, q; 
vector<vector<int> > a(N);

int main()
{
    cin >> n >> q;
    int flag, i, j, k;
    for(int i = 1; i <= q; i++)
    {
        cin >> flag;
        if (flag == 1)
        {
            cin >> i >> j >> k;
            a[i].resize(j);
            a[i].push_back(k);
        }
        if (flag == 2)
        {
            cin >> i >> j;
            cout << a[i][j] << endl;
        }
    }

    return 0;
}

用向量vector做,MLE,WA, 快哭了,题目的样例AC啊。哪个大佬诊断一下。。。。。。


by HardBlackPencil @ 2023-09-02 20:34:05

#include <bits/stdc++.h>
#include <vector>
using namespace std;

const int N = 1e5 + 10;
int n, q; 
vector<vector<int> > a(N);

int main()
{
    cin >> n >> q;
    int flag, i, j, k;
    for(int l = 1; l <= q; l++)
    {
        cin >> flag;
        if (flag == 1)
        {
            cin >> i >> j >> k;
            a[i].resize(j);
            a[i].push_back(k);
        }
        if (flag == 2)
        {
            cin >> i >> j;
            cout << a[i][j] << endl;
        }
    }

    return 0;
}

刚才发错了,嘿嘿。


by Sqj147 @ 2023-10-12 10:24:37

@HardBlackPencil 你resize后可能丢弃一些元素,可以这样:

if (j + 1 > a[i].size()) a[i].resize(j + 1);  

|