Advertisement
Dang_Quan_10_Tin

SUPCOM (Duyên hải 11 2019)

Jul 27th, 2022
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #define task "SUPCOM"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 1e5 + 5;
  12. template <class T>
  13. void read(T &x)
  14. {
  15.     x = 0;
  16.     register int c;
  17.     while ((c = getchar()) && (c > '9' || c < '0'))
  18.         ;
  19.     for (; c >= '0' && c <= '9'; c = getchar())
  20.         x = x * 10 + c - '0';
  21. }
  22.  
  23. template <class T>
  24. void readsign(T &x)
  25. {
  26.     x = 0;
  27.     register int c, neg = 0;
  28.     while ((c = getchar()) && (c > '9' || c < '0'))
  29.         if (c == '-')
  30.             neg = 1;
  31.     for (; c >= '0' && c <= '9'; c = getchar())
  32.         x = x * 10 + c - '0';
  33.     if (neg)
  34.         x = -x;
  35. }
  36. constexpr int lift = 1e9 + 1;
  37.  
  38. struct node
  39. {
  40.     int v;
  41.     ll sum;
  42.     node *child[2];
  43.     node()
  44.     {
  45.         child[0] = child[1] = NULL;
  46.         sum = v = 0;
  47.     }
  48. };
  49.  
  50. #define bit(i, x) (((x) >> (i)) & 1)
  51.  
  52. struct Trie
  53. {
  54.     node beg;
  55.     void Add(int v)
  56.     {
  57.         int x = v + lift,
  58.             id = 30;
  59.         node *a = &beg;
  60.  
  61.         while (1)
  62.         {
  63.             if (id == -1)
  64.             {
  65.                 a->sum += v;
  66.                 ++(a->v);
  67.                 return;
  68.             }
  69.  
  70.             if (!(a->child[bit(id, x)]))
  71.                 a->child[bit(id, x)] = new node;
  72.  
  73.             a->sum += v;
  74.             ++(a->v);
  75.  
  76.             a = a->child[bit(id, x)];
  77.  
  78.             --id;
  79.         }
  80.     }
  81.  
  82.     ll Get(int x)
  83.     {
  84.         ll ans(0);
  85.         int id(30);
  86.         node *a = &beg;
  87.  
  88.         while (1)
  89.         {
  90.             if (id == -1)
  91.             {
  92.                 ans += a->sum;
  93.                 break;
  94.             }
  95.  
  96.             if (bit(id, x))
  97.             {
  98.                 if (a->child[0])
  99.                     ans += a->child[0]->sum;
  100.                 if (!(a->child[1]))
  101.                     break;
  102.                 a = a->child[1];
  103.             }
  104.             else
  105.             {
  106.                 if (!(a->child[0]))
  107.                     break;
  108.                 a = a->child[0];
  109.             }
  110.             --id;
  111.         }
  112.  
  113.         return ans;
  114.     }
  115.  
  116.     ll Query2(int a, int b)
  117.     {
  118.         return Get(b + lift) - Get(a - 1 + lift);
  119.     }
  120.  
  121.     ll Gets(int z)
  122.     {
  123.         int id(30);
  124.         node *a = &beg;
  125.  
  126.         ll ans(0);
  127.  
  128.         while (1)
  129.         {
  130.  
  131.             if (id == -1)
  132.             {
  133.                 ans += (a->v == 0 ? 0 : (a->sum) / (a->v) * z);
  134.                 break;
  135.             }
  136.  
  137.             if (a->child[0] && a->child[0]->v >= z)
  138.                 a = a->child[0];
  139.             else
  140.             {
  141.                 if (a->child[0])
  142.                 {
  143.                     ans += a->child[0]->sum;
  144.                     z -= a->child[0]->v;
  145.                 }
  146.                 a = a->child[1];
  147.             }
  148.  
  149.             --id;
  150.         }
  151.  
  152.         return ans;
  153.     }
  154.  
  155.     int Find(int z)
  156.     {
  157.         int id(30);
  158.         node *a = &beg;
  159.  
  160.         int ans(0);
  161.  
  162.         while (1)
  163.         {
  164.             if (id == -1)
  165.                 break;
  166.  
  167.             if (a->child[0] && a->child[0]->v >= z)
  168.             {
  169.                 a = a->child[0];
  170.                 ans = ans * 2;
  171.             }
  172.             else
  173.             {
  174.                 ans = ans * 2 + 1;
  175.                 if (a->child[0])
  176.                     z -= a->child[0]->v;
  177.                 a = a->child[1];
  178.             }
  179.  
  180.             --id;
  181.         }
  182.  
  183.         return ans - lift;
  184.     }
  185.  
  186.     ll Query3(int l, int r)
  187.     {
  188.         ll ans = Gets(r) - Gets(l - 1);
  189.  
  190.         int x = Find(r),
  191.             y = Find(l);
  192.  
  193.         Add(x - 1);
  194.         Add(y + 1);
  195.  
  196.         return ans;
  197.     }
  198. } f;
  199.  
  200. int n;
  201.  
  202. void Read()
  203. {
  204.     //cin >> n;
  205.     read(n);
  206. }
  207.  
  208. void Solve()
  209. {
  210.     while (n--)
  211.     {
  212.         int t;
  213.         //cin >> t;
  214.         read(t);
  215.  
  216.         if (t == 1)
  217.         {
  218.             int v;
  219.             //cin >> v;
  220.             readsign(v);
  221.             f.Add(v);
  222.         }
  223.         else
  224.         {
  225.             int a, b;
  226.             //cin >> a >> b;
  227.             readsign(a);
  228.             readsign(b);
  229.             if (t == 2)
  230.                 cout << f.Query2(a, b) << "\n";
  231.             else
  232.                 cout << f.Query3(a, b) << "\n";
  233.         }
  234.     }
  235. }
  236.  
  237. int32_t main()
  238. {
  239.     ios::sync_with_stdio(0);
  240.     cin.tie(0);
  241.     cout.tie(0);
  242.     if (fopen(task ".INP", "r"))
  243.     {
  244.         freopen(task ".INP", "r", stdin);
  245.         freopen(task ".OUT", "w", stdout);
  246.     }
  247.  
  248.     Read();
  249.     Solve();
  250. }
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement