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;
typedef long long ll;
const int maxn = 300000 + 10;
const int maxN = (1 << 21) + 10;
int fen[maxN];
int get(int x){
int ret = 0;
for (x = max(0, x + 1); x; x -= x & -x)
ret += fen[x];
return ret;
}
void add(int x){
for (x ++; x < maxN; x += x & -x)
fen[x] ++;
}
vector<pair<ll,ll> > get(vector<pair<ll,ll> > &a, bool wh){
vector<pair<ll,ll>> c;
int n = a.size();
for (int mask = 1; mask < (1 << n); mask++){
ll last = -1, first = -1;
bool flag = 1;
ll sum = 0;
for (int i = 0; i < n; i++){
if (!(mask & (1 << i)))
continue;
if (last > a[i].first){
flag = 0;
break;
}
if (first == -1)
first = a[i].first;
last = a[i].first;
sum += a[i].second;
}
if (flag == 0)
continue;
if (wh == 0)
c.push_back({last, sum});
else
c.push_back({first, sum});
}
if (wh == 0)
c.push_back({1, 0});
else
c.push_back({1000*1000*1000, 0});
return c;
}
int main(){
ios_base::sync_with_stdio(false);
int n;
ll k;
cin >> n >> k;
vector<pair<ll,ll> > a, b;
for (int i = 0; i < n; i++){
ll h, g;
cin >> h >> g;
if (i < n / 2)
a.push_back({h, g});
else
b.push_back({h, g});
}
vector<pair<ll,ll>> c = get(a, 0);
vector<pair<ll,ll>> d = get(b, 1);
sort(c.begin(), c.end());
sort(d.begin(), d.end());
vector<ll> cmp;
for (auto it : c)
cmp.push_back(it.second);
for (auto it : d)
cmp.push_back(k - it.second - 1);
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
for (auto& it : c)
it.second = lower_bound(cmp.begin(), cmp.end(), it.second) - cmp.begin() + 1;
for (auto& it : d)
it.second = lower_bound(cmp.begin(), cmp.end(), k - it.second - 1) - cmp.begin() + 1;
int ptr = 0;
n = c.size();
int m = d.size();
ll answer = 0;
for (int i = 0; i < d.size(); i++){
while (ptr < n and c[ptr].first <= d[i].first){
add(c[ptr ++].second);
}
answer += ptr - get(d[i].second);
}
cout << answer << endl;
}
Compilation message (stderr)
san.cpp: In function 'int main()':
san.cpp:89:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < d.size(); i++){
~~^~~~~~~~~~
san.cpp:86:6: warning: unused variable 'm' [-Wunused-variable]
int m = d.size();
^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |