Advertisement
bogolyubskiyalexey

2018/2019 region tour 1 task 3

Feb 17th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <unordered_map>
  7. using namespace std;
  8.  
  9. using i32 = int32_t;
  10. using ui32 = uint32_t;
  11.  
  12.  
  13.  
  14. class TTree {
  15. private:
  16.     ui32 Size;
  17.     vector<i32> Data;
  18.  
  19. public:
  20.     TTree(ui32 size) {
  21.         for (Size = 1; Size < size; Size *= 2);
  22.         Data.resize(2 * Size - 1);
  23.     }
  24.  
  25.     void Change(ui32 index, i32 value) {
  26.         index += Size - 1;
  27.         if (index >= Data.size()) {
  28.             return;
  29.         }
  30.         Data[index] += value;
  31.         while (index) {
  32.             index = (index - 1) / 2;
  33.             Data[index] += value;
  34.         }
  35.     }
  36.  
  37.     i32 GetSum(ui32 left, ui32 right) {
  38.         if (left > right) {
  39.             return 0;
  40.         }
  41.         return GetSum(0, 0, Size - 1, left, right);
  42.     }
  43.  
  44.     i32 GetValue(ui32 index) {
  45.         return Data[index + Size - 1];
  46.     }
  47.  
  48.  
  49. private:
  50.     i32 GetSum(ui32 index, ui32 realLeft, ui32 realRight, ui32 queryLeft, ui32 queryRight) const {
  51.         if (queryLeft <= realLeft && realRight <= queryRight) {
  52.             return Data[index];
  53.         }
  54.         if (queryRight < realLeft || realRight < queryLeft) {
  55.             return 0;
  56.         }
  57.         ui32 middle = (realLeft + realRight) / 2;
  58.         return GetSum(index * 2 + 1, realLeft, middle, queryLeft, queryRight)
  59.             + GetSum(index * 2 + 2, middle + 1, realRight, queryLeft, queryRight);
  60.     }
  61. };
  62.  
  63. void pushToCloud(ui32 value, set<ui32>& cloud, vector<ui32>& items) {
  64.     cloud.insert(value);
  65.     items.push_back(value);
  66.     //cout << value << " ";
  67. }
  68.  
  69. void fillNext(const vector<ui32>& items, vector<ui32>& next) {
  70.     unordered_map<ui32, ui32> next_index;
  71.     next.resize(items.size());
  72.     ui32 size = items.size();
  73.     for (ui32 i = size; i >= 1; --i) {
  74.         ui32 index = i - 1;
  75.         if (next_index.count(items[index])) {
  76.             next[index] = next_index[items[index]];
  77.         }
  78.         else {
  79.             next[index] = size;
  80.         }
  81.         next_index[items[index]] = index;
  82.     }
  83. }
  84.  
  85.  
  86. int main() {
  87.     ui32 N, M;
  88.     scanf("%u%u", &N, &M);
  89.  
  90.     vector<ui32> a(M), b(N);
  91.     for (ui32 i = 0; i < M; ++i) {
  92.         scanf("%u", &a[i]);
  93.     }
  94.     for (ui32 i = 0; i < N; ++i) {
  95.         scanf("%u", &b[i]);
  96.     }
  97.  
  98.     ui32 current = 0;
  99.     vector<ui32> items;
  100.     set<ui32> cloud;
  101.     for (ui32 i = 0; i < M; ++i) {
  102.         ui32 found = false;
  103.         if (current < N && a[i] == b[current]) {
  104.             found = true;
  105.             pushToCloud(b[current++], cloud, items);
  106.         }
  107.         else {
  108.             if (cloud.count(a[i])) {
  109.                 pushToCloud(a[i], cloud, items);
  110.                 found = true;
  111.             }
  112.         }
  113.         while (!found) {
  114.             if (b[current] == a[i]) {
  115.                 found = true;
  116.             }
  117.             pushToCloud(b[current++], cloud, items);
  118.         }
  119.     }
  120.  
  121.     vector<ui32> next;
  122.     fillNext(items, next);
  123.  
  124.     TTree tree(items.size());
  125.     for (ui32 i = 0; i < items.size(); ++i) {
  126.         tree.Change(i, 1);
  127.     }
  128.     for (ui32 i = 0; i < items.size(); ++i) {
  129.         if (next[i] != items.size() && tree.GetValue(i)) {
  130.             ui32 index = next[i];
  131.             while (index < items.size()) {
  132.                 tree.Change(index, -1);
  133.                 index = next[index];
  134.             }
  135.         }
  136.     }
  137.  
  138.     printf("%u\n", items.size());
  139.     for (ui32 i = 0; i < items.size(); ++i) {
  140.         i32 countOfDifferent = tree.GetSum(i + 1, next[i] - 1);
  141.         printf("%d ", countOfDifferent + 1);
  142.         tree.Change(next[i], 1);
  143.     }
  144.     printf("\n");
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement