Advertisement
K_Y_M_bl_C

Untitled

Feb 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. //#define _GLIBCXX_DEBUG
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef pair<int, int> pii;
  8. typedef vector<int> vi;
  9.  
  10. #define mk make_pair
  11. #define inb push_back
  12. #define X first
  13. #define Y second
  14. #define all(v) v.begin(), v.end()
  15. #define sqr(x) (x) * (x)
  16.  
  17. /** Fast input-output */
  18.  
  19. template <class T = int> inline T readInt();                       
  20. inline double readDouble();
  21. inline int readUInt();                   
  22. inline int readChar(); // first non-blank character
  23. inline void readWord( char *s );
  24. inline bool readLine( char *s ); // do not save '\n'
  25. inline bool isEof();
  26. inline int getChar();
  27. inline int peekChar();
  28. inline bool seekEof();
  29. inline void skipBlanks();
  30.  
  31. template <class T> inline void writeInt( T x, char end = 0, int len = -1 );
  32. inline void writeChar( int x );
  33. inline void writeWord( const char *s );
  34. inline void writeDouble( double x, int len = 0 );
  35. inline void flush();
  36.  
  37. static struct buffer_flusher_t {
  38.     ~buffer_flusher_t() {
  39.         flush();
  40.     }
  41. } buffer_flusher;
  42.  
  43. /** Read */
  44.  
  45. static const int buf_size = 4096;
  46.  
  47. static unsigned char buf[buf_size];
  48. static int buf_len = 0, buf_pos = 0;
  49.  
  50. inline bool isEof() {
  51.     if (buf_pos == buf_len) {
  52.         buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
  53.         if (buf_pos == buf_len)
  54.             return 1;
  55.     }
  56.     return 0;
  57. }
  58.  
  59. inline int getChar() {
  60.     return isEof() ? -1 : buf[buf_pos++];
  61. }
  62.  
  63. inline int peekChar() {
  64.     return isEof() ? -1 : buf[buf_pos];
  65. }
  66.  
  67. inline bool seekEof() {
  68.     int c;
  69.     while ((c = peekChar()) != -1 && c <= 32)
  70.         buf_pos++;
  71.     return c == -1;
  72. }
  73.  
  74. inline void skipBlanks() {
  75.     while (!isEof() && buf[buf_pos] <= 32U)
  76.         buf_pos++;
  77. }
  78.  
  79. inline int readChar() {
  80.     int c = getChar();
  81.     while (c != -1 && c <= 32)
  82.         c = getChar();
  83.     return c;
  84. }
  85.  
  86. inline int readUInt() {
  87.     int c = readChar(), x = 0;
  88.     while ('0' <= c && c <= '9')
  89.         x = x * 10 + c - '0', c = getChar();
  90.     return x;
  91. }
  92.  
  93. template <class T>
  94. inline T readInt() {
  95.     int s = 1, c = readChar();
  96.     T x = 0;
  97.     if (c == '-')
  98.         s = -1, c = getChar();
  99.     else if (c == '+')
  100.         c = getChar();
  101.     while ('0' <= c && c <= '9')
  102.         x = x * 10 + c - '0', c = getChar();
  103.     return s == 1 ? x : -x;
  104. }
  105.  
  106. inline double readDouble() {
  107.     int s = 1, c = readChar();
  108.     double x = 0;
  109.     if (c == '-')
  110.         s = -1, c = getChar();
  111.     while ('0' <= c && c <= '9')
  112.         x = x * 10 + c - '0', c = getChar();
  113.     if (c == '.') {
  114.         c = getChar();
  115.         double coef = 1;
  116.         while ('0' <= c && c <= '9')
  117.             x += (c - '0') * (coef *= 1e-1), c = getChar();
  118.     }
  119.     return s == 1 ? x : -x;
  120. }
  121.  
  122. inline void readWord( char *s ) {
  123.     int c = readChar();
  124.     while (c > 32)
  125.         *s++ = c, c = getChar();
  126.     *s = 0;
  127. }
  128.  
  129. inline bool readLine( char *s ) {
  130.     int c = getChar();
  131.     while (c != '\n' && c != -1)
  132.         *s++ = c, c = getChar();
  133.     *s = 0;
  134.     return c != -1;
  135. }
  136.  
  137. /** Write */
  138.  
  139. static int write_buf_pos = 0;
  140. static char write_buf[buf_size];
  141.  
  142. inline void writeChar( int x ) {
  143.     if (write_buf_pos == buf_size)
  144.         fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  145.     write_buf[write_buf_pos++] = x;
  146. }
  147.  
  148. inline void flush() {
  149.     if (write_buf_pos)
  150.         fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
  151. }
  152.  
  153. template <class T>
  154. inline void writeInt( T x, char end, int output_len ) {
  155.     if (x < 0)
  156.         writeChar('-'), x = -x;
  157.  
  158.     char s[24];
  159.     int n = 0;
  160.     while (x || !n)
  161.         s[n++] = '0' + x % 10, x /= 10;
  162.     while (n < output_len)
  163.         s[n++] = '0';
  164.     while (n--)
  165.         writeChar(s[n]);
  166.     if (end)
  167.         writeChar(end);
  168. }
  169.  
  170. inline void writeWord( const char *s ) {
  171.     while (*s)
  172.         writeChar(*s++);
  173. }
  174.  
  175. inline void writeDouble( double x, int output_len ) {
  176.     if (x < 0)
  177.         writeChar('-'), x = -x;
  178.     int t = (int)x;
  179.     writeInt(t), x -= t;
  180.     writeChar('.');
  181.     for (int i = output_len - 1; i > 0; i--) {
  182.         x *= 10;
  183.         t = std::min(9, (int)x);
  184.         writeChar('0' + t), x -= t;
  185.     }
  186.     x *= 10;
  187.     t = std::min(9, (int)(x + 0.5));
  188.     writeChar('0' + t);
  189. }
  190.  
  191.  
  192. int solve();
  193.  
  194. int main()
  195. {
  196.     //ios_base::sync_with_stdio(0);
  197.     //cin.tie(0);
  198. #define TASK "carno"
  199. #ifndef _DEBUG
  200.     //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  201. #endif
  202.     solve();
  203. }
  204.  
  205. const int K = 322;
  206.  
  207. struct SqrtDecomposition
  208. {
  209.     vector<vector<int>> bl;
  210.     set<int> ok;
  211.     SqrtDecomposition(int n)
  212.     {
  213.         for (int i = 0, idb = 0; i < n; i += K, ++idb)
  214.         {
  215.             bl.inb({});
  216.             for (int j = 0; i + j < min(i + K, n); ++j)
  217.             {
  218.                 ok.insert(i + j);
  219.                 bl[idb].inb(0);
  220.             }
  221.         }
  222.     };
  223.     pii get_block(int x)
  224.     {
  225.         int idb = 0;
  226.         for (auto &v : bl)
  227.         {
  228.             if (x >= (int)v.size())
  229.             {
  230.                 x -= (int)v.size();
  231.             }
  232.             else
  233.             {
  234.                 break;
  235.             }
  236.             ++idb;
  237.         }
  238.         return mk(idb, x);
  239.     }
  240.     void trykill(int idl)
  241.     {
  242.         if (!bl[idl].empty() || bl.size() == 1)
  243.             return;
  244.         bl.erase(bl.begin() + idl);
  245.     }
  246.     void balance(int idl)
  247.     {
  248.         if (bl[idl].size() < 2 * K)
  249.             return;
  250.         vi nbl;
  251.         int sz = bl[idl].size();
  252.         while ((int)bl[idl].size() >= sz / 2)
  253.         {
  254.             nbl.inb(bl[idl].back());
  255.             bl[idl].pop_back();
  256.         }
  257.         reverse(all(nbl));
  258.         bl.insert(bl.begin() + idl + 1, nbl);
  259.     }
  260.     void insert(int pos, int x)
  261.     {
  262.         pii res = get_block(pos);
  263.         int l = res.X;
  264.         int id = max(res.Y, 0);
  265.         int nxt0 = *ok.lower_bound(pos);
  266.         if (bl[l][id])
  267.         {
  268.             del(nxt0);
  269.             bl[l].insert(bl[l].begin() + id, x);
  270.         }
  271.         else
  272.         {
  273.             bl[l][id] = x;
  274.         }
  275.         ok.erase(nxt0);
  276.         balance(l);
  277.     }
  278.     void del(int pos)
  279.     {
  280.         pii res = get_block(pos);
  281.         int l = res.X;
  282.         int id = res.Y;
  283.         bl[l].erase(bl[l].begin() + id);
  284.         trykill(l);
  285.     }
  286.     void collect(vector<int>& ans)
  287.     {
  288.         ans.clear();
  289.         for (auto &v : bl)
  290.         {
  291.             for (auto &x : v)
  292.             {
  293.                 ans.inb(x);
  294.             }
  295.         }
  296.         while (!ans.back())
  297.             ans.pop_back();
  298.     }
  299. };
  300.  
  301. int solve()
  302. {
  303.     int n = readInt(), m = readInt();
  304.     SqrtDecomposition T = SqrtDecomposition(n + m);
  305.     auto printans = [&]()
  306.     {
  307.         vi ans;
  308.         T.collect(ans);
  309.         printf("%d\n", (int)ans.size());
  310.         for (int x : ans)
  311.             printf("%d ", x);
  312.         puts("");
  313.     };
  314.     for (int i = 1; i <= n; ++i)
  315.     {
  316.         T.insert(readInt() - 1, i);
  317.     }
  318.     printans();
  319.     return 0;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement