Advertisement
a53

TSM

a53
Mar 5th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define maxN 1000005
  3. #define lsb(x) ((x)&(-x))
  4. using namespace std;
  5. int aib[maxN];
  6. int viz[maxN];
  7. int n,last,x,op;
  8.  
  9. class InParser {
  10. private:
  11. FILE *fin;
  12. char *buff;
  13. int sp;
  14.  
  15. char read_ch() {
  16. ++sp;
  17. if (sp==4096) {
  18. sp=0;
  19. fread(buff,1,4096,fin);
  20. }
  21. return buff[sp];
  22. }
  23.  
  24. public:
  25. InParser(const char* nume) {
  26. fin=fopen(nume,"r");
  27. buff=new char[4096]();
  28. sp=4095;
  29. }
  30.  
  31. InParser& operator >> (int &n) {
  32. char c;
  33. while(!isdigit(c=read_ch()) && c!='-');
  34. int sgn=1;
  35. if(c=='-'){
  36. n=0;
  37. sgn=-1;
  38. }else
  39. n=c-'0';
  40. while(isdigit(c=read_ch()))
  41. n=10*n+c-'0';
  42. n*=sgn;
  43. return *this;
  44. }
  45.  
  46. InParser& operator >> (long long &n) {
  47. char c;
  48. n=0;
  49. while (!isdigit(c=read_ch()) && c!='-');
  50. long long sgn=1;
  51. if(c=='-'){
  52. n=0;
  53. sgn=-1;
  54. }else
  55. n=c-'0';
  56. while(isdigit(c=read_ch()))
  57. n=10*n+c-'0';
  58. n*=sgn;
  59. return *this;
  60. }
  61. };
  62.  
  63. class OutParser{
  64. private:
  65. FILE *fout;
  66. char *buff;
  67. int sp;
  68.  
  69. void write_ch(char ch) {
  70. if (sp==50000){
  71. fwrite(buff,1,50000,fout);
  72. sp=0;
  73. buff[sp++]=ch;
  74. } else {
  75. buff[sp++]=ch;
  76. }
  77. }
  78.  
  79. public:
  80. OutParser(const char* name) {
  81. fout=fopen(name,"w");
  82. buff=new char[50000]();
  83. sp=0;
  84. }
  85. ~OutParser() {
  86. fwrite(buff,1,sp,fout);
  87. fclose(fout);
  88. }
  89.  
  90. OutParser& operator << (int vu32) {
  91. if (vu32<=9) {
  92. write_ch(vu32+'0');
  93. } else {
  94. (*this)<<(vu32/10);
  95. write_ch(vu32%10+'0');
  96. }
  97. return *this;
  98. }
  99.  
  100. OutParser& operator << (long long vu64) {
  101. if (vu64<=9) {
  102. write_ch(vu64+'0');
  103. } else {
  104. (*this)<<(vu64/10);
  105. write_ch(vu64%10+'0');
  106. }
  107. return *this;
  108. }
  109.  
  110. OutParser& operator << (char ch) {
  111. write_ch(ch);
  112. return *this;
  113. }
  114. OutParser& operator << (const char *ch) {
  115. while(*ch){
  116. write_ch(*ch);
  117. ++ch;
  118. }
  119. return *this;
  120. }
  121. };
  122.  
  123. void update(int val)
  124. {
  125. for(int i=val;i<maxN;i+=lsb(i))
  126. aib[i]++;
  127. }
  128.  
  129. void _delete(int val)
  130. {
  131. for(int i=val;i<maxN;i+=lsb(i))
  132. if(aib[i])
  133. aib[i]--;
  134. }
  135.  
  136. int query(int pos)
  137. {
  138. int res=0;
  139. for(int i=pos;i>0;i-=lsb(i))
  140. res+=aib[i];
  141. return res;
  142. }
  143.  
  144. int _search(int val)
  145. {
  146. int step,i;
  147. for(step=1;step<maxN;step<<=1);
  148. for(i=0;step;step>>=1)
  149. if(i+step<=1000000)
  150. if(val>=aib[i+step])
  151. {
  152. i+=step;
  153. val-=aib[i];
  154. }
  155. i++;
  156. while(!viz[i] && viz[i]<val) /* mai putine iteratii decat ma asteptam */
  157. i++,val-=viz[i];
  158. return i;
  159. }
  160.  
  161. int main()
  162. {
  163. InParser f("tsm.in");
  164. OutParser g("tsm.out");
  165. f>>n;
  166. while(n--)
  167. {
  168. f>>op;
  169. if(op==1){
  170. f>>x;
  171. viz[x]++;
  172. if(viz[x]==1)
  173. update(x);
  174. }
  175. if(op==2){
  176. f>>x;
  177. last=_search(x-1);
  178. g<<last<<'\n';
  179. }
  180. if(op==3){
  181. g<<_search(0)<<'\n';
  182. }
  183. if(op==4){
  184. viz[last]--;
  185. if(viz[last]==0)
  186. _delete(last);
  187. }
  188. }
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement