Submission #1086163

# Submission time Handle Problem Language Result Execution time Memory
1086163 2024-09-09T16:22:38 Z TgX_2 Index (COCI21_index) C++17
110 / 110
2485 ms 57592 KB
/*-----------------------------
        Author : TgX.2
       11Ti - K28 - CHV
-----------------------------*/

#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC optimize("O1")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,mmx,abm")

#include<bits/stdc++.h>
using namespace std;

static struct FastInput {
    static constexpr int BUF_SIZE = 1 << 20;
    char buf[BUF_SIZE];
    size_t chars_read = 0;
    size_t buf_pos = 0;
    FILE *in = stdin;
    char cur = 0;

        inline char get_char() {
        if (buf_pos >= chars_read) {
        chars_read = fread(buf, 1, BUF_SIZE, in);
        buf_pos = 0;
        buf[0] = (chars_read == 0 ? -1 : buf[0]);
        }
        return cur = buf[buf_pos++];
    }

    inline void tie(int) {}

    inline explicit operator bool() {
        return cur != -1;
    }

    inline static bool is_blank(char c) {
        return c <= ' ';
    }

    inline bool skip_blanks() {
        while (is_blank(cur) && cur != -1) {
        get_char();
        }
        return cur != -1;
    }

    inline FastInput& operator>>(char& c) {
        skip_blanks();
        c = cur;
        return *this;
    }

    inline FastInput& operator>>(string& s) {
        if (skip_blanks()) {
        s.clear();
        do {
            s += cur;
        } while (!is_blank(get_char()));
        }
        return *this;
    }

    template <typename T>
    inline FastInput& read_integer(T& n) {
        // unsafe, doesn't check that characters are actually digits
        n = 0;
        if (skip_blanks()) {
        int sign = +1;
        if (cur == '-') {
            sign = -1;
            get_char();
        }
        do {
            n += n + (n << 3) + cur - '0';
        } while (!is_blank(get_char()));
        n *= sign;
        }
        return *this;
    }

    template <typename T>
    inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>(T& n) {
        return read_integer(n);
    }

    #if !defined(_WIN32) | defined(_WIN64)
    inline FastInput& operator>>(__int128& n) {
        return read_integer(n);
    }
    #endif

    template <typename T>
    inline typename enable_if<is_floating_point<T>::value, FastInput&>::type operator>>(T& n) {
        // not sure if really fast, for compatibility only
        n = 0;
        if (skip_blanks()) {
        string s;
        (*this) >> s;
        sscanf(s.c_str(), "%lf", &n);
        }
        return *this;
    }
    } fast_input;

    #define cin fast_input

    static struct FastOutput {
    static constexpr int BUF_SIZE = 1 << 20;
    char buf[BUF_SIZE];
    size_t buf_pos = 0;
    static constexpr int TMP_SIZE = 1 << 20;
    char tmp[TMP_SIZE];
    FILE *out = stdout;

    inline void put_char(char c) {
        buf[buf_pos++] = c;
        if (buf_pos == BUF_SIZE) {
        fwrite(buf, 1, buf_pos, out);
        buf_pos = 0;
        }
    }

    ~FastOutput() {
        fwrite(buf, 1, buf_pos, out);
    }

    inline FastOutput& operator<<(char c) {
        put_char(c);
        return *this;
    }

    inline FastOutput& operator<<(const char* s) {
        while (*s) {
        put_char(*s++);
        }
        return *this;
    }

    inline FastOutput& operator<<(const string& s) {
        for (int i = 0; i < (int) s.size(); i++) {
        put_char(s[i]);
        }
        return *this;
    }

    template <typename T>
    inline char* integer_to_string(T n) {
        char* p = tmp + TMP_SIZE - 1;
        if (n == 0) {
        *--p = '0';
        } else {
        bool is_negative = false;
        if (n < 0) {
            is_negative = true;
            n = -n;
        }
        while (n > 0) {
            *--p = (char) ('0' + n % 10);
            n /= 10;
        }
        if (is_negative) {
            *--p = '-';
        }
        }
        return p;
    }

    template <typename T>
    inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) {
        return integer_to_string(n);
    }

    #if !defined(_WIN32) || defined(_WIN64)
    inline char* stringify(__int128 n) {
        return integer_to_string(n);
    }
    #endif

    template <typename T>
    inline typename enable_if<is_floating_point<T>::value, char*>::type stringify(T n) {
        sprintf(tmp, "%.17f", n);
        return tmp;
    }

    template <typename T>
    inline FastOutput& operator<<(const T& n) {
        auto p = stringify(n);
        for (; *p != 0; p++) {
        put_char(*p);
        }
        return *this;
    }
} fast_output;

#define cout fast_output
#define endl '\n'

#define FOR(i, a, b)       for (int i = (a), _b = (b); i <= _b; i += 1)
#define FORD(i, a, b)      for (int i = (a), _b = (b); i >= _b; i -= 1)
#define FORC(i, a, b, c)   for (int i = (a), _b = (b), _c = (c); i <= _b; i += _c)

#define fi                 first
#define se                 second
#define pb                 push_back
#define len(x)             (int)((x).size())
#define all(x)             (x).begin(), (x).end()

#define _                  << " " <<
#define __                 << "\n"

#define ______________TgX______________ main()
#define int                long long
#define intmax             1e9
#define intmin            -1e9
#define llongmax           1e18
#define llongmin          -1e18
#define memo(a, val)       memset((a), (val), sizeof((a)))

using   namespace std;
typedef long long          ll;
typedef pair<int, int>     pii;

template<typename T1, typename T2> 
bool mini(T1 &a, T2 b)
    {if (a > b) a = b; else return 0; return 1;}

template<typename T1, typename T2> 
bool maxi(T1 &a, T2 b)
    {if (a < b) a = b; else return 0; return 1;}
/*-----------------------------*/

template<typename T>
class FastSegTree {
private: 
    vector<vector<T>> seg;
    int n;

public:
    FastSegTree(const vector<T> &a) :
        n(len(a)), seg(len(a) << 1, vector<T>()) {
        FOR(i, 0, n - 1)
            seg[i + n].pb(a[i]);
        FORD(id, n - 1, 1) {
            merge(all(seg[id << 1]), all(seg[id << 1 | 1]), back_inserter(seg[id]));
        }
    }

    int get(int l, int r, T val) {
        int ans = 0;
        for(l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) {
                ans += seg[l].end() - lower_bound(all(seg[l]), val);
                l++;
            } 
            if (r & 1) {
                r--;
                ans += seg[r].end() - lower_bound(all(seg[r]), val);
            }
        }
        return ans;
    }

    int query(int u, int v) {
        int l = 0, r = (v - u + 1);
        int ans = intmin;
        while(l <= r) {
            int mid = (l + r) >> 1;
            if (get(u, v, mid) >= mid) {
                ans = mid;
                l = mid + 1;
            } else r = mid - 1;
        }
        return ans;
    }
};


int n, q;
void process() {
    cin >> n >> q;
    vector<int> a(n);
    FOR(i, 0, n - 1)
        cin >> a[i];

    FastSegTree<int> segtree(a);
    while(q--) {
        int l, r; cin >> l >> r;
        cout << segtree.query(l - 1, r - 1) __ ;
    }
}



/*-----------------------------*/
______________TgX______________ {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("temp.inp", "r")) {
        freopen("temp.inp", "r", stdin);
        freopen("temp.out", "w", stdout);
    }
    process();
}


/*==============================+
|INPUT                          |
--------------------------------|

================================+
|OUTPUT                         |
--------------------------------|

===============================*/

Compilation message

index.cpp:253:41: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  253 | #define ______________TgX______________ main()
      |                                         ^~~~
index.cpp:337:1: note: in expansion of macro '______________TgX______________'
  337 | ______________TgX______________ {
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index.cpp: In instantiation of 'FastSegTree<T>::FastSegTree(const std::vector<_Tp>&) [with T = long long int]':
index.cpp:327:31:   required from here
index.cpp:278:9: warning: 'FastSegTree<long long int>::n' will be initialized after [-Wreorder]
  278 |     int n;
      |         ^
index.cpp:277:23: warning:   'std::vector<std::vector<long long int>, std::allocator<std::vector<long long int> > > FastSegTree<long long int>::seg' [-Wreorder]
  277 |     vector<vector<T>> seg;
      |                       ^~~
index.cpp:281:5: warning:   when initialized here [-Wreorder]
  281 |     FastSegTree(const vector<T> &a) :
      |     ^~~~~~~~~~~
index.cpp: In function 'int main()':
index.cpp:341:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  341 |         freopen("temp.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:342:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  342 |         freopen("temp.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 3 ms 2652 KB Output is correct
3 Correct 3 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 2 ms 2528 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 3 ms 2904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 3 ms 2652 KB Output is correct
3 Correct 3 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 2 ms 2528 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 3 ms 2904 KB Output is correct
11 Correct 376 ms 15448 KB Output is correct
12 Correct 377 ms 15448 KB Output is correct
13 Correct 418 ms 15448 KB Output is correct
14 Correct 370 ms 15448 KB Output is correct
15 Correct 370 ms 15700 KB Output is correct
16 Correct 391 ms 15444 KB Output is correct
17 Correct 388 ms 15448 KB Output is correct
18 Correct 388 ms 15448 KB Output is correct
19 Correct 398 ms 15448 KB Output is correct
20 Correct 384 ms 15444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 3 ms 2652 KB Output is correct
3 Correct 3 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 2 ms 2528 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 3 ms 2904 KB Output is correct
11 Correct 376 ms 15448 KB Output is correct
12 Correct 377 ms 15448 KB Output is correct
13 Correct 418 ms 15448 KB Output is correct
14 Correct 370 ms 15448 KB Output is correct
15 Correct 370 ms 15700 KB Output is correct
16 Correct 391 ms 15444 KB Output is correct
17 Correct 388 ms 15448 KB Output is correct
18 Correct 388 ms 15448 KB Output is correct
19 Correct 398 ms 15448 KB Output is correct
20 Correct 384 ms 15444 KB Output is correct
21 Correct 2400 ms 57252 KB Output is correct
22 Correct 2417 ms 57356 KB Output is correct
23 Correct 2455 ms 57284 KB Output is correct
24 Correct 2485 ms 57324 KB Output is correct
25 Correct 2419 ms 57284 KB Output is correct
26 Correct 2472 ms 57272 KB Output is correct
27 Correct 2441 ms 57592 KB Output is correct
28 Correct 2432 ms 57376 KB Output is correct
29 Correct 2392 ms 57296 KB Output is correct
30 Correct 2440 ms 57388 KB Output is correct