HDU 1166 敵兵布陣

2022-05-16 03:48:20 字數 2743 閱讀 7842

用結構體做的  470+ms;

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8

9using

namespace

std;

1011

structn12

;1617struct n *creat()

1824

25int st[50010

];26

27int change(int z,int y,struct n *root)

2837 root->l =creat();

38 root->r =creat();

39int tl = change(z,m,root->l);

40int tr = change(m+1,y,root->r);

41 root->data = (tl +tr);

42return root->data;43}

4445

void add(int site,int s,int z,int y,struct n *root)

4654

55if(site <=m)

5660

else

6165}66

67void sub(int site,int s,int z,int y,struct n *root)

6876

77if(site <=m)

7882

else

8387}88

89int query(int ml,int mr,int z,int y,struct n *root)

90100

else

if(m+1

<=ml)

101104

else

105108

}109

110int

main()

111124

struct n *root =creat();

125126 root->z = 1

;127 root->y =n;

128129 change(1

,n,root);

130131 printf("

case %d:\n

",++icase);

132133

while(scanf("

%s",order) !=eof)

134143

else

if(order[0] == 's'

)144

149else

if(order[0] == 'q'

)150

156}

157158

}159

return0;

160 }

1 #include //用陣列模擬的線段樹  跑了370+  瑪德  說好的0ms呢

2 #include //在大白書 點修改 上看的 就是在不停的傳遞 樹的節點編號node

3 #include //當前節點的左子節點編號為2*node 右子節點為2*node+1;

4 #include 5 #include 6 #include 7 #include 8

9using

namespace

std;

1011

int st[100010

];12

int a[50001

];13

14int change(int node,int l,int

r)15

22 st[node] = change(node+node,l,m) + change(node+node+1,m+1

,r);

23return

st[node];24}

2526

void add(int site,int s,int node,int l,int

r)27

35if(site <=m)

3641

if(m 4247}48

49void sub(int site,int s,int node,int l,int

r)50

58if(site <=m)

5964

if(m 6570}71

72int query(int ml,int mr,int node,int l,int

r)73

78int m = (l+r)/2;79

80if(mr <=m)

8184

if(m 8588

return (query(ml,m,node+node,l,m) + query(m+1,mr,node+node+1,m+1

,r));89}

9091

intmain()

92115

else

if(order[0] == 's'

)116

121else

if(order[0] == 'q'

)122

127else

if(order[0] == 'e'

)128

break

;129

}130

131}

132return0;

133 }

hdu 1166 敵兵布陣

這是一道線段樹里最簡單的一類題目了,單點更新型別!include define lson l m rt 1 define rson m 1 r rt 1 1 const int maxx 55555 int sum maxx 2 void pushup int rt void build int l...

hdu 1166 敵兵布陣

線段樹的模版 沒什麼思想 include include include include include include include include include include include include include define ll long long define vi vec...

HDU 1166 敵兵布陣

description lily 特別喜歡養花,但是由於她的花特別多,所以照料這些花就變得不太容易。她把她的花依次排成一行,每盆花都有乙個美觀值。如果lily把某盆花照料的好的話,這盆花的美觀值就會上公升,如果照料的不好的話,這盆花的美觀值就會下降。有時,lily想知道某段連續的花的美觀值之和是多少...