dalao单链表为啥会超时啊

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

Catlz @ 2024-02-06 16:31:21

const int N = 1e6 + 10, M = 2 * N;
int h[N],eh[M],ee[N],idxg,nt[N];
void add(int a, int b, int k)
{
    eh[idxg] = b,ee[idxg] = k,nt[idxg]=h[a],h[a] = idxg++;  //eh存放储物柜的牌号,ee存储这个柜子的物品
}
int main()
{
    memset(h, -1, sizeof h);
    int n, q;
    cin >> n >> q;
    while (q--)
    {
        int x;
        int i, j, k;
        cin >> x>>i>>j;
        if (x == 1)
        {
            cin >> k;
            add(i, j, k);
        }
        else
        {
            for (int d = h[i]; d != -1; d = nt[d])
            {
                int t = eh[d];
                if (t==j&&ee[d] != 0)
                {
                    cout << ee[d] << endl; 
                    break;
                }
            }
        }
    }
    return 0;
}

by Catlz @ 2024-02-06 16:34:01

好吧,貌似确实会超时T A T


by llqqhh @ 2024-02-25 09:42:43

同问, 我也用单链表做的


by llqqhh @ 2024-02-25 09:48:02

知道了,单链表查询复杂度O(nq)


by shb20111113 @ 2024-03-11 22:09:59

Ο[Θ](nq)

|