#include <bits/stdc++.h>
using namespace std;
void imax(int &a, int b){
a=max(a, b);
}
void imin(int &a, int b){
a=min(a, b);
}
void lmax(long long &a, long long b){
a=max(a, b);
}
void lmin(long long &a, long long b){
a=min(a, b);
}
/*
WARNING: I'm using strange bracket style!
*/
const int SIZE=1100000;
long long res, n, x, y, MI=SIZE, MX, len=0;
int arr[SIZE], where[SIZE];
long long f(int X, int Y, int L){
if (Y-X+1>L) return 0;
int pb=max(1, Y-L+1);
int pe=min(X, (int)n-L+1);
return pe-pb+1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for (int i=1; i<=n; i++)
cin>>arr[i], where[arr[i]]=i;
for (int i=2*n; i>n; i--)
{
x=i/2, y=i-x, len++;
lmin(MI, where[y]), lmax(MX, where[y]);
lmin(MI, where[x]), lmax(MX, where[x]);
res+=f(MI, MX, len);
//cout<<x<<" "<<y<<" "<<MI<<" "<<MX<<" "<<max(0ll, min((len-(MX-MI)), n))<<endl;
}
cout<<(n+1)+n<<" "<<res<<"\n";
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; void imax(int &a, int b){ a=max(a, b); } void imin(int &a, int b){ a=min(a, b); } void lmax(long long &a, long long b){ a=max(a, b); } void lmin(long long &a, long long b){ a=min(a, b); } /* WARNING: I'm using strange bracket style! */ const int SIZE=1100000; long long res, n, x, y, MI=SIZE, MX, len=0; int arr[SIZE], where[SIZE]; long long f(int X, int Y, int L){ if (Y-X+1>L) return 0; int pb=max(1, Y-L+1); int pe=min(X, (int)n-L+1); return pe-pb+1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for (int i=1; i<=n; i++) cin>>arr[i], where[arr[i]]=i; for (int i=2*n; i>n; i--) { x=i/2, y=i-x, len++; lmin(MI, where[y]), lmax(MX, where[y]); lmin(MI, where[x]), lmax(MX, where[x]); res+=f(MI, MX, len); //cout<<x<<" "<<y<<" "<<MI<<" "<<MX<<" "<<max(0ll, min((len-(MX-MI)), n))<<endl; } cout<<(n+1)+n<<" "<<res<<"\n"; return 0; } |
English