Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

AshToady's blog

By AshToady, history, 3 years ago, In English

Considering everything i could find on maps, unordered maps are supposed to be faster than maps. I experimented on a cses problem, and surprisingly unordered map gave TLE on a few test cases whereas map passed them all.

problem

Code

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
#define int long long
 main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int n,x;cin>>n>>x;int a[n+1];

 for(int i=1;i<=n;cin>>a[i++]);
  a[0]=0;
  unordered_map<int,int>mp;
  for(int i=1;i<=n;i++)
  {
    a[i]+=a[i-1];mp[a[i]]++;
  }
  mp[0]++;
  int cnt=0;

  for(int i=n;i>0;i--)
  {  mp[a[i]]--;
      cnt+=mp[a[i]-x];

    //cnt+=k;
    //cout<<it1-a<<it-a<<endl;
  }
cout<<cnt;
}
  • Vote: I like it
  • -1
  • Vote: I do not like it

| Write comment?