[关闭]
@y20070316 2017-02-14T06:11:24.000000Z 字数 6142 阅读 935

GDKOI2017模拟赛

OI 校内赛


【bzoj1824】path - A*+堆求K短路

  1. #include <cstdio>
  2. #include <cctype>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6. #define F(i,a,b) for (int i=(a);i<=(b);i++)
  7. #define X first
  8. #define Y second
  9. #define LL long long
  10. LL rd(void);
  11. const int N=1024;
  12. const LL MAX=(LL)1e16;
  13. int n,m;
  14. struct Ed {
  15. int v;
  16. LL d;
  17. Ed(int _v=0,LL _d=0) {
  18. v=_v;
  19. d=_d;
  20. }
  21. };
  22. vector<Ed> fac_g[N],rev_g[N];
  23. int s,t,k;
  24. int vis[N];
  25. LL h[N];
  26. priority_queue< pair<LL,int> > q;
  27. struct Info {
  28. LL f;
  29. LL g;
  30. int id;
  31. Info(LL _f=0,LL _g=0,int _id=0) {
  32. f=_f;
  33. g=_g;
  34. id=_id;
  35. }
  36. friend int operator < (Info a,Info b) {
  37. return a.f>b.f;
  38. }
  39. };
  40. priority_queue<Info> q2;
  41. int cnt;
  42. int main(void) {
  43. #ifndef ONLINE_JUDGE
  44. freopen("A.in","r",stdin);
  45. freopen("A.out","w",stdout);
  46. #endif
  47. n=rd(),m=rd();
  48. F(i,1,m) {
  49. int x=rd(),y=rd();
  50. LL z=rd();
  51. fac_g[x].push_back(Ed(y,z));
  52. rev_g[y].push_back(Ed(x,z));
  53. }
  54. s=rd(),t=rd(),k=rd();
  55. fill(h,h+N,MAX);
  56. q.push(make_pair(0,t));
  57. while (!q.empty()) {
  58. pair<LL,int> x=q.top();
  59. q.pop();
  60. if (vis[x.Y])
  61. continue;
  62. h[x.Y]=-x.X;
  63. vis[x.Y]=1;
  64. F(i,1,rev_g[x.Y].size())
  65. if (!vis[rev_g[x.Y][i-1].v])
  66. q.push(make_pair(-h[x.Y]-rev_g[x.Y][i-1].d,rev_g[x.Y][i-1].v));
  67. }
  68. if (h[s]==MAX) {
  69. printf("-1\n");
  70. return 0;
  71. }
  72. q2.push(Info(0+h[s],0,s));
  73. while (!q2.empty()) {
  74. Info x=q2.top();
  75. q2.pop();
  76. if (x.id==t) {
  77. cnt++;
  78. if (cnt==k) {
  79. printf("%lld\n",x.g);
  80. return 0;
  81. }
  82. }
  83. F(i,1,fac_g[x.id].size())
  84. q2.push(Info(x.g+fac_g[x.id][i-1].d+h[fac_g[x.id][i-1].v],x.g+fac_g[x.id][i-1].d,fac_g[x.id][i-1].v));
  85. }
  86. printf("-1\n");
  87. return 0;
  88. }
  89. LL rd(void) {
  90. LL x=0,f=1; char c=getchar();
  91. while (!isdigit(c)) {
  92. if (c=='-') f=-1;
  93. c=getchar();
  94. }
  95. while (isdigit(c)) {
  96. x=x*10+c-'0';
  97. c=getchar();
  98. }
  99. return x*f;
  100. }

【xsy1822】set - 可持久化并查集

