This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include "teams.h"
template <typename T>
struct FenwickTree2dOff
{
vector<vector<T>> t, c;
bool initialized = 0;
FenwickTree2dOff() {}
FenwickTree2dOff(size_t n)
{
t = vector<vector<T>>(n);
c = vector<vector<T>>(n);
}
void initialize()
{
for (size_t i = 0; i < t.size(); i++)
{
sort(c[i].begin(), c[i].end());
c[i].resize(unique(c[i].begin(), c[i].end()) - c[i].begin());
t[i] = vector<T>(c[i].size(), 0);
}
initialized = 1;
}
void increment(size_t i, size_t j, T x)
{
i++, j++;
while (i <= t.size())
{
if (!initialized)
c[i - 1].push_back(j);
else
{
size_t u = upper_bound(c[i - 1].begin(), c[i - 1].end(), j) - c[i - 1].begin();
while (u <= t[i - 1].size())
{
t[i - 1][u - 1] += x;
u += u & -u;
}
}
i += i & -i;
}
}
T prefix_sum(size_t i, size_t j)
{
i++, j++;
T x = 0;
while (i)
{
size_t u = upper_bound(c[i - 1].begin(), c[i - 1].end(), j) - c[i - 1].begin();
while (u)
{
x += t[i - 1][u - 1];
u -= u & -u;
}
i -= i & -i;
}
return x;
}
T sum(size_t i1, size_t j1, size_t i2, size_t j2)
{
T x = prefix_sum(i2, j2);
if (i1)
x -= prefix_sum(i1 - 1, j2);
if (j1)
x -= prefix_sum(i2, j1 - 1);
if (i1 && j1)
x += prefix_sum(i1 - 1, j1 - 1);
return x;
}
};
FenwickTree2dOff<unsigned> tree;
int n;
void init(int N, int A[], int B[])
{
n = N;
tree = FenwickTree2dOff<unsigned>(N + 2);
for (size_t i = 0; i < N; i++)
tree.increment(A[i], B[i], 1);
tree.initialize();
}
int can(int M, int K[])
{
sort(K, K + M);
int y_lim = 1, residual = 0;
for (size_t i = 0; i < M; i++)
{
int a = max(y_lim, K[i]), b = n + 1, last_num = 0;
while (a < b) // Binary search for the highest end index needed.
{
int const y = (a + b) / 2;
int const x = tree.sum(1, y, K[i], n);
if (x >= K[i] - residual)
b = y, last_num = x;
else
a = y + 1;
}
if (a == n + 1)
return 0;
residual = last_num - (K[i] - residual);
y_lim = a + 1;
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:90:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
90 | for (size_t i = 0; i < N; i++)
| ~~^~~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:100:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
100 | for (size_t i = 0; i < M; i++)
| ~~^~~
teams.cpp: In instantiation of 'FenwickTree2dOff<T>::FenwickTree2dOff(size_t) [with T = unsigned int; size_t = long unsigned int]':
teams.cpp:89:44: required from here
teams.cpp:13:29: warning: declaration of 'n' shadows a global declaration [-Wshadow]
13 | FenwickTree2dOff(size_t n)
| ~~~~~~~^
teams.cpp:84:5: note: shadowed declaration is here
84 | int n;
| ^
teams.cpp:13:29: warning: declaration of 'n' shadows a global declaration [-Wshadow]
13 | FenwickTree2dOff(size_t n)
| ~~~~~~~^
teams.cpp:84:5: note: shadowed declaration is here
84 | int n;
| ^
teams.cpp: In instantiation of 'void FenwickTree2dOff<T>::increment(size_t, size_t, T) [with T = unsigned int; size_t = long unsigned int]':
teams.cpp:91:37: required from here
teams.cpp:37:36: warning: conversion from 'size_t' {aka 'long unsigned int'} to 'std::vector<unsigned int>::value_type' {aka 'unsigned int'} may change value [-Wconversion]
37 | c[i - 1].push_back(j);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |