Advertisement
Guest User

Untitled

a guest
May 3rd, 2014
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <algorithm>
  4. using namespace std;
  5. int a,b,c,d,e,f;
  6. struct point
  7. {
  8. int x;
  9. int y;
  10. };
  11. point tree[4*30000];
  12. char q[30000+9];
  13. void moguce()
  14. {
  15. if(tree[1].x==0 && tree[1].y==0)printf("YES\n");
  16. else printf("NO\n");
  17. return;
  18. }
  19. void ini()
  20. {
  21. int b=0;
  22. for(b=1;b<a;b*=2){}
  23. //printf("%d\n",b+a-1);
  24.  
  25. for(int i=b+a-1;i>1;--i)
  26. {
  27. if(i%2==1)
  28. {
  29. //printf("Cvor %d %d %d\n",i,tree[i].x,tree[i].y);
  30. tree[i/2].x=tree[i].x;
  31. tree[i/2].y=tree[i].y;
  32. }
  33. else
  34. {
  35. //ako lijevom treba ) a desni ih ima više onda moramo vidjeti koji ima više i u to zabilježiti koje
  36. int br=0;
  37. //printf("Cvor %d. %d-> %d %d -> %d %d\n",i/2,i,tree[i].x,tree[i].y,tree[i/2].x,tree[i/2].y);
  38. if(tree[i/2].y>tree[i].x && tree[i].x>0){tree[i/2].y-=tree[i].x;}
  39. else if(tree[i/2].y<=tree[i].x && tree[i/2].y>0){tree[i/2].x+=(tree[i].x-tree[i/2].y);tree[i/2].y=0;}
  40. else
  41. {
  42. tree[i/2].y+=tree[i].y;
  43. tree[i/2].x+=tree[i].x;
  44. }
  45. }
  46. }
  47. /*for(int i=1;i<=b+a-1;++i)
  48. {
  49. printf("%d. %d %d\n",i,tree[i].x,tree[i].y);
  50. }*/
  51. }
  52. void postavi(int pos)
  53. {
  54. int broj=1;
  55. for(broj=1;broj<a;broj*=2){}
  56. int pozicija=broj+pos;
  57. //printf("%d\n",pozicija);
  58. tree[pozicija].x=1-tree[pozicija].x;
  59. tree[pozicija].y=1-tree[pozicija].y;
  60. int pozicija2=pozicija;
  61. pozicija2/=2;
  62. while(pozicija2>0)
  63. {
  64. tree[pozicija2].x=tree[2*pozicija2+1].x;
  65. tree[pozicija2].y=tree[2*pozicija2+1].y;
  66. if(tree[pozicija2].y>tree[2*pozicija2].x && tree[2*pozicija2].x>0){tree[pozicija2].y-=tree[2*pozicija2].x;}
  67. else if(tree[pozicija2].y<=tree[2*pozicija2].x && tree[pozicija2].y>0){tree[pozicija2].x+=(tree[2*pozicija2].x-tree[pozicija2].y);tree[pozicija2].y=0;}
  68. else
  69. {
  70. tree[pozicija2].y+=tree[2*pozicija2].y;
  71. tree[pozicija2].x+=tree[2*pozicija2].x;
  72. }
  73. pozicija2/=2;
  74. }
  75. return;
  76. }
  77. int tt(int cvor,int from,int to,int a,int b)
  78. {
  79. if(from>=a && to<=b)
  80. {
  81. return tree[cvor].x;
  82. }
  83. int m1,m2;
  84. if((from+to)>=a)m1=tt(cvor*2,from,(from+to)/2,a,b);
  85. if((from+to)+1<=b)m2=tt(cvor*2+1,(from+to)/2+1,to,a,b);
  86. return max(m1,m2);
  87. }
  88. int main()
  89. {
  90. int p=0;
  91. while(scanf("%d",&a) == 1)
  92. {
  93. scanf("%s",q);
  94. scanf("%d",&b);
  95. printf("Test %d:\n",p+1);
  96. ++p;
  97. int ba=1;
  98. for(ba=1;ba<a;ba*=2){}
  99. for(int i=0;i<a;++i)
  100. {
  101. if(q[i]=='(')tree[i+ba].x=1;
  102. if(q[i]==')')tree[i+ba].y=1;
  103. }
  104. ini();
  105. for(int i=0;i<b;++i)
  106. {
  107. scanf("%d",&c);
  108. if(c==0){moguce();}
  109. else postavi(c-1);
  110. }
  111. for(int i=1;i<ba+a+6;++i)tree[i].x=0,tree[i].y=0;
  112. }
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement