#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
typedef vector < int > vi;
vector < int > s[N],A[N];
ll pri[N],sz[N],lz[N],L[N],R[N],ans[N],uu;
char u[N+N];
void put(int x){
if(!x) return;
lz[x] = 1 - lz[x];
swap(L[x],R[x]);
}
void push(int x){
if(!lz[x]) return;
lz[x] = 0;
put(L[x]); put(R[x]);
}
vi top(vi a, vi b){
if(a.size() < b.size()) swap(a,b);
int t,i,j,h=0;
for(i=a.size()-1,j=b.size()-1 ; j>=0 ; i--,j--){
t = a[i]+b[j]+h;
if(t >= 10) { h=1; t-=10; }
a[i] = t;
}
if(h) { if(i>=0) a[i]++; else a.insert(a.begin() , 1); }
return a;
}
vi cik(vi &a, vi &b){
int t,i,j,h=0;
for(i=a.size()-1,j=b.size()-1 ; j >= 0; i--,j--){
t = a[i]-b[j]-h;
if(t < 0) { h=1; t+=10; }
a[i] = t;
}
if(h) a[i]--;
for(; a.size()>1 && a[0] == 0 ;) a.erase(a.begin());
return a;
}
void relax(int x){
sz[x] = sz[ L[x] ] + sz[ R[x] ] + 1;
s[x] = top(top(s[ L[x] ] , s[ R[x] ]) , A[x]);
}
int mrg(int x, int y){
if(!x) return y;
if(!y) return x;
if(pri[x] < pri[y]){
push(y);
L[y] = mrg(x,L[y]);
relax(y);
return y;
}
else{
push(x);
R[x] = mrg(R[x],y);
relax(x);
return x;
}
}
bool g(vi &a, vi &b){
if(a.size() < b.size()) return 1;
if(a.size() > b.size()) return 0;
for(int i=0; i<a.size(); i++){
if(b[i] > a[i])
return 1;
if(a[i] > b[i])
return 0;
}
return 1;
}
void split(int x, vector <int> &k, int &a, int &b){
if(x == 0){
a = b = 0;
return;
}
push(x);
if(g(k,s[ L[x] ])){
split(L[x], k, a, b);
L[x] = b;
b = x;
}
else{
cik(k,s[ L[x] ]);
cik(k,A[x]);
split(R[x], k, a, b);
R[x] = a;
a = x;
}
relax(x);
}
void f(int x, int h){
if(x==0) return;
push(x);
f(L[x],h); ans[x] = h+sz[ L[x] ]+1; f(R[x],h+sz[ L[x] ]+1);
}
int main(){
ll n,i,j;
vi bir,x,y,t;
bir.pb(1);
s[0].pb(0);
y.pb(0);
scanf("%lld",&n);
int a,b,root = 0;
srand(time(NULL));
for(i=1;i<=n;i++){
scanf(" %s", u);
uu = strlen(u);
x.clear();
for(j=0;j<uu;j++) x.pb(u[j]-'0');
cik(x,y);
vi t = x;
cik(t,bir);
split(root, t, a, b);
//return 0;
sz[i] = 1;
pri[i] = rand();
s[i] = A[i] = x;
root = mrg( mrg(a,i) , b );
y = top(x,y);
// return 0;
}
f(root,0);
for(i=1;i<=n;i++) printf("%lld ",ans[i]);
return 0;
}
Compilation message
permutation.cpp: In function 'bool g(vi&, vi&)':
permutation.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<a.size(); i++){
~^~~~~~~~~
permutation.cpp: In function 'int main()':
permutation.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&n);
~~~~~^~~~~~~~~~~
permutation.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %s", u);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |