Advertisement
ivnikkk

Untitled

Apr 25th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <cmath>
  7. #include <iomanip>
  8. #include <algorithm>
  9. #include <map>
  10. #include <set>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <stack>
  14. #include <queue>
  15. #include <random>
  16.  
  17. #define ll int
  18. #define ld long double
  19. #define mp make_pair
  20. #define all(v) (v).begin(), (v).end()
  21.  
  22. using namespace std;
  23. #include <cstdio>
  24.  
  25. /** Interface */
  26.  
  27. inline int readChar();
  28. template <class T = int> inline T readInt();
  29. template <class T> inline void writeInt(T x, char end = 0);
  30. inline void writeChar(int x);
  31. inline void writeWord(const char* s);
  32.  
  33. /** Read */
  34.  
  35. static const int buf_size = 4096;
  36.  
  37. inline int getChar() {
  38.     static char buf[buf_size];
  39.     static int len = 0, pos = 0;
  40.     if (pos == len)
  41.         pos = 0, len = fread(buf, 1, buf_size, stdin);
  42.     if (pos == len)
  43.         return -1;
  44.     return buf[pos++];
  45. }
  46.  
  47. inline int readChar() {
  48.     int c = getChar();
  49.     while (c <= 32)
  50.         c = getChar();
  51.     return c;
  52. }
  53.  
  54. template <class T>
  55. inline T readInt() {
  56.     int s = 1, c = readChar();
  57.     T x = 0;
  58.     if (c == '-')
  59.         s = -1, c = getChar();
  60.     while ('0' <= c && c <= '9')
  61.         x = x * 10 + c - '0', c = getChar();
  62.     return s == 1 ? x : -x;
  63. }
  64.  
  65. /** Write */
  66.  
  67. static int write_pos = 0;
  68. static char write_buf[buf_size];
  69.  
  70. inline void writeChar(int x) {
  71.     if (write_pos == buf_size)
  72.         fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  73.     write_buf[write_pos++] = x;
  74. }
  75.  
  76. template <class T>
  77. inline void writeInt(T x, char end) {
  78.     if (x < 0)
  79.         writeChar('-'), x = -x;
  80.  
  81.     char s[24];
  82.     int n = 0;
  83.     while (x || !n)
  84.         s[n++] = '0' + x % 10, x /= 10;
  85.     while (n--)
  86.         writeChar(s[n]);
  87.     if (end)
  88.         writeChar(end);
  89. }
  90.  
  91. inline void writeWord(const char* s) {
  92.     while (*s)
  93.         writeChar(*s++);
  94. }
  95.  
  96. struct Flusher {
  97.     ~Flusher() {
  98.         if (write_pos)
  99.             fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  100.     }
  101. } flusher;
  102.  
  103. const ll inf = 1e18, siz = 350; // 350
  104.  
  105. ll n, m;
  106. vector<vector<ll>> b1;
  107.  
  108. ll get_res(vector<ll>& a, ll b, ll c) {
  109.     ll l1 = -1, r1 = (ll)a.size() - 1;
  110.     while (r1 - l1 > 1) {
  111.         ll mid = (l1 + r1) / 2;
  112.         if (a[mid] < b) l1 = mid;
  113.         else r1 = mid;
  114.     }
  115.  
  116.     if (a[r1] < b || a[r1] > c) return 0;
  117.  
  118.     ll l2 = 0, r2 = (ll)a.size();
  119.     while (r2 - l2 > 1) {
  120.         ll mid = (l2 + r2) / 2;
  121.         if (a[mid] <= c) l2 = mid;
  122.         else r2 = mid;
  123.     }
  124.  
  125.     if (a[l2] < b || a[l2] > c) return 0;
  126.  
  127.     return l2 - l1;
  128. }
  129.  
  130. void run() {
  131.    // cin >> n >> m;
  132.     n = readInt(), m = readInt();
  133.     vector<ll> v(n);
  134.     for (ll i = 0; i < n / siz; i++) {
  135.         vector<ll> cur;
  136.         for (ll j = 0; j < siz; j++) {
  137.            // cin >> v[i * siz + j];
  138.             v[i * siz + j] = readInt();
  139.             cur.push_back(v[i * siz + j]);
  140.         }
  141.         b1.push_back(cur);
  142.     }
  143.     vector<ll> cur;
  144.     for (ll i = 0; i < n % siz; i++) {
  145.         cin >> v[(n / siz) * siz + i];
  146.         v.push_back(v[(n / siz) * siz + i]);
  147.         cur.push_back(v[(n / siz) * siz + i]);
  148.     }
  149.     if (!cur.empty()) b1.push_back(cur);
  150.  
  151.     for (auto& to : b1) {
  152.         sort(all(to));
  153.     }
  154.  
  155.     while (m--) {
  156.         ll x, y, k, l;
  157.        // cin >> x >> y >> k >> l;
  158.         x = readInt();
  159.         y = readInt();
  160.         k = readInt();
  161.         l = readInt();
  162.         x--, y--;
  163.         ll ans = 0;
  164.  
  165.         if (y - x + 1 <= siz) {
  166.             for (ll i = x; i <= y; i++) {
  167.                 if (k <= v[i] && v[i] <= l) ans++;
  168.             }
  169.             writeInt(ans);
  170.             writeWord("\n");
  171.             //cout << ans << "\n";
  172.             continue;
  173.         }
  174.  
  175.         ll ind = 0;
  176.         for (ll i = 0; i < (ll)b1.size(); i++) {
  177.             ll lb = ind, rb = ind + (ll)b1[i].size() - 1;
  178.             if (x <= lb && rb <= y) {
  179.                 ans += get_res(b1[i], k, l);
  180.             }
  181.             else if (lb <= x && x <= rb) {
  182.                 for (ll j = x; j <= rb; j++) {
  183.                     if (k <= v[j] && v[j] <= l) ans++;
  184.                 }
  185.             }
  186.             else if (lb <= y && y <= rb) {
  187.                 for (ll j = lb; j <= y; j++) {
  188.                     if (k <= v[j] && v[j] <= l) ans++;
  189.                 }
  190.             }
  191.             ind += (ll)b1[i].size();
  192.         }
  193.  
  194.         writeInt(ans);
  195.         writeWord("\n");
  196.         //cout << ans << "\n";
  197.     }
  198. }
  199.  
  200. int main() {
  201.   //  ios::sync_with_stdio(false);
  202.    // cin.tie(nullptr);
  203.  
  204.     ll t = 1;
  205.     //cin >> t;
  206.     while (t--) {
  207.         run();
  208.         writeWord("\n");
  209.     }
  210.  
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement