This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int INF=1e9;
struct node{
int s,e,m;
ii val;
ll lazy=0;
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
val=ii(-1,s);
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void propo(){
if (lazy){
val.fi+=lazy;
if (s!=e){
l->lazy+=lazy;
r->lazy+=lazy;
}
lazy=0;
}
}
void update(int i,int j,ll k){
if (s==i && e==j){
lazy+=k;
}
else{
if (j<=m) l->update(i,j,k);
else if (m<i) r->update(i,j,k);
else l->update(i,m,k),r->update(m+1,j,k);
l->propo(),r->propo();
val=max(l->val,r->val);
}
}
} *root=new node(0,1100005);
int n;
int arr[1100000];
int temp[1100000];
ii cnt[1100000];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n;
rep(x,0,1<<n) cin>>arr[x];
if (n==1){
cout<<2<<endl;
return 0;
}
int af=(1<<n)-1;
ll ans=(1LL<<n)*((1LL<<n)+1)/2;
memset(temp,-1,sizeof(temp));
rep(x,0,1<<n){
if (temp[arr[x]]==-1) temp[af^arr[x]]=x;
}
rep(x,0,1<<n){
if (x%4<2) cnt[x].fi++;
else cnt[x].se++;
}
rep(x,0,1<<n){
if (x) root->update(0,x-1,-1);
if (temp[arr[x]]==-1){
root->update(0,x,-INF);
}
else{
root->update(0,temp[arr[x]],INF+2);
}
auto it=root->val;
if (it.fi==0){
cnt[x+1]=cnt[it.se];
if ((x+1)%4<2){
ans-=cnt[x+1].fi;
cnt[x+1].fi++;
}
else{
ans-=cnt[x+1].se;
cnt[x+1].se++;
}
//cout<<x+1<<" "<<cnt[x+1].fi<<" "<<cnt[x+1].se<<endl;
}
}
cout<<ans<<endl;
}
Compilation message (stderr)
dojave.cpp: In constructor 'node::node(int, int)':
dojave.cpp:46:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | s=_s,e=_e,m=s+e>>1;
| ~^~
# | 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... |