日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[BZOJ1500][NOI2005]维修数列(splay)

發(fā)布時(shí)間:2024/4/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [BZOJ1500][NOI2005]维修数列(splay) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

1500: [NOI2005]維修數(shù)列

Time Limit: 10 Sec??Memory Limit: 64 MB
Submit: 16266??Solved: 5410
[Submit][Status][Discuss]

Description

請(qǐng)寫一個(gè)程序,要求維護(hù)一個(gè)數(shù)列,支持以下 6 種操作: 請(qǐng)注意,格式欄 中的下劃線‘ _ ’表示實(shí)際輸入文件中的空格

Input

輸入的第1 行包含兩個(gè)數(shù)N 和M(M ≤20 000),N 表示初始時(shí)數(shù)列中數(shù)的個(gè)數(shù),M表示要進(jìn)行的操作數(shù)目。
第2行包含N個(gè)數(shù)字,描述初始時(shí)的數(shù)列。
以下M行,每行一條命令,格式參見問題描述中的表格。
任何時(shí)刻數(shù)列中最多含有500 000個(gè)數(shù),數(shù)列中任何一個(gè)數(shù)字均在[-1 000, 1 000]內(nèi)。
插入的數(shù)字總數(shù)不超過4 000 000個(gè),輸入文件大小不超過20MBytes。

Output

對(duì)于輸入數(shù)據(jù)中的GET-SUM和MAX-SUM操作,向輸出文件依次打印結(jié)果,每個(gè)答案(數(shù)字)占一行。

Sample Input

9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM

Sample Output

-1
10
1
10

HINT

Source

[Submit][Status][Discuss]

戰(zhàn)勝恐懼去寫就行了,沒什么難的,不過是板子題。

主要問題就是如果模板不是非常熟練的話,幾乎不可能在寫代碼之前將所有問題都考慮清楚,這就需要gdb和輸出調(diào)試的能力,一個(gè)datamaker和一個(gè)網(wǎng)上的標(biāo)程對(duì)拍還是比較有效的。有幾個(gè)需要注意的問題:

1. cov標(biāo)記只存0和1,不要存具體應(yīng)該下放的數(shù)。因?yàn)镸AKE-SAME的數(shù)可能為0,而v[x]本身存的就是需要下放的數(shù)。

2. 垃圾要回收,用棧就可以了,這題不卡時(shí)間和空間,可以放心的寫。

3. 這題MAX-SUM要求至少包含一個(gè)數(shù)(也就是這個(gè)區(qū)間全為負(fù)數(shù)的時(shí)候不能輸出0),mx[]存的是至少包含一個(gè)數(shù)的最大區(qū)間和,mxl[]和mxr[]存的是靠左/右的連續(xù)一段的最大和(可以為0)。這樣就需要mx[0]=-inf

4. 多push()和upd(),其余細(xì)節(jié)考慮清楚就可以了

寫了一個(gè)小時(shí),調(diào)了三個(gè)小時(shí)。

BZOJ 150T 留念

?

