CodeForces 1333-C Eugene and an array (sub-interval sum is 0, prefix sum)

 

http://codeforces.com/contest/1333/problem/C

 

 

 

 

Rough meaning:

If a sub-interval, the sum of any of its sub-intervals is not 0, then it is good, find the number of such sub-intervals

 

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 const int INF=0x3f3f3f3f;
 4 const double eps =1e-8;
 5 const int mod=1e8;
 6 const int maxn=2e5+10;
 7 using namespace std;
 8 
 9 map<LL,LL> mp;
10 
11 int main()
12 {
13     #ifdef DEBUG
14     freopen ( " sample.txt " , " r " , stdin);
 15      #endif 
16      
17      int n;
 18      scanf ( " % d " , & n);
 19      LL pos = 1 ; // Record the last interval and When the position is 0, the initial position is 1 (the number 0 is inserted into position 1) 
20      LL sum = 0 ; // prefix and 
21      LL ans = 0 ;
 22      mp [ 0 ] = 1 ; // the number 0 is inserted into position 1 
23      for ( inti = 2 ; i <= n + 1 ; i ++) // equivalent to adding a number 0 at the front, so the number is all +1 
24      {
 25          int x;
 26          scanf ( " % d " , & x);
 27          sum + = X;
 28          IF (mp.count (SUM))
 29          {
 30              POS = max (POS, MP [SUM]);
 31 is              ANS = + I- (POS + . 1 ) + . 1 - . 1 ; // -1 because the interval [ The sum of pos + 1, i] is 0, to subtract this interval 
32          }
 33          else ans + = i- (pos +1 ) + 1 ; // Add interval [pos + 1, i], [pos + 2, i], [pos + 3, i] ... [i, i] 
34          mp [sum] = i;
 35      }
 36      printf ( " % lld \ n " , ans);
 37      
38      return  0 ;
 39 }

 

 

 

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12664197.html
Recommended