#include<bits/stdc++.h>
using namespace std;
unordered_map<int, int> hmap;
int presum, ma, n, k,st, arr[100005];
int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    hmap.insert({0, -1});//特别注意
    for (int i = 0; i < n; i++) {
        presum+=arr[i];
        if(hmap.find(presum-k)!=hmap.end()){
            st=hmap[presum-k];
            ma=max(ma,i-st);
        }
        if(hmap.find(presum)==hmap.end())hmap[presum]=i;//i处的前缀和最后更新
    }
    cout << ma;
}

【算法讲解046【必备】构建前缀信息的技巧-解决子数组相关问题】 https://www.bilibili.com/video/BV1Sj411q7fi/?share_source=copy_web&vd_source=5065fa61022691e8df35c771a30e6d29