1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #define rep(i,l,r) for (int i=l; i<=r; i++) 5 using namespace std; 6 7 const int N=1000100,inf=1000000000; 8 int n,m,pos,tot,rt,top,k,nd,f[N],sz[N],v[N],sm[N],mxl[N],mxr[N],mx[N],ch[N][2],stk[N],C[N],R[N],a[N]; 9 char op[20]; 10 11 int get(){ int x; if (top) return x=stk[top--]; return ++nd; } 12 void thr(int x){ f[x]=v[x]=sm[x]=mxl[x]=mxr[x]=mx[x]=ch[x][0]=ch[x][1]=0; stk[++top]=x; } 13 14 void cov(int x,int k){ 15 sm[x]=sz[x]*k; v[x]=k; 16 mxl[x]=mxr[x]=(k>0)?sm[x]:0; mx[x]=(k>0)?sm[x]:k; 17 C[x]=1; 18 } 19 20 void rev(int x){ swap(ch[x][0],ch[x][1]); swap(mxl[x],mxr[x]); R[x]^=1; } 21 22 void push(int x){ 23 if (C[x]){ 24 if (ch[x][0]) cov(ch[x][0],v[x]); 25 if (ch[x][1]) cov(ch[x][1],v[x]); C[x]=0; 26 } 27 if (R[x]){ 28 if (ch[x][0]) rev(ch[x][0]); 29 if (ch[x][1]) rev(ch[x][1]); R[x]=0; 30 } 31 } 32 33 void upd(int x){ 34 int ls=ch[x][0],rs=ch[x][1]; 35 sz[x]=sz[ls]+sz[rs]+1; sm[x]=sm[ls]+sm[rs]+v[x]; 36 mx[x]=max(max(mx[ls],mx[rs]),mxr[ls]+mxl[rs]+v[x]); 37 mxl[x]=max(mxl[ls],sm[ls]+mxl[rs]+v[x]); 38 mxr[x]=max(mxr[rs],sm[rs]+mxr[ls]+v[x]); 39 } 40 41 int build(int l,int r){ 42 int mid=(l+r)>>1; 43 int x=get(); v[x]=sm[x]=mx[x]=a[mid]; 44 mxl[x]=mxr[x]=(a[mid]>0)?a[mid]:0; 45 if (l<mid) f[ch[x][0]=build(l,mid-1)]=x; 46 if (mid<r) f[ch[x][1]=build(mid+1,r)]=x; 47 upd(x); return x; 48 } 49 50 void rot(int &rt,int x){ 51 int y=f[x],z=f[y],w=ch[y][1]==x; 52 if (y==rt) rt=x; else ch[z][ch[z][1]==y]=x; 53 f[x]=z; f[y]=x; f[ch[x][w^1]]=y; 54 ch[y][w]=ch[x][w^1]; ch[x][w^1]=y; upd(y); 55 } 56 57 void splay(int &rt,int x){ 58 while (x!=rt){ 59 int y=f[x],z=f[y]; 60 if (y!=rt){ if ((ch[z][0]==y)^(ch[y][0]==x)) rot(rt,x); else rot(rt,y); } 61 rot(rt,x); 62 } 63 upd(x); 64 } 65 66 int find(int x,int k){ 67 push(x); 68 if (sz[ch[x][0]]+1==k) return x; 69 if (k<=sz[ch[x][0]]) return find(ch[x][0],k); 70 else return find(ch[x][1],k-sz[ch[x][0]]-1); 71 } 72 73 void ins(int pos,int p){ 74 int x=find(rt,pos),y=find(rt,pos+1); 75 splay(rt,x); splay(ch[rt][1],y); 76 ch[y][0]=p; f[p]=y; upd(y); upd(x); 77 } 78 79 void era(int x){ 80 push(x); 81 if (ch[x][0]) era(ch[x][0]); 82 if (ch[x][1]) era(ch[x][1]); 83 thr(x); 84 } 85 86 int split(int x,int y){ 87 x=find(rt,x); y=find(rt,y); splay(rt,x); splay(ch[x][1],y); 88 push(ch[y][0]); return ch[y][0]; 89 } 90 91 void del(int x,int y){ 92 x=find(rt,x); y=find(rt,y); splay(rt,x); splay(ch[x][1],y); 93 era(ch[y][0]); ch[y][0]=0; upd(y); upd(x); 94 } 95 96 int main(){ 97 freopen("bzoj1500.in","r",stdin); 98 freopen("bzoj1500.out","w",stdout); 99 scanf("%d%d",&n,&m); mx[0]=-inf; 100 rep(i,2,n+1) scanf("%d",&a[i]); a[1]=-inf; a[n+2]=inf; 101 rt=build(1,n+2); 102 while (m--){ 103 scanf("%s",op); 104 if (op[0]=='I'){ 105 scanf("%d%d",&pos,&tot); rep(i,1,tot) scanf("%d",&a[i]); 106 int x=build(1,tot); ins(pos+1,x); 107 } 108 if (op[0]=='D') scanf("%d%d",&pos,&tot),del(pos,pos+tot+1); 109 if (op[0]=='M' && op[2]=='K') scanf("%d%d%d",&pos,&tot,&k),cov(split(pos,pos+tot+1),k); 110 if (op[0]=='R') scanf("%d%d",&pos,&tot),rev(split(pos,pos+tot+1)); 111 if (op[0]=='G') scanf("%d%d",&pos,&tot),printf("%d\n",sm[split(pos,pos+tot+1)]); 112 if (op[0]=='M' && op[2]=='X') printf("%d\n",mx[split(1,sz[rt])]); 113 } 114 return 0; 115 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/HocRiser/p/8547359.html

總結(jié)

以上是生活随笔為你收集整理的[BZOJ1500][NOI2005]维修数列(splay)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。