Raslboyy

segment_tree_max_count

Dec 21st, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <math.h>
  6. #include <climits>
  7.  
  8. //#pragma GCC optimize("O3, unroll-loops");
  9.  
  10. #include <list>
  11. #include <map>
  12. #include <set>
  13. #include <queue>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16.  
  17. //#include <chrono>
  18. //#include <fstream>
  19. //#include <bits/stdc++.h>
  20.  
  21. #define mp make_pair
  22. #define pb push_back
  23. #define eb emplace_back
  24. #define x first
  25. #define y second
  26. #define sz(x) (int)x.size()
  27. #define all(x) begin(x), end(x)
  28. #define rall(x) rbegin(x), rend(x)
  29. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  30. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  31.  
  32. using namespace std;
  33.  
  34. typedef unsigned long long ull;
  35. typedef long long ll;
  36. typedef long double ld;
  37. typedef pair<int, int> pi;
  38. typedef pair<ll, ll> pl;
  39. typedef vector<int> veci;
  40. typedef vector<bool> vecb;
  41. typedef vector<vector<int>> vvi;
  42. typedef vector<vector<bool>> vvb;
  43.  
  44. const int INF_I = 1e9;
  45. const ll INF_LL = 1e18;
  46.  
  47. veci a;
  48. vector<pi> st;
  49. int n;
  50.  
  51. pi com(pi a, pi b) {
  52.     if (a.x > b.x)
  53.         return a;
  54.     if (b.x > a.x)
  55.         return b;
  56.     return mp(a.x, a.y + b.y);
  57. }
  58.  
  59. void build(int c = 1, int tl = 0, int tr = n - 1) {
  60.     if (tl == tr) {
  61.         st[c] = mp(a[tl], 1);
  62.         return;
  63.     }
  64.     int tm = (tl + tr) >> 1;
  65.     build(c << 1, tl, tm);
  66.     build((c << 1) + 1, tm + 1, tr);
  67.     st[c] = com(st[c << 1], st[(c << 1) + 1]);
  68. }
  69.  
  70. pi get(int l, int r, int c = 1, int tl = 0, int tr = n - 1) {
  71.     if (l > r)
  72.         return pi();
  73.     if (tl == l && tr == r)
  74.         return st[c];
  75.     int tm = (tl + tr) >> 1;
  76.     return com(
  77.         get(l, min(r, tm), c << 1, tl, tm),
  78.         get(max(l, tm + 1), r, (c << 1) + 1, tm + 1, tr)
  79.     );
  80. }
  81.  
  82. int main() {
  83.     /*ifstream cin("in.txt");
  84.     ofstream cout("out.txt");
  85.     auto start_time = chrono::high_resolution_clock::now();*/
  86.  
  87.     ios::sync_with_stdio(false);
  88.     cin.tie(0);
  89.     cout.tie(0);
  90.  
  91.    
  92.     cin >> n;
  93.     a.assign(n, 0);
  94.     st.assign(n << 2, pi());
  95.  
  96.     FOR(i, 0, n)
  97.         cin >> a[i];
  98.  
  99.     build();
  100.  
  101.     int q;
  102.     cin >> q;
  103.     while (q--) {
  104.         int l, r;
  105.         cin >> l >> r;
  106.         pi res = get(--l, --r);
  107.         cout << res.x << " " << res.y << "\n";
  108.     }
  109.    
  110.     /*auto end_time = chrono::high_resolution_clock::now();
  111.     chrono::duration<double> duration = end_time - start_time;
  112.     cout << duration.count() << "\n";
  113.     cin.close();
  114.     cout.close();*/
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment