Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- using namespace std;
- int main()
- {
- int n,b;
- cin >> n >>b;
- vector<int>nums(n+1);
- int whereM=-1;
- for(int i=1; i<=n; i++)
- {
- int x;
- cin>>x;
- if(x==b)
- {
- whereM=i;
- }
- nums[i]=x;
- }
- int sum=0;
- unordered_map<int,int> odd;
- unordered_map<int,int> even;
- even[0]=1;
- odd[0]=0;
- for(int i=whereM-1; i>0; i--)
- {
- if(nums[i]>b)sum++;
- else sum--;
- if((whereM-i)%2==0)
- {
- if(even.find(sum)!=even.end())even[sum]++;
- else even[sum]=1;
- }
- else if(odd.find(sum)!=odd.end())odd[sum]++;
- else odd[sum]=1;
- }
- sum=0;
- int count=even[0];
- for(int i=whereM+1; i<=n ; i++)
- {
- if(nums[i]>b)sum++;
- else sum--;
- if((i-whereM)%2==0)
- {
- if(even.find(-sum)!=even.end()) count +=even[-sum];
- }
- else
- {
- if(odd.find(-sum)!=odd.end()) count+=odd[-sum];
- }
- }
- cout << count<<'\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement