企业建网站服务,网络营销的方式都有哪些,绚丽的网站欣赏,百度推广哪家做的最好传送门 文章目录题意#xff1a;思路#xff1a;题意#xff1a; 思路#xff1a;
注意到一个很关键的条件#xff0c;每次插入iii#xff0c;而iii是递增的#xff0c;也就是说插入iii之后只会从前面的最大值转移过来#xff0c;所以我们现在只需要维护插入操作即可思路题意 思路
注意到一个很关键的条件每次插入iii而iii是递增的也就是说插入iii之后只会从前面的最大值转移过来所以我们现在只需要维护插入操作即可这个显然可以用平衡树来维护。 让后就是一个裸的fhq−treapfhq-treapfhq−treap了我们需要维护一个dpdpdp数组dp[i]dp[i]dp[i]表示以iii个数结尾的最长上升子序列对于平衡树中的每个节点都维护一个他的位置pospospos当pushuppushuppushup的时候max(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val))max(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val))max(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val))转移即可。注意dp[tr[u].pos]dp[tr[u].pos]dp[tr[u].pos]不能用tr[u].valtr[u].valtr[u].val代替因为他的值在子树分裂之前存的是子树中最大值分裂后不一定是这个有可能变小所以需要一个dpdpdp数组存下来当前位置的答案。 附带debugdebugdebug代码。
// Problem: P4309 [TJOI2013]最长上升子序列
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4309
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize(Ofast,no-stack-protector,unroll-loops,fast-math)
//#pragma GCC target(sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tunenative)
//#pragma GCC optimize(2)
#includecstdio
#includeiostream
#includestring
#includecstring
#includemap
#includecmath
#includecctype
#includevector
#includeset
#includequeue
#includealgorithm
#includesstream
#includectime
#includecstdlib
#define X first
#define Y second
#define L (u1)
#define R (u1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].ltr[u].r1)
#define Len(u) (tr[u].r-tr[u].l1)
#define random(a,b) ((a)rand()%((b)-(a)1))
#define db puts(---)
using namespace std;//void rd_cre() { freopen(d://dp//data.txt,w,stdout); srand(time(NULL)); }
//void rd_ac() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//AC.txt,w,stdout); }
//void rd_wa() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//WA.txt,w,stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pairint,int PII;const int N1000010,mod1e97,INF0x3f3f3f3f;
const double eps1e-6;int n,root,x,y,z;
int tot;
int dp[N];
struct Node {int l,r;int id,size,rank,fa,val;int pos;
}tr[N];void pushup(int u) {tr[u].sizetr[tr[u].l].sizetr[tr[u].r].size1;tr[u].valmax(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val));
}int newnode(int v,int i) {int utot;tr[u].ltr[u].r0;tr[u].rankrand(); tr[u].size1;tr[u].valv;tr[u].posi;dp[i]v;return u;
}void split(int u,int k,int x,int y) {if(!u) { xy0; return; }if(ktr[tr[u].l].size) yu,split(tr[u].l,k,x,tr[u].l);else xu,split(tr[u].r,k-tr[tr[u].l].size-1,tr[u].r,y);pushup(u);
}int merge(int u,int v) {if(!u||!v) return vu;if(tr[u].ranktr[v].rank) {tr[u].rmerge(tr[u].r,v);pushup(u);return u;}else {tr[v].lmerge(u,tr[v].l);pushup(v);return v;}
}void dfs(int u) {if(!u) return;dfs(tr[u].l);printf(%d %d\n,u,tr[u].val);dfs(tr[u].r);
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf(%d,n);for(int i1;in;i) {int pos; scanf(%d,pos); split(root,pos,x,y);int valtr[x].val;//couti valendl;//cout**i valendl;//dfs(x);rootmerge(merge(x,newnode(val1,i)),y);printf(%d\n,tr[root].val);//db;}return 0;
}
/**/