# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
990027 |
2024-05-29T11:37:31 Z |
Has2008 |
Poklon (COCI17_poklon) |
C++17 |
|
1464 ms |
61576 KB |
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define mod 1000000007
const int maxn = 500005;
int n, q, N = 1;
int arr[maxn], tree[maxn * 4];
int query(int n, int ql, int qr, int l, int r)
{
if(l >= ql && r <= qr) return tree[n];
if(l > qr || r < ql) return 0;
int md = (l + r) / 2;
return query(n * 2, ql, qr, l, md) + query(n * 2 + 1, ql, qr, md + 1, r);
}
void update(int u, int v)
{
u += N;
tree[u] = v;
u /= 2;
while (u > 0)
{
tree[u] = tree[u * 2] + tree[u * 2 + 1];
u /= 2;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> q;
while (N < n) N *= 2;
for(int i = 0; i < n; i++) cin >> arr[i];
vector < pair < int, int >> v, o;
while (q--)
{
int l, r;
cin >> l >> r;
v.push_back({r, l});
o.push_back({l, r});
}
sort(v.begin(), v.end());
map < int, vector < int >> mp;
map < pair < int, int >, int > ans;
int indx = 0;
for(int i = 0; i < n; i++)
{
vector < int > vv = mp[arr[i]];
if(vv.size() == 0) mp[arr[i]].push_back(i);
else if(vv.size() == 1)
{
update(vv[0], 1);
mp[arr[i]].push_back(i);
}
else if(vv.size() == 2)
{
update(vv[0], -1);
update(vv[1], 1);
mp[arr[i]].push_back(i);
}
else
{
update(vv[0], 0);
update(vv[1], -1);
update(vv[2], 1);
mp[arr[i]].clear();
mp[arr[i]].push_back(vv[1]);
mp[arr[i]].push_back(vv[2]);
mp[arr[i]].push_back(i);
}
while (indx != q && v[indx].first == i + 1)
{
ans[{v[indx].second, v[indx].first}] = query(1, v[indx].second - 1, v[indx].first - 1, 0, N - 1);
indx++;
}
}
for(int i = 0; i < o.size(); i++) cout << ans[{o[i].first, o[i].second}] << endl;
}
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:96:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i = 0; i < o.size(); i++) cout << ans[{o[i].first, o[i].second}] << endl;
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
9 ms |
3160 KB |
Output is correct |
5 |
Correct |
211 ms |
14260 KB |
Output is correct |
6 |
Correct |
216 ms |
14240 KB |
Output is correct |
7 |
Correct |
508 ms |
27304 KB |
Output is correct |
8 |
Correct |
827 ms |
40592 KB |
Output is correct |
9 |
Correct |
1112 ms |
52152 KB |
Output is correct |
10 |
Correct |
1464 ms |
61576 KB |
Output is correct |