以后写完题目的时候,若样例比较费,要自己再手出一下。

  1. #include <cstdio>
  2. #include <cctype>
  3. #include <algorithm>
  4. using namespace std;
  5. #define F(i,a,b) for (int i=(a);i<=(b);i++)
  6. int rd(void);
  7. const int M=32768;
  8. const int S=1048576;
  9. int n,m;
  10. int tot,root[M];
  11. struct Info {
  12. int f;
  13. int g;
  14. Info(int _f=0,int _g=0) {
  15. f=_f;
  16. g=_g;
  17. }
  18. };
  19. struct Tree {
  20. int l,r;
  21. int lc,rc;
  22. Info data;
  23. }tr[S];
  24. int Copynode(int frm) {
  25. int x=++tot;
  26. tr[x]=tr[frm];
  27. return x;
  28. }
  29. int Build(int l,int r) {
  30. int x=++tot;
  31. tr[x].l=l,tr[x].r=r;
  32. if (l!=r) {
  33. int mid=(l+r)>>1;
  34. tr[x].lc=Build(l,mid);
  35. tr[x].rc=Build(mid+1,r);
  36. }
  37. else tr[x].data=Info(l,0);
  38. return x;
  39. }
  40. Info Query(int x,int loc) {
  41. if (tr[x].l==tr[x].r)
  42. return tr[x].data;
  43. int mid=(tr[x].l+tr[x].r)>>1;
  44. if (loc<=mid)
  45. return Query(tr[x].lc,loc);
  46. else return Query(tr[x].rc,loc);
  47. }
  48. Info Find(int rt,int loc) {
  49. Info f_pos=Query(rt,loc);
  50. while (loc!=f_pos.f) {
  51. loc=f_pos.f;
  52. f_pos=Query(rt,loc);
  53. }
  54. return f_pos;
  55. }
  56. int Modify(int frm,int loc,Info data) {
  57. int x=Copynode(frm);
  58. if (tr[x].l==tr[x].r) {
  59. tr[x].data=data;
  60. return x;
  61. }
  62. int mid=(tr[x].l+tr[x].r)>>1;
  63. if (loc<=mid)
  64. tr[x].lc=Modify(tr[frm].lc,loc,data);
  65. else tr[x].rc=Modify(tr[frm].rc,loc,data);
  66. return x;
  67. }
  68. int main(void) {
  69. #ifndef ONLINE_JUDGE
  70. freopen("B.in","r",stdin);
  71. freopen("B.out","w",stdout);
  72. #endif
  73. n=rd(),m=rd();
  74. root[0]=Build(1,n);
  75. F(i,1,m) {
  76. int kd=rd();
  77. if (kd==1) {
  78. int a=rd(),b=rd();
  79. Info fa=Find(root[i-1],a);
  80. Info fb=Find(root[i-1],b);
  81. if (fa.f==fb.f)
  82. root[i]=root[i-1];
  83. else {
  84. if (fa.g>fb.g)
  85. swap(fa,fb);
  86. int t=Modify(root[i-1],fa.f,Info(fb.f,fa.g));
  87. root[i]=Modify(t,fb.f,Info(fb.f,max(fa.g+1,fb.g)));
  88. }
  89. }
  90. else if (kd==2) {
  91. int k=rd();
  92. root[i]=root[k];
  93. }
  94. else {
  95. int a=rd(),b=rd();
  96. Info fa=Find(root[i-1],a);
  97. Info fb=Find(root[i-1],b);
  98. printf("%d\n",fa.f==fb.f);
  99. root[i]=root[i-1];
  100. }
  101. }
  102. return 0;
  103. }
  104. int rd(void) {
  105. int x=0,f=1; char c=getchar();
  106. while (!isdigit(c)) {
  107. if (c=='-') f=-1;
  108. c=getchar();
  109. }
  110. while (isdigit(c)) {
  111. x=x*10+c-'0';
  112. c=getchar();
  113. }
  114. return x*f;
  115. }

【xsy1825】sequence - 非旋Treap

