題目鏈接:點擊查看
題目大意:初始時給出 n 個數字組成的數列,接下來有 m 次操作,分別對應著區間查詢最大值以及單點修改,實現 m 次操作
題目分析:線段樹裸題,就不多說了,Splay的話,就在每個節點再維護一個 val 和一個 mmax 分別代表點權和子樹的最大值,單點修改很簡單,只需要找到對應的節點修改權值就好了,而區間查詢也非常的巧妙,首先將 l - 1 這個節點旋到根節點 處,這樣根節點的右子樹中的所有節點的下標都一定比 l - 1 大了,此時再把 r + 1 這個節點旋到根節點的右兒子 處,根據SBT的性質可知,此時根節點的右兒子的左子樹 中的所有節點一定都屬于 [ l , r ] 這個區間中了,因為 mmax 維護的是子樹中的最大值,所以答案就是根節點的右兒子的左兒子 的 mmax 了
非常巧妙的思想,利用Slpay實現了區間查詢的功能,時間復雜度均攤下來也是 logn ,不過唯一的要求就是需要自己動手對模板內部實現進行一定量的修改,從而滿足自己的需求,這就需要對模板的理解有著一定程度的要求了
代碼: ?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;class Splay//存儲規則:小左大右,重復節點記錄
{#define root e[0].ch[1] //該樹的根節點private:class node{public:int mmax;int val;int v,father;//節點值,父級節點 int ch[2];//左孩子=0,右孩子=1int sum;//自己+自己下級有多少節點。在根節點為1。int recy;//記錄自己被重復了幾次};node e[N];//Splay樹主體int n,points;//使用存儲數,元素數void update(int x){e[x].sum=e[e[x].ch[0]].sum+e[e[x].ch[1]].sum+e[x].recy;e[x].mmax=max(max(e[e[x].ch[0]].mmax,e[e[x].ch[1]].mmax),e[x].val);}int identify(int x){return e[e[x].father].ch[0]==x?0:1;}void connect(int x,int f,int son)//連接函數。用法:connect(son,father,1/0){e[x].father=f;e[f].ch[son]=x;}//作用:使得x的father=f,f的son=x.void rotate(int x){int y=e[x].father;int mroot=e[y].father;int mrootson=identify(y);int yson=identify(x);int B=e[x].ch[yson^1];connect(B,y,yson);connect(y,x,(yson^1));connect(x,mroot,mrootson);update(y);update(x);}void splay(int at,int to)//將at位置的節點移動到to位置{to=e[to].father;while(e[at].father!=to){int up=e[at].father;if(e[up].father==to) rotate(at);else if(identify(up)==identify(at)){rotate(up);rotate(at);}else{rotate(at);rotate(at);}}}int crepoint(int v,int father,int num){n++;e[n].v=v;e[n].father=father;e[n].sum=e[n].recy=1;e[n].val=num;e[n].mmax=num;e[n].ch[0]=e[n].ch[1]=0;return n;}void destroy(int x)//pop后摧毀節點 {e[x].v=e[x].ch[0]=e[x].ch[1]=e[x].sum=e[x].father=e[x].recy=0;if(x==n) n--;//最大限度優化}public:void init(){points=n=root=0;e[root].v=e[root].father=e[root].sum=e[root].recy=e[root].val=e[root].mmax=e[root].ch[0]=e[root].ch[1]=0;}int getroot(){return root;}int find(int v)//用于外部的find調用{int now=root;while(true){if(e[now].v==v){
// splay(now,root);return now;}int next=v<e[now].v?0:1;if(!e[now].ch[next]) return 0;now=e[now].ch[next];}}int build(int v,int num)//內部調用的插入函數,沒有splay {points++;if(points==1)//特判無點狀態 {root=n+1;crepoint(v,0,num);}else{int now=root;while(true)//向下找到一個空節點 {e[now].sum++;//自己的下級肯定增加了一個節點 if(v==e[now].v){e[now].recy++;return now;}int next=v<e[now].v?0:1;if(!e[now].ch[next]){crepoint(v,now,num);e[now].ch[next]=n;return n;}now=e[now].ch[next];}}return 0;}void push(int v,int num)//插入元素時,先添加節點,再進行伸展 {int add=build(v,num);splay(add,root);}void pop(int v)//刪除節點 {int deal=find(v);if(!deal) return;points--;if(e[deal].recy>1){e[deal].recy--;e[deal].sum--;return;}if(!e[deal].ch[0]){root=e[deal].ch[1];e[root].father=0;}else{int lef=e[deal].ch[0];while(e[lef].ch[1]) lef=e[lef].ch[1];splay(lef,e[deal].ch[0]);int rig=e[deal].ch[1];connect(rig,lef,1);connect(lef,0,1);update(lef);}destroy(deal);}int rank(int v)//獲取值為v的元素在這棵樹里是第幾小 {int ans=0,now=root;while(true){if(e[now].v==v) {ans+=e[e[now].ch[0]].sum;splay(now,root);return ans+1;}if(now==0) return 0;if(v<e[now].v) now=e[now].ch[0];else{ans=ans+e[e[now].ch[0]].sum+e[now].recy;now=e[now].ch[1];}}return 0;}int atrank(int x)//獲取第x小的元素的值 {if(x>points) return -inf;int now=root;while(true){int minused=e[now].sum-e[e[now].ch[1]].sum;if(x>e[e[now].ch[0]].sum&&x<=minused) break;if(x<minused) now=e[now].ch[0];else{x=x-minused;now=e[now].ch[1];}}splay(now,root);return e[now].v;}int upper(int v)//尋找該值對應的一個最近的上界值 {int now=root;int result=inf;while(now){if(e[now].v>=v&&e[now].v<result) result=e[now].v;if(v<e[now].v) now=e[now].ch[0];else now=e[now].ch[1];}return result;}int lower(int v)//尋找該值對應的一個最近的下界值 {int now=root;int result=-inf;while(now){if(e[now].v<=v&&e[now].v>result) result=e[now].v;if(v>e[now].v) now=e[now].ch[1];else now=e[now].ch[0];}return result;}void UPDATE(int pos,int val){splay(find(pos),root);//旋到根 e[root].val=val;e[root].mmax=max(max(e[e[root].ch[0]].mmax,e[e[root].ch[1]].mmax),val);}int query(int l,int r){splay(find(l-1),root);splay(find(r+1),e[root].ch[1]);return e[e[e[root].ch[1]].ch[0]].mmax;}#undef root
}tree;int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n,m;while(scanf("%d%d",&n,&m)!=EOF){tree.init();tree.push(0,-inf);for(int i=1;i<=n;i++){int num;scanf("%d",&num);tree.push(i,num);}tree.push(n+1,-inf);while(m--){char op[5];scanf("%s",op);if(op[0]=='U'){int pos,val;scanf("%d%d",&pos,&val);tree.UPDATE(pos,val);}else{int l,r;scanf("%d%d",&l,&r);printf("%d\n",tree.query(l,r));}}}return 0;
}
?
總結
以上是生活随笔 為你收集整理的HDU - 1754 I Hate It(Splay-区间最大值) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。