// tested by Hightail: https://github.com/dj3500/hightail
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int i=0;(i)<(n);++(i))
#define FORI(i,n) for(int i=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int n; scanf("%d",&(n));
#define wez2(n,m) int n,m; scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int n,m,k; scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);
const int N = 1'000'007;
int a[N], pos[N];
int main () {
wez(n);
FORI(i,n) {
wez(x);
a[i] = x;
pos[x] = i;
}
int le = pos[n], ri = pos[n];
ll res = 1;
REP(sz,2,n) {
if (sz % 2 == 0) {
REMIN(le, pos[n-sz/2]);
REMAX(ri, pos[n-sz/2]);
}
// how many intervals in [1,n] of length `sz` containing [le,ri]
// [x,x+sz-1] must have x <= le, ri <= x+sz-1, 1 <= x, x+sz-1 <= n
// x <= le, x <= n-sz+1
// x >= ri-sz+1, x >= 1
int now = min(le, n-sz+1) - max(ri-sz+1, 1) + 1;
res += max(0, now);
}
printf("%d %lld", 2*n+1, res);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | // tested by Hightail: https://github.com/dj3500/hightail #include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int i=0;(i)<(n);++(i)) #define FORI(i,n) for(int i=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int n; scanf("%d",&(n)); #define wez2(n,m) int n,m; scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int n,m,k; scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); const int N = 1'000'007; int a[N], pos[N]; int main () { wez(n); FORI(i,n) { wez(x); a[i] = x; pos[x] = i; } int le = pos[n], ri = pos[n]; ll res = 1; REP(sz,2,n) { if (sz % 2 == 0) { REMIN(le, pos[n-sz/2]); REMAX(ri, pos[n-sz/2]); } // how many intervals in [1,n] of length `sz` containing [le,ri] // [x,x+sz-1] must have x <= le, ri <= x+sz-1, 1 <= x, x+sz-1 <= n // x <= le, x <= n-sz+1 // x >= ri-sz+1, x >= 1 int now = min(le, n-sz+1) - max(ri-sz+1, 1) + 1; res += max(0, now); } printf("%d %lld", 2*n+1, res); } |
English