ATSTNG

A5. Alakazam

Sep 16th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define E(A,B) for (int A = 0; A < B; A++)
  6. #define R(F,V,C) V = F(V,C);
  7. #define dout if ( 0 ) cout
  8.  
  9. typedef long long int i64;
  10.  
  11. const int L = 0;
  12. const int R = 1;
  13. const int V = 2;
  14.  
  15. int n, q;
  16. set<tuple<int, int, double>> ss;
  17.  
  18. bool split(int x) {
  19.     if (x > n) return false;
  20.  
  21.     auto seg = ss.lower_bound(make_tuple(x, INT_MAX, INT_MAX)); seg--;
  22.  
  23.     int l = get<L>(*seg);
  24.     int r = get<R>(*seg);
  25.     double v = get<V>(*seg);
  26.     double e = v / (r-l);
  27.  
  28.     if (l == x) return false;
  29.  
  30.     ss.erase(seg);
  31.     ss.insert(make_tuple(l, x, e*(x-l)));
  32.     ss.insert(make_tuple(x, r, e*(r-x)));
  33.  
  34.     return true;
  35. }
  36.  
  37. void unite(int l, int r) {
  38.     split(l);
  39.     split(r);
  40.  
  41.     auto seg = ss.lower_bound(make_tuple(l, INT_MAX, INT_MAX)); seg--;
  42.     double nv = 0;
  43.  
  44.     bool running = true;
  45.     while (running && seg != ss.end()) {
  46.         int segl = get<L>(*seg);
  47.         int segr = get<R>(*seg);
  48.         double segv = get<V>(*seg);
  49.  
  50.         if (segr == r) running = false;
  51.  
  52.         nv += segv;
  53.  
  54.         auto segz = seg;
  55.         seg++;
  56.         ss.erase(segz);
  57.     }
  58.  
  59.     ss.insert(make_tuple(l, r, nv));
  60. }
  61.  
  62. double query(int x) {
  63.     auto seg = ss.lower_bound(make_tuple(x, INT_MAX, INT_MAX)); seg--;
  64.  
  65.     int l = get<L>(*seg);
  66.     int r = get<R>(*seg);
  67.     double v = get<V>(*seg);
  68.  
  69.     return v / (r-l);
  70. }
  71.  
  72. void solve() {
  73.     cin >> n >> q;
  74.     E(i,n) {
  75.         int v;
  76.         cin >> v;
  77.         ss.insert(make_tuple(i+1, i+2, v));
  78.     }
  79.  
  80.     E(_,q) {
  81.         int l, r, x;
  82.         string t;
  83.         cin >> t;
  84.  
  85.         if (t == "get") {
  86.             cin >> x;
  87.             cout << query(x) << "\n";
  88.         } else {
  89.             cin >> l >> r;
  90.             unite(l, r+1);
  91.         }
  92.     }
  93. }
  94.  
  95. /**
  96.  
  97. 3 8
  98. 1 2 3
  99. get 1
  100. get 3
  101. shuffle 1 2
  102. shuffle 2 3
  103. get 1
  104. get 3
  105. shuffle 1 3
  106. get 2
  107.  
  108. */
  109.  
  110. int main() {
  111.     string problem_name = "xxx";
  112.  
  113.     // freopen((problem_name + ".in").c_str(), "r", stdin); freopen((problem_name + ".out").c_str(), "w", stdout);
  114.  
  115.     cout << setprecision(20) << fixed;
  116.  
  117.     ios_base::sync_with_stdio(false);
  118.     cin.tie(0);
  119.     cout.tie(0);
  120.  
  121.     solve();
  122. }
Advertisement
Add Comment
Please, Sign In to add comment