CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Challenge 2013
    • May Cook-Off 2013
    • May Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home  » Multiples of 3 » All Submissions » Shubham [558614]
0
Your rating: None
CodeChef submission 558614 (C)

CodeChef submission 558614 (C) plaintext list. Status: TLE, problem MULTQ3, contest . By shubh09 (shubh09), 2011-05-30 22:47:02.
  1. //<O(log n),O(n)> (update time - O(log n), query time - O(n))
  2.  
  3. //working with r0,r1 - might cause a problem in this case, since we are trying to update in O(log n) time, which means that once we find a node of the segment tree which lies completely inside the range, we don't go and update its entire subtree...using r0,r1 like this - which might include deduction in values - will create complications as to how to imbibe those changes in its children and ancestors
  4.  
  5. //hence we are working with a single array instead, which takes into account the number added so far to the entire subtree
  6.  
  7. //not working for large inputs of N - not even 10000000...which was to be expected since an array of size of about n is being declared and using large arrays is not allowed...is there a workaround??
  8.  
  9. #include <stdio.h>
  10.  
  11. int min(int a,int b)
  12. {
  13. if (a<b) return a;
  14. else return b;
  15. }
  16.  
  17. int epow(int a, int b)
  18. {
  19. if (b==0) return 1;
  20. if (b%2 == 0)
  21. {
  22. int temp = epow(a,b/2);
  23. return temp*temp;
  24. }
  25. return a*epow(a,b-1);
  26. }
  27.  
  28. int logb2c(int n) //calculates the ceiling of (log n (to the base 2))
  29. {
  30. int i=1,j=0;
  31. while (i<n)
  32. {
  33. i*=2;
  34. j++;
  35. }
  36. return j;
  37. }
  38.  
  39. void view(int node, int b, int e, int a[])
  40. {
  41. printf("%d %d %d a-%d \n",node,b,e,a[node]);
  42. if (b!=e)
  43. {
  44. view((2*node),b,((b+e)/2),a);
  45. view((2*node)+1,((b+e)/2)+1,e,a);
  46. }
  47. }
  48.  
  49. void initialize(int node, int b, int e,int a[])
  50. {
  51. a[node]=0;
  52. if (b!=e)
  53. {
  54. initialize(2*node,b,(b+e)/2,a);
  55. initialize((2*node)+1,((b+e)/2)+1,e,a);
  56. }
  57. }
  58.  
  59. void update1(int node, int b, int e, int r0[], int r1[]) //not used
  60. {
  61. //base case - b=e
  62. int v = e-b+1;
  63. /* int r01 = min(v,r0[node]);
  64. int r12 = min(v,r1[node]);
  65. int r20 = min(v,v-r0[node]-r1[node]);
  66. int r0n = r0[node]-r01+r20;
  67. */
  68. int temp0,temp1;
  69. temp0=r0[node];
  70. temp1=r1[node];
  71. r0[node]=v-temp0-r1[node];
  72. r1[node]=temp0;
  73.  
  74. /* int* change;
  75. *(change)=r0[node]-temp0;
  76. *(change+1)=r1[node]-temp1;
  77. */
  78. if (b!=e)
  79. {
  80. update1((2*node),b,(b+e)/2,r0,r1);
  81. update1((2*node)+1,((b+e)/2)+1,e,r0,r1);
  82. }
  83. // return change;
  84.  
  85. }
  86.  
  87. void backtrack(int node,int r0[], int r1[], int v0, int v1) //not used
  88. {
  89. r0[node]=r0[node]+v0;
  90. r1[node]=r1[node]+v1;
  91. if (node!=1) backtrack(node/2,r0,r1,v0,v1);
  92. }
  93.  
  94. void update(int node, int b, int e, int i, int j, int a[])
  95. {
  96. int change0,change1;
  97. if (j<b || i>e) {}
  98. else if (i<=b && j>=e)
  99. {
  100. // int temp0 = r0[node],temp1=r1[node];
  101.  
  102. // update1(node,b,e,r0,r1);
  103.  
  104. a[node]++;
  105.  
  106. // change0=r0[node]-temp0;change1=r1[node]-temp1;
  107. // if (node!=1) backtrack(node/2,r0,r1,change0,change1);
  108. }
  109. else// if (b!=e)
  110. {
  111. update((2*node),b,(b+e)/2,i,j,a);
  112. update((2*node)+1,((b+e)/2)+1,e,i,j,a);
  113. }
  114. }
  115.  
  116. int query(int node, int b, int e, int i, int j, int a[], int num)
  117. {
  118. int t = (num+(a[node]%3))%3;
  119. if (j<b || i>e) return 0;
  120. if (b==e) {if (t==0) return 1; else return 0;}
  121. else /*if (i<=b && j>=e)*/ return query((2*node),b,(b+e)/2,i,j,a,t) + query((2*node)+1,((b+e)/2)+1,e,i,j,a,t);
  122. //else return query((2*node),b,(b+e)/2,i,j,r0) + query((2*node)+1,((b+e)/2)+1,e,i,j,r0);
  123. }
  124.  
  125. int main()
  126. {
  127. int n,q;
  128. scanf("%d %d", &n, &q);
  129. int height = logb2c(n)+1;
  130. int array_size=epow(2,height);
  131. int a1[array_size]; //numbering of nodes starts from 1
  132. // int rem0[array_size];
  133. // int rem1[array_size];
  134. initialize(1,0,n-1,a1);
  135. int i;
  136. int a,b,c;
  137. for (i=1;i<=q;i++)
  138. {
  139. scanf("%d %d %d", &a, &b, &c);
  140. if (a==0) //update
  141. {
  142. update(1,0,n-1,b,c,a1);
  143. }
  144. else if (a==1)
  145. {
  146. printf ("%d\n",query(1,0,n-1,b,c,a1,0));
  147. }
  148. else if (a==2) view(1,0,n-1,a1); //for checking purposes
  149. }
  150. return 0;
  151. }


Comments

  • Login or Register to post a comment.

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.