이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i,a,b) for (int i=a; i<(b); ++i)
#define per(i,a,b) for (int i=(b)-1; i>=(a); --i)
using namespace std;
using i64 = long long;
int const MAXN=1<<21;
int n;
int a[MAXN];
int to[MAXN];
int len_[MAXN];
i64 cnt_[MAXN][2];
auto len=len_+1;
auto cnt=cnt_+1;
i64 solve(int l,int r) {
if (l==r) return 0;
int m=(l+r)/2;
i64 res=solve(l,m)+solve(m+1,r);
int lo=m+1,hi=m;
int i=m+1,j=m;
while (l<=lo && hi<=r) {
if (lo<i) {
i-=1;
lo=min(lo,to[a[i]]);
hi=max(hi,to[a[i]]);
} else if (j<hi) {
j+=1;
lo=min(lo,to[a[j]]);
hi=max(hi,to[a[j]]);
} else {
int curLen=j-i+1;
int lenL=(l<=i-1 ? len[i-1] : 0);
int lenR=(j+1<=r ? len[j+1] : 0);
lo-=lenL;
hi+=lenR;
len[lo]=len[hi]=hi-lo+1;
rep(modL,0,2) if (m<hi) {
i64 cntL=(l<=i-1 ? cnt[i-1][modL] : 0) + (curLen>0 && modL==0 ? 1 : 0);
cnt[hi][((curLen+lenR)/2+modL)%2]+=cntL;
}
rep(modR,0,2) if (lo<=m) {
i64 cntR=(j+1<=r ? cnt[j+1][modR] : 0) + (curLen>0 && modR==0 ? 1 : 0);
cnt[lo][((curLen+lenL)/2+modR)%2]+=cntR;
}
rep(modL,0,2) rep(modR,0,2) if ((curLen/2+modL+modR)%2==0) {
i64 cntL=(l<=i-1 ? cnt[i-1][modL] : 0) + (curLen>0 && modL==0 ? 1 : 0);
i64 cntR=(j+1<=r ? cnt[j+1][modR] : 0) + (curLen>0 && modR==0 ? 1 : 0);
res+=cntL*cntR;
}
lo-=1;
hi+=1;
}
}
return res;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout<<fixed<<setprecision(10);
cin>>n;
int N=1<<n;
rep(i,0,N) cin>>a[i];
if (n==1) {
cout<<2<<endl;
return 0;
}
rep(i,0,N) to[N-1-a[i]]=i;
cout<<i64(N+1)*N/2-solve(0,N-1)<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |