Submission #736157

#TimeUsernameProblemLanguageResultExecution timeMemory
736157marvinthangIndex (COCI21_index)C++17
110 / 110
1460 ms143008 KiB
/****************************** * author : @marvinthang * * date : 10 / 02 / 2022 * ******************************/ #include <bits/stdc++.h> using namespace std; #define superspeed ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); } template <class U, class V> ostream & operator << (ostream& out, const pair<U, V> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template <class T> ostream & operator << (ostream &out, const vector<T> &vt) { out << '{'; for (size_t i = 0; i + 1 < vt.size(); i++) out << vt[i] << ", "; if (!vt.empty()) out << vt.back(); return out << '}'; } const int MOD = 1e9 + 7; const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0); const int dir[] = {0, 1, 0, -1, 0}; // {0, 1, 1, -1, -1, 1, 0, -1, 0}; const int MAX = 2e5 + 5; struct Node { Node *l, *r; int val; Node(Node *_l = nullptr, Node *_r = nullptr, int v = 0) { l = _l; r = _r; val = v; } }; Node *versions[MAX]; const int oo = 1 << 18; void build(Node *cur, int l = 1, int r = oo - 1) { if (l == r) return; int mid = l + r >> 1; cur -> l = new Node(); build(cur -> l, l, mid); cur -> r = new Node(); build(cur -> r, mid + 1, r); } void upgrade(Node *prev, Node *cur, int pos, int val, int l = 1, int r = oo - 1) { if (l > pos || r < pos) return; if (l == r) { cur -> val = prev -> val + val; return; } int mid = l + r >> 1; if (pos <= mid) { cur -> r = prev -> r; cur -> l = new Node(); upgrade(prev -> l, cur -> l, pos, val, l, mid); } else { cur -> l = prev -> l; cur -> r = new Node(); upgrade(prev -> r, cur -> r, pos, val, mid + 1, r); } cur -> val = cur -> l -> val + cur -> r -> val; } int get(Node *prev, Node *cur, int u, int l = 1, int r = oo - 1) { if (l > oo || r < u) return 0; if (u <= l) return cur -> val - prev -> val; int mid = l + r >> 1; return get(prev -> l, cur -> l, u, l, mid) + get(prev -> r, cur -> r, u, mid + 1, r); } int N, Q, A[MAX]; int main(void) { superspeed; file("coci2021_r6_index"); scanf("%d%d", &N, &Q); for (int i = 1; i <= N; ++i) scanf("%d", A + i); versions[0] = new Node(); build(versions[0]); for (int i = 1; i <= N; ++i) { versions[i] = new Node(); upgrade(versions[i - 1], versions[i], A[i], 1); } while (Q--) { int l, r; scanf("%d%d", &l, &r); int low = 1, high = oo; while (low <= high) { int mid = low + high >> 1; if (get(versions[l - 1], versions[r], mid) >= mid) low = mid + 1; else high = mid - 1; } printf("%d\n", high); } return 0; }

Compilation message (stderr)

index.cpp: In function 'void build(Node*, int, int)':
index.cpp:37:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |  int mid = l + r >> 1;
      |            ~~^~~
index.cpp: In function 'void upgrade(Node*, Node*, int, int, int, int)':
index.cpp:50:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |  int mid = l + r >> 1;
      |            ~~^~~
index.cpp: In function 'int get(Node*, Node*, int, int, int)':
index.cpp:66:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |  int mid = l + r >> 1;
      |            ~~^~~
index.cpp: In function 'int main()':
index.cpp:87:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |       int mid = low + high >> 1;
      |                 ~~~~^~~~~~
index.cpp:11:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:74:5: note: in expansion of macro 'file'
   74 |     file("coci2021_r6_index");
      |     ^~~~
index.cpp:11:95: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                                                       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:74:5: note: in expansion of macro 'file'
   74 |     file("coci2021_r6_index");
      |     ^~~~
index.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     scanf("%d%d", &N, &Q);
      |     ~~~~~^~~~~~~~~~~~~~~~
index.cpp:76:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     for (int i = 1; i <= N; ++i) scanf("%d", A + i);
      |                                  ~~~~~^~~~~~~~~~~~~
index.cpp:84:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |      int l, r; scanf("%d%d", &l, &r);
      |                ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...