fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /* A map-based data structure
  6.  
  7.   [27-bit id+2-bit x] = 29-bit key
  8.   [16-bit score2+16-bit score2] = 32-bit integer score value
  9.  
  10. */
  11.  
  12. const int s_max = 65535, x_max = 2, id_max = 7e7;
  13.  
  14. using uint32_scores = vector<uint32_t>;
  15.  
  16. struct double_scores: vector<pair<double,double>>
  17. {
  18. static double int_to_score(uint32_t x)
  19. {
  20. assert(x <= s_max);
  21.  
  22. double x1 = x; x1 /= s_max; return x1;
  23. }
  24.  
  25. double_scores() {}
  26.  
  27. double_scores(const uint32_scores& s)
  28. {
  29. for(auto p: s)
  30. emplace_back(int_to_score(p&s_max),int_to_score(p>>16));
  31. }
  32. };
  33.  
  34. struct data_tuples: map<uint32_t,uint32_scores>
  35. {
  36. static uint32_t double_to_int(double s)
  37. {
  38. assert(s >= 0.0 and s <= 1.0);
  39.  
  40. return round(s *= s_max);
  41. }
  42.  
  43. void add_tuple(uint32_t id, uint32_t x, double s1, double s2)
  44. {
  45. assert(id <= id_max and x <= x_max);
  46.  
  47. uint32_t key = (id << 2)|x;
  48.  
  49. uint32_t value = (double_to_int(s2) << 16) | double_to_int(s1);
  50.  
  51. (*this)[key].push_back(value);
  52. }
  53.  
  54. double_scores tuples(uint32_t id, uint32_t x)
  55. {
  56. assert(id <= id_max and x <= x_max);
  57.  
  58. uint32_t key = (id << 2)|x; auto it = find(key);
  59.  
  60. return it != end() ? double_scores(it->second) : double_scores();
  61. }
  62.  
  63. friend ostream& operator<<(ostream& out, const data_tuples& a)
  64. {
  65. for(auto p: a)
  66. {
  67. out << '[' << setw(8) << (p.first>>2) << ',' << (p.first&3) << ']';
  68.  
  69. for(auto q: double_scores(p.second))
  70. out << '[',
  71. out << fixed << setprecision(5) << q.first << ',',
  72. out << fixed << setprecision(5) << q.second << ']';
  73.  
  74. out << '\n';
  75. }
  76.  
  77. return out;
  78. }
  79. };
  80.  
  81. int main()
  82. {
  83. data_tuples a; int n;
  84.  
  85. cin >> n, srand(time(NULL));
  86.  
  87. for(int i = 0; i < n; ++i)
  88. {
  89. int id = 1+(rand()%id_max);
  90. int x = rand()%(x_max+1);
  91. double s1 = double(rand()%(s_max+1))/s_max;
  92. double s2 = double(rand()%(s_max+1))/s_max;
  93.  
  94. a.add_tuple(id,x,s1,s2);
  95. }
  96.  
  97. cout << a;
  98. }
Success #stdin #stdout 0s 15240KB
stdin
20
stdout
[ 3476958,2][0.08582,0.43424]
[ 3636082,1][0.24848,0.77708]
[ 5929579,1][0.46490,0.22734]
[11902905,1][0.59266,0.72465]
[16136280,1][0.01297,0.65336]
[22288594,1][0.05861,0.51748]
[24674473,0][0.00946,0.14098]
[35224906,0][0.10130,0.11974]
[35289077,1][0.18408,0.67942]
[36088458,0][0.65846,0.49737]
[37631039,1][0.88070,0.62773]
[40889875,1][0.03983,0.99803]
[46008668,0][0.29400,0.59052]
[49917914,0][0.61711,0.81559]
[54605778,1][0.69306,0.23342]
[54807037,0][0.66726,0.06534]
[63748350,0][0.79892,0.83485]
[65318001,2][0.30713,0.06058]
[65827659,1][0.61192,0.57873]
[68938168,1][0.30338,0.01390]