ãåºéã®å¤ã®åããè¦ãããç´¯ç©åãã¨ããï¼ï¼
åé¡æ¦è¦
æ°å ã¨æ´æ° ãä¸ããããã
æ°åã®é£ç¶ããåºéã§ãã£ã¦ããã®ç·åã ã«ä¸è´ãããã®ãä½åããããæ±ããã
å¶ç´
èãããã¨
0-indexed ã§èããã
æ°å ã®ç´¯ç©åã ã¨ãããããã®ã¨ãã
ã¨ãªãããã£ã¦ããã¨ã®åé¡ã¯æ¬¡ã®ããã«ãªãã
åã®æ°ãããªãæ°å ãä¸ããããã
ãæºãããã㪠ã®çµã®åæ°ãæ±ããã
ãããªãã°ã解ãããããªãã ã«å¯¾ãã¦ã
- ãã¤
ãæºãããã㪠ã®åæ°ãåç®ãã¦ããã°ããããããããããã«ã¯ã次ã®é£æ³é
å (C++ ãªãã° map
) ãåçã«ç®¡çãã¦ããã°ããã
nums[x]
ï¼å¤ ã®åæ°
è¨ç®é㯠ã¨ãªãã
ã³ã¼ã
#include <bits/stdc++.h> using namespace std; int main() { long long N, K; cin >> N >> K; vector<long long> A(N), S(N+1, 0); map<long long, long long> nums; for (int i = 0; i < N; i++) { cin >> A[i]; S[i+1] = S[i] + A[i]; } long long res = 0; for (int i = 0; i <= N; i++) { if (nums.count(S[i] - K)) res += nums[S[i] - K]; nums[S[i]]++; } cout << res << endl; }