注意字符串的空间问题。
至少要开大2,而不是1!!

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cctype>
  4. #include <ctime>
  5. #include <algorithm>
  6. #include <cassert>
  7. using namespace std;
  8. #define F(i,a,b) for (int i=(a);i<=(b);i++)
  9. #define LL long long
  10. LL rd(void);
  11. const int N=262144;
  12. int n;
  13. char s[16];
  14. int m;
  15. int root,tot;
  16. struct Treap {
  17. int lc,rc;
  18. LL key;
  19. int fix;
  20. int siz;
  21. int rev_tag;
  22. LL delta;
  23. LL min_val;
  24. }tr[N];
  25. struct D {
  26. int lc,rc;
  27. D(int _lc=0,int _rc=0) {
  28. lc=_lc,rc=_rc;
  29. }
  30. };
  31. int Newnode(LL val) {
  32. int x=++tot;
  33. tr[x].lc=tr[x].rc=0;
  34. tr[x].key=val;
  35. tr[x].fix=rand();
  36. tr[x].siz=1;
  37. tr[x].rev_tag=0;
  38. tr[x].delta=0;
  39. tr[x].min_val=val;
  40. return x;
  41. }
  42. void Update(int x) {
  43. int lc=tr[x].lc;
  44. int rc=tr[x].rc;
  45. tr[x].siz=tr[lc].siz+1+tr[rc].siz;
  46. tr[x].min_val=tr[x].key;
  47. if (lc>0)
  48. tr[x].min_val=min(tr[x].min_val,tr[lc].min_val);
  49. if (rc>0)
  50. tr[x].min_val=min(tr[x].min_val,tr[rc].min_val);
  51. }
  52. void Rev(int x) {
  53. tr[x].rev_tag^=1;
  54. swap(tr[x].lc,tr[x].rc);
  55. }
  56. void Push_del(int x,LL d) {
  57. tr[x].key+=d;
  58. tr[x].delta+=d;
  59. tr[x].min_val+=d;
  60. }
  61. void Clear(int x) {
  62. int lc=tr[x].lc;
  63. int rc=tr[x].rc;
  64. if (tr[x].rev_tag) {
  65. if (lc>0)
  66. Rev(lc);
  67. if (rc>0)
  68. Rev(rc);
  69. tr[x].rev_tag=0;
  70. }
  71. if (tr[x].delta!=0) {
  72. if (lc>0)
  73. Push_del(lc,tr[x].delta);
  74. if (rc>0)
  75. Push_del(rc,tr[x].delta);
  76. tr[x].delta=0;
  77. }
  78. }
  79. int Merge(int x,int y) {
  80. if (!x||!y)
  81. return x+y;
  82. if (tr[x].fix<tr[y].fix) {
  83. Clear(x);
  84. tr[x].rc=Merge(tr[x].rc,y);
  85. Update(x);
  86. return x;
  87. }
  88. else {
  89. Clear(y);
  90. tr[y].lc=Merge(x,tr[y].lc);
  91. Update(y);
  92. return y;
  93. }
  94. }
  95. D Split(int x,int rk) {
  96. if (!rk)
  97. return D(0,x);
  98. Clear(x);
  99. D t;
  100. if (rk<=tr[tr[x].lc].siz) {
  101. t=Split(tr[x].lc,rk);
  102. tr[x].lc=t.rc;
  103. Update(x);
  104. t.rc=x;
  105. }
  106. else {
  107. t=Split(tr[x].rc,rk-tr[tr[x].lc].siz-1);
  108. tr[x].rc=t.lc;
  109. Update(x);
  110. t.lc=x;
  111. }
  112. return t;
  113. }
  114. void Add(int &rt,int x,int y,LL d) {
  115. D tx=Split(rt,y);
  116. D ty=Split(tx.lc,x-1);
  117. Push_del(ty.rc,d);
  118. rt=Merge(Merge(ty.lc,ty.rc),tx.rc);
  119. }
  120. void Reverse(int &rt,int x,int y) {
  121. D tx=Split(rt,y);
  122. D ty=Split(tx.lc,x-1);
  123. Rev(ty.rc);
  124. rt=Merge(Merge(ty.lc,ty.rc),tx.rc);
  125. }
  126. void Revolve(int &rt,int x,int y,int t) {
  127. D tx=Split(rt,y);
  128. D ty=Split(tx.lc,x-1);
  129. t%=tr[ty.rc].siz;
  130. D tz=Split(ty.rc,tr[ty.rc].siz-t);
  131. rt=Merge(Merge(ty.lc,Merge(tz.rc,tz.lc)),tx.rc);
  132. }
  133. void Insert(int &rt,int rk,LL val) {
  134. D tx=Split(rt,rk);
  135. int x=Newnode(val);
  136. rt=Merge(Merge(tx.lc,x),tx.rc);
  137. }
  138. void Delete(int &rt,int rk) {
  139. D tx=Split(rt,rk-1);
  140. D ty=Split(tx.rc,1);
  141. rt=Merge(tx.lc,ty.rc);
  142. }
  143. LL GetMin(int rt,int x,int y) {
  144. D tx=Split(rt,y);
  145. D ty=Split(tx.lc,x-1);
  146. LL ans=tr[ty.rc].min_val;
  147. rt=Merge(Merge(ty.lc,ty.rc),tx.rc);
  148. return ans;
  149. }
  150. int main(void) {
  151. #ifndef ONLINE_JUDGE
  152. freopen("C.in","r",stdin);
  153. freopen("C.out","w",stdout);
  154. #endif
  155. srand(time(0));
  156. n=rd();
  157. F(i,1,n) {
  158. LL x=rd();
  159. root=Merge(root,Newnode(x));
  160. }
  161. m=rd();
  162. F(i,1,m) {
  163. scanf("%s",s+1);
  164. if (s[1]=='A') {
  165. int x=rd(),y=rd();
  166. LL d=rd();
  167. Add(root,x,y,d);
  168. }
  169. else if (s[1]=='R'&&s[4]=='E') {
  170. int x=rd(),y=rd();
  171. Reverse(root,x,y);
  172. }
  173. else if (s[1]=='R'&&s[4]=='O') {
  174. int x=rd(),y=rd(),t=rd();
  175. Revolve(root,x,y,t);
  176. }
  177. else if (s[1]=='I') {
  178. int x=rd(); LL p=rd();
  179. Insert(root,x,p);
  180. }
  181. else if (s[1]=='D') {
  182. int x=rd();
  183. Delete(root,x);
  184. }
  185. else if (s[1]=='M') {
  186. int x=rd(),y=rd();
  187. LL ans=GetMin(root,x,y);
  188. printf("%lld\n",ans);
  189. }
  190. }
  191. return 0;
  192. }
  193. LL rd(void) {
  194. LL x=0,f=1; char c=getchar();
  195. while (!isdigit(c)) {
  196. if (c=='-') f=-1;
  197. c=getchar();
  198. }
  199. while (isdigit(c)) {
  200. x=x*10+c-'0';
  201. c=getchar();
  202. }
  203. return x*f;
  204. }

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注