Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<ctime>
- #include<vector>
- #include<algorithm>
- #include<cstdio>
- using namespace std;
- int X[1000002];
- long long T[1000000002];
- long long Q[1000000002];
- long long A[1000002];
- vector<int> indexesof1;
- int N;
- int Z;
- long long possible1; //how many teams are mekable
- int maxi=1;
- unsigned long long a = 1;
- long long teamcheck(int previousindexof1, int currentindexof1, int nextindexof1);
- int main() {
- ios_base::sync_with_stdio(0);
- srand(time(NULL));
- scanf("%d", &Z);
- for(int z=0; z<Z; z++){
- scanf("%d",&N);
- a <<= 60; //huge prime num
- for(int n = 0; n < N; n++){
- scanf("%d",&X[n]); //input
- if(X[n]==1){
- indexesof1.push_back(n); //adding indexes on which I can find 1's
- possible1++;
- }
- maxi=max(maxi, X[n]); //checking for the maximum num in this array
- }
- for(int i=1; i<=maxi; i++){
- T[i]=rand()%a; //weird random nums assigned to numbers in this array
- }
- Q[1]=T[1];
- for(int k = 2; k <= maxi; k++)
- Q[k]=Q[k-1]+T[k]; //prefix sums of weird random nums
- A[0]=T[X[0]];
- for(int i=1; i<N; i++){
- A[i]=A[i-1]+T[X[i]]; //prefix sums of numbers in this array
- }
- for(int i=0; i<indexesof1.size(); i++){
- int previousindexof1;
- int currentindexof1;
- int nextindexof1;
- if(i==0){
- previousindexof1=0;
- currentindexof1=indexesof1[i];
- nextindexof1=indexesof1[i+1];
- }
- else if(i==indexesof1.size()-1){
- previousindexof1=indexesof1[i-1];
- currentindexof1=indexesof1[i];
- nextindexof1=N-1;
- }
- else{
- previousindexof1=indexesof1[i-1];
- currentindexof1=indexesof1[i];
- nextindexof1=indexesof1[i+1];
- }
- possible1+=teamcheck(previousindexof1, currentindexof1, nextindexof1);
- }
- printf("%lld \n", possible1);
- }
- }
- long long teamcheck(int previousindexof1, int currentindexof1, int nextindexof1){
- long long possible=0;
- int j=1;
- while(previousindexof1+j<currentindexof1){
- //from left side for given indexesof1[i]
- if(X[previousindexof1+j]<X[previousindexof1+j+1]){
- if(previousindexof1+j+X[previousindexof1+j+1]-1<N){
- if(A[previousindexof1+j+X[previousindexof1+j+1]-1]-A[previousindexof1+j-1]==Q[X[previousindexof1+j+1]])
- possible++;
- }
- }
- else if(X[previousindexof1+j]>X[previousindexof1+j+1]){
- if(previousindexof1+j+X[previousindexof1+j]-1<N){
- if(A[previousindexof1+j+X[previousindexof1+j]-1]-A[previousindexof1+j-1]==Q[X[previousindexof1+j]])
- possible++;
- }
- }
- j++;
- }
- int k=1;
- while(nextindexof1-k>currentindexof1){
- //from right side for given indexesof1[i]
- if(X[nextindexof1-k]<X[nextindexof1-k-1]){
- if(nextindexof1-k-X[nextindexof1-k-1]>=0){
- if(A[nextindexof1-k]-A[nextindexof1-k-X[nextindexof1-k-1]]==Q[X[nextindexof1-k-1]])
- possible++;
- }
- }
- else if(X[nextindexof1-k]>X[nextindexof1-k-1]){
- if(nextindexof1-k-X[nextindexof1-k]>=0){
- if(A[nextindexof1-k]-A[nextindexof1-k-X[nextindexof1-k]]==Q[X[nextindexof1-k]])
- possible++;
- }
- }
- k++;
- }
- return possible;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement