This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define f first
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
const int mod= 1e9 +7;
const int N=1e5*4;
int binpow (int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binpow (a, n-1) * a;
else {
int b = binpow (a, n/2);
return b * b;
}
}
int t[N*4],bn[N*4];
vector<int>v(N);
void push(int tl,int tr,int v){
if(bn[v]==-1){
return;
}
t[v]=(tr-tl+1)*bn[v];
if(tl!=tr){
bn[v*2]=bn[v];
bn[v*2+1]=bn[v];
}
bn[v]=-1;
}
void build(int tl,int tr,int vs){
if(tl==tr){
t[vs] = v[tl];
return;
}
int tm = (tl+tr)/2;
build(tl,tm,vs*2);
build(tm+1,tr,vs*2+1);
t[vs] = t[vs*2] + t[vs*2+1];
}
void update( int v,int l,int r,int x,int tl,int tr){
push(tl,tr,v);
if(l>tr||r<tl||tr<tl)return;
if(l<=tl&&tr<=r){
bn[v]=x;
push(tl,tr,v);
return;
}
int tm=(tl+tr)/2;
update(v*2,l,r,x,tl,tm);
update(v*2+1,l,r,x,tm+1,tr);
t[v]=t[v*2]+t[v*2+1];
}
int getsum(int l,int r,int v,int tl,int tr){
push(tl,tr,v);
if(l>tr||r<tl||tr<tl)return 0;
if(l<=tl&&tr<=r){
return t[v];
}
int tm=(tl+tr)/2;
return getsum(l,r,v*2,tl,tm)+getsum(l,r,v*2+1,tm+1,tr);
}
void solve(){
for(int i = 1;i<N*4;i++){
bn[i] = -1;
}
int n,m,k;
cin>>n;
for(int i = 0;i<n;i++)cin>>v[i];
build(0,n-1,1);
vector<int>ans(n);
if(n<=2000){
for(int i = 0;i<n;i++){
ans[i] = v[i];
for(int j = i - 1;j>=0;j--){
if(ans[j]==v[i]){
for(int k = j;k<i;k++){
ans[k] = v[i];
}
break;
}
}
}
for(auto to:ans)cout<<to<<"\n";
return;
}
deque<int>q1,q2;
// int ind1 = -1,ind2 = -1;
vector<pair<int,pii>>vs;
for(int i = 0;i<n;i++){
if(v[i]==1){
if(q1.empty()){
q1.pb(i);
continue;
}
int u = q1.back();
while(!q2.empty()&&q2.back()>u)q2.pop_back();
q1.pb(i);
vs.pb({1,{u,i}});
// cout<<u<<" "<<i<<endl;
}
else{
if(q2.empty()){
q2.pb(i);
continue;
}
int u = q2.back();
while(!q1.empty()&&q1.back()>u)q1.pop_back();
q2.pb(i);
vs.pb({2,{u,i}});
}
}
for(auto [val,to] : vs){
update(1,to.f,to.s,val,0,n - 1);
}
for(int i = 0;i<n;i++){
ans[i] = getsum(i,i,1,0,n-1);
}
for(auto to:ans)
cout<<to<<"\n";
}
signed main()
{
// freopen("seq.in", "r", stdin);
// freopen("seq.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int cnt=0;
int tt=1;//cin>>tt;
while(tt--)solve();
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:94:8: warning: unused variable 'm' [-Wunused-variable]
94 | int n,m,k;
| ^
Main.cpp:94:10: warning: unused variable 'k' [-Wunused-variable]
94 | int n,m,k;
| ^
Main.cpp: In function 'int main()':
Main.cpp:174:6: warning: unused variable 'cnt' [-Wunused-variable]
174 | int cnt=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... |