rt,题面里说的是 序列中的元素值在 int 范围内,但是我不开 longlong 就只有 50pts
这是50分代码:
#include<bits/stdc++.h>
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=5e5+5;
int read() {
int x=0,flag=1;
char ch=0;
while(ch<'0'||ch>'9') {
if(ch=='-')flag=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*flag;
}
int n,a[N],help[N],dy[N],pre[N],ans,tr[N*4],b[N*4];
void pushup(int p){
if(tr[p<<1]>tr[p<<1|1])tr[p]=tr[p<<1],b[p]=b[p<<1];
else tr[p]=tr[p<<1|1],b[p]=b[p<<1|1];
}
void update(int p,int l,int r,int x,int op,int dep){
if(l==r){
if(dep>tr[p])tr[p]=dep,b[p]=op;
return;
}
int m=(l+r)>>1;
if(m>=x)update(p<<1,l,m,x,op,dep);
else update(p<<1|1,m+1,r,x,op,dep);
pushup(p);
}
PII query(int p,int l,int r,int s,int t){
if(s>=l&&t<=r){
return {tr[p],b[p]};
}
int m=(s+t)>>1;
PII ans={0,0};
if(m>=l){
PII op=query(p<<1,l,r,s,m);
if(op.fi>ans.fi)ans.fi=op.fi,ans.se=op.se;
}
if(m<r){
PII op=query(p<<1|1,l,r,m+1,t);
if(op.fi>ans.fi)ans.fi=op.fi,ans.se=op.se;
}
return ans;
}
signed main(){
n=read();
for(int i=1;i<=n;i++){
a[i]=read();
help[i]=a[i];
}
sort(help+1,help+1+n);
int v=unique(help+1,help+1+n)-help-1;
for(int i=1;i<=n;i++){
dy[i]=lower_bound(help+1,help+1+v,a[i])-help;
}
for(int i=1;i<=n;i++){
if(i==1||dy[i]==v)ans++,pre[i]=0,update(1,1,v,dy[i],i,1);
else {
PII p=query(1,dy[i]+1,v,1,v);
pre[i]=p.se;
ans+=p.fi+1;
update(1,1,v,dy[i],i,p.fi+1);
}
}
cout<<ans<<endl;
for(int i=1;i<=n;i++){
printf("%d ",pre[i]);
}
return 0;
}
这是AC代码:
#include<bits/stdc++.h>
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=5e5+5;
int read() {
int x=0,flag=1;
char ch=0;
while(ch<'0'||ch>'9') {
if(ch=='-')flag=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*flag;
}
int n,a[N],help[N],dy[N],pre[N],ans,tr[N*4],b[N*4];
void pushup(int p){
if(tr[p<<1]>tr[p<<1|1])tr[p]=tr[p<<1],b[p]=b[p<<1];
else tr[p]=tr[p<<1|1],b[p]=b[p<<1|1];
}
void update(int p,int l,int r,int x,int op,int dep){
if(l==r){
if(dep>tr[p])tr[p]=dep,b[p]=op;
return;
}
int m=(l+r)>>1;
if(m>=x)update(p<<1,l,m,x,op,dep);
else update(p<<1|1,m+1,r,x,op,dep);
pushup(p);
}
PII query(int p,int l,int r,int s,int t){
if(s>=l&&t<=r){
return {tr[p],b[p]};
}
int m=(s+t)>>1;
PII ans={0,0};
if(m>=l){
PII op=query(p<<1,l,r,s,m);
if(op.fi>ans.fi)ans.fi=op.fi,ans.se=op.se;
}
if(m<r){
PII op=query(p<<1|1,l,r,m+1,t);
if(op.fi>ans.fi)ans.fi=op.fi,ans.se=op.se;
}
return ans;
}
signed main(){
n=read();
for(int i=1;i<=n;i++){
a[i]=read();
help[i]=a[i];
}
sort(help+1,help+1+n);
int v=unique(help+1,help+1+n)-help-1;
for(int i=1;i<=n;i++){
dy[i]=lower_bound(help+1,help+1+v,a[i])-help;
}
for(int i=1;i<=n;i++){
if(i==1||dy[i]==v)ans++,pre[i]=0,update(1,1,v,dy[i],i,1);
else {
PII p=query(1,dy[i]+1,v,1,v);
pre[i]=p.se;
ans+=p.fi+1;
update(1,1,v,dy[i],i,p.fi+1);
}
}
cout<<ans<<endl;
for(int i=1;i<=n;i++){
printf("%lld ",pre[i]);
}
return 0;
}
很疑惑是我的实现问题还是数据问题