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;
const int N = 500005;
int M;
struct ban
{
int x, t;
};
bool operator<(const ban& a, const ban& b)
{
return a.x < b.x;
}
int n, k;
ban a[N];
vector<int> v[N];
int uv[N];
vector<int> uu[N];
int t[N * 4];
void bil(int tl, int tr, int pos)
{
t[pos] = 1;
if (tl == tr)
return;
int m = (tl + tr) / 2;
bil(tl, m, pos * 2);
bil(m + 1, tr, pos * 2 + 1);
}
void ubd(int tl, int tr, int x, int pos)
{
if (tl == tr)
{
++t[pos];
t[pos] %= M;
return;
}
int m = (tl + tr) / 2;
if (x <= m)
ubd(tl, m, x, pos * 2);
else
ubd(m + 1, tr, x, pos * 2 + 1);
t[pos] = (t[pos * 2] * t[pos * 2 + 1]) % M;
}
int qry(int tl, int tr, int l, int r, int pos)
{
if (l > r)
return 1;
if (tl == l && tr == r)
return t[pos];
int m = (tl + tr) / 2;
return (qry(tl, m, l, min(m, r), pos * 2) *
qry(m + 1, tr, max(m + 1, l), r, pos * 2 + 1)) % M;
}
int ver[N];
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
#endif // SOMETHING
scanf("%d%d%d", &n, &k, &M);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &a[i].x, &a[i].t);
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i)
{
uv[i] = v[a[i].t].size();
v[a[i].t].push_back(i);
}
uv[0] = -1;
for (int i = 1; i <= k; ++i)
{
int l = 1, r = n;
int ans = 0;
while (l <= r)
{
int m = (l + r) / 2;
if (a[m].x * 2 <= a[v[i].back()].x)
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
uu[ans].push_back(i);
}
int yans = 0;
bil(1, n, 1);
for (int i = 0; i < n; ++i)
{
if (i)
{
ubd(1, n, v[a[i].t].back(), 1);
ver[a[i].t] = i;
}
for (int j = 0; j < uu[i].size(); ++j)
{
int x = uu[i][j];
int l = 1, r = n;
int ans = n + 1;
while (l <= r)
{
int m = (l + r) / 2;
if (a[m].x >= a[v[x][uv[ver[x]] + 1]].x * 2)
{
ans = m;
r = m - 1;
}
else
l = m + 1;
}
l = 1, r = ans - 1;
int ans1 = ans;
while (l <= r)
{
int m = (l + r) / 2;
if (a[m].x >= a[v[x].back()].x)
{
ans1 = m;
r = m - 1;
}
else
l = m + 1;
}
int baz = (qry(1, n, 1, v[x].back() - 1, 1) * qry(1, n, v[x].back() + 1, ans - 1, 1)) % M;
yans = (yans + baz) % M;
baz = (qry(1, n, 1, v[x].back() - 1, 1) * qry(1, n, v[x].back() + 1, ans1 - 1, 1)) % M;
yans = (yans + baz * ((uv[ver[x]] + 1) % M)) % M;
}
}
printf("%d\n", yans);
return 0;
}
Compilation message (stderr)
fish.cpp: In function 'int main()':
fish.cpp:102:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < uu[i].size(); ++j)
~~^~~~~~~~~~~~~~
fish.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &k, &M);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
fish.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i].x, &a[i].t);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | 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... |
# | 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... |
# | 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... |