首页 > 其他分享 >珂朵莉树合集

珂朵莉树合集

时间:2022-08-20 11:24:00浏览次数:102  
标签:int split 朵莉树 cht mp res 合集 first

Color the Ball

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

//conllexpr int mod = 998244353;
//conllexpr int mod = 1000000007;

template <typename T> 
class ChthollyTree {
 public:
  map<int, T> mp;
  ChthollyTree() { mp[0] = 0; }
  inline void split(int x) {
    auto it = prev(mp.upper_bound(x));
    mp[x] = it->second;
  }
  inline T assign(int l, int r, T x) {
    split(l);
    split(r + 1);
    auto it = mp.find(l);
    while (it->first != r + 1) {
      it->second = x;
      it = mp.erase(it);
    }
    mp[l] = x;
  }
};

void solve() {
  int n;
  while (cin >> n) {
    ChthollyTree<int> cht;
    for (int i = 0; i < n; i++) {
      int l, r;
      cin >> l >> r;
      char ch;
      cin >> ch;
      int color = 0;
      if (ch == 'w') {
        color = 1;
      } else {
        color = 0;
      }
      cht.assign(l, r, color);
    }
    bool ok = 0;
    int len = -1;
    int ll = 0, rr = 0;
    int mxl = 0, mxr = 0;
    for (auto it = cht.mp.begin(); it != cht.mp.end(); it++) {
      if (it->second == 1 && ok == 0) {
        ll = it->first;
        rr = next(it)->first - 1;
        ok = 1;
      } else if (it->second == 0 && ok == 1) {
        rr = it->first - 1;
        ok = 0;
        if (len < rr - ll + 1) {
          mxl = ll;
          mxr = rr;
          len = rr - ll + 1;
        }
      }
    }
    if (len < rr - ll + 1) {
      mxl = ll;
      mxr = rr;
      len = rr - ll + 1;
    }
    if (len == -1) {
      cout << "Oh, my god" << '\n';
    } else {
      cout << mxl << " " << mxr << '\n';
    }
  }
}

int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout << setprecision(6);
  int tt = 1;
  //cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}

Physical Education Lessons

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

//constexpr int mod = 998244353;
//constexpr int mod = 1000000007;

template <typename T> 
class ChthollyTree {
 public:
  map<int, T> mp;
  ChthollyTree() { mp[0] = 0; }
  inline void split(int x) {
    auto it = prev(mp.upper_bound(x));
    mp[x] = it->second;
  }
  inline T get(int l, int r, T x) {
    T res = 0;
    split(l);
    split(r + 1);
    auto it = mp.find(l);
    while (it->first != r + 1) {
      res -= it->second * (next(it)->first - it->first);
      it = mp.erase(it);
    }
    mp[l] = x;
    res += x * (r - l + 1);
    return res;
  }
};

void solve() {
  int n, q;
  cin >> n >> q;
  ChthollyTree<int> cht;
  cht.mp[1] = 1;
  cht.mp[n + 1] = 0;
  int ans = n;
  for (int i = 0; i < q; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    ans += cht.get(l, r, k - 1);
    cout << ans << '\n';
  }
}

int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  //cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}

Willem, Chtholly and Seniorious

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

//constexpr int mod = 998244353;
constexpr int mod = 1000000007;

template<typename T, typename U>
inline T POW(T a, U b, int p) {
  T res = 1;
  a %= p;
  while (b) {
    if (b & 1) res = res * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return res;
}

template<typename T>
class Chthollytree {
 public:
  map<int, T> mp;
  Chthollytree() { mp[0] = 0; }
  inline void split(int x) {
    auto it = prev(mp.upper_bound(x));
    mp[x] = it->second;
  }
  inline void assign(int l, int r, T x) {
    split(l);
    split(r + 1);
    auto it = mp.find(l);
    while (it->first != r + 1) {
      it = mp.erase(it);
    }
    mp[l] = x;
  }  
  inline void add(int l, int r, T x) {
    split(l);
    split(r + 1);
    auto it = mp.find(l);
    while (it->first != r + 1) {
      it->second += x;
      it = next(it);
    }
  }
  inline T get(int l, int r, int x, int y) {
    i64 res = 0;
    split(l);
    split(r + 1);
    auto it = mp.find(l);
    while (it->first != r + 1) {
      res = (res + POW(it->second, x, y) * (next(it)->first - it->first) % y) % y;
      it = next(it);
    }
    return res;
  }
  inline T nth_element(int l, int r, int k) {
    split(l);
    split(r + 1);
    auto it = mp.find(l);
    vector<pair<T, T>> a;
    while (it->first != r + 1) {
      a.push_back({it->second, next(it)->first - it->first});
      it = next(it);
    }
    sort(a.begin(), a.end());
    for (int i = 0, cnt = 0; ; i++) {
      if (k <= (cnt += a[i].second)) {
        return a[i].first;
      }
    }
  }
};

void solve() {
  int n, m, seed, vmax;
  auto rnd = [&]() {
    int ret = seed;
    seed = (seed * 7LL + 13) % mod;
    return ret;    
  };
  cin >> n >> m >> seed >> vmax;
  Chthollytree<i64> cht;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    a[i] = (rnd() % vmax) + 1;
    cht.mp[i] = a[i];
  }
  for (int i = 1; i <= m; ++i) {
    int op, l, r, x, y;
    op = (rnd() % 4) + 1;
    l = (rnd() % n);
    r = (rnd() % n);
    if (l > r) {
      swap(l, r);
    }
    if (op == 3) {
      x = (rnd() % (r - l + 1)) + 1;
    } else {
      x = (rnd() % vmax) + 1;
    }
    if (op == 4) {
      y = (rnd() % vmax) + 1;
    }
    if (op == 1) {
      cht.add(l, r, x);
    } else if (op == 2) {    
      cht.assign(l, r, x);
    } else if (op == 3) {
      cout << cht.nth_element(l, r, x) << '\n';
    } else {
      cout << cht.get(l, r, x, y) << '\n';
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  //cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    solve();
  }
  return 0;
}

标签:int,split,朵莉树,cht,mp,res,合集,first
From: https://www.cnblogs.com/kiddingma/p/16607367.html

相关文章