#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<pair<int,int>,int>
using namespace std;
const int N=2e5+5,mod=1e9+7;
int tree[N],n,q,x,ans[N];
vector<int> c[N];
vector<pair<int,int> > st[N];
void upd(int ind,int val) {
for(ind;ind<=n;ind+=(-ind)&ind) {
tree[ind] += val;
}
}
int get(int ind) {
int ans = 0;
for(ind;ind>=1;ind-=(-ind)&ind) ans += tree[ind];
return ans;
}
void solve(int l,int r,vector<pii> v) {
if(l > r) return;
if(l==r) {
for(int i=0;i<c[l].size();i++) {
upd(c[l][i],1);
}
for(int i=0;i<v.size();i++) {
int L = v[i].f.f, r = v[i].f.s;
if(get(r) - get(L-1) >=l ) ans[v[i].s] = l;
}
for(int i=0;i<c[l].size();i++) {
upd(c[l][i],-1);
}
return ;
}
int mid = (l+r)/2;
for(int i=mid; i<=r; i++) {
for(int j=0;j<c[i].size();j++) {
upd(c[i][j],1);
}
}
vector<pii> x,y;
for(int i=0;i<v.size();i++) {
int l = v[i].f.f, r = v[i].f.s;
if(get(r) - get(l-1) >=mid ) {
ans[v[i].s] = mid;
x.push_back(v[i]);
}
else y.push_back(v[i]);
}
solve(l,mid-1,y);
for(int i=mid; i<=r; i++) {
for(int j=0;j<c[i].size();j++) {
upd(c[i][j],-1);
}
}
solve(mid+1,r,x);
}
main(){
cin>>n>>q;
x = 0;
for(int i=1;i<=n;i++){
int a;
cin>>a;
c[a].push_back(i);
x = max(x,a);
}
vector<pii> all;
for(int i=1;i<=q;i++) {
int l,r;
cin >> l >> r;
all.push_back({{l,r},i});
st[l].push_back({r,i});
}
solve(1,x,all);
for(int i=1;i<=q;i++) cout<<ans[i]<<"\n";
}
Compilation message
index.cpp: In function 'void upd(int, int)':
index.cpp:11:6: warning: statement has no effect [-Wunused-value]
11 | for(ind;ind<=n;ind+=(-ind)&ind) {
| ^~~
index.cpp: In function 'int get(int)':
index.cpp:17:6: warning: statement has no effect [-Wunused-value]
17 | for(ind;ind>=1;ind-=(-ind)&ind) ans += tree[ind];
| ^~~
index.cpp: In function 'void solve(int, int, std::vector<std::pair<std::pair<int, int>, int> >)':
index.cpp:23:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0;i<c[l].size();i++) {
| ~^~~~~~~~~~~~
index.cpp:26:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
index.cpp:30:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0;i<c[l].size();i++) {
| ~^~~~~~~~~~~~
index.cpp:37:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j=0;j<c[i].size();j++) {
| ~^~~~~~~~~~~~
index.cpp:42:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
index.cpp:52:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int j=0;j<c[i].size();j++) {
| ~^~~~~~~~~~~~
index.cpp: At global scope:
index.cpp:58:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9932 KB |
Output is correct |
2 |
Correct |
8 ms |
9844 KB |
Output is correct |
3 |
Correct |
8 ms |
9804 KB |
Output is correct |
4 |
Correct |
9 ms |
9844 KB |
Output is correct |
5 |
Correct |
7 ms |
9932 KB |
Output is correct |
6 |
Correct |
8 ms |
9804 KB |
Output is correct |
7 |
Correct |
9 ms |
9804 KB |
Output is correct |
8 |
Correct |
7 ms |
9860 KB |
Output is correct |
9 |
Correct |
8 ms |
9828 KB |
Output is correct |
10 |
Correct |
8 ms |
9828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9932 KB |
Output is correct |
2 |
Correct |
8 ms |
9844 KB |
Output is correct |
3 |
Correct |
8 ms |
9804 KB |
Output is correct |
4 |
Correct |
9 ms |
9844 KB |
Output is correct |
5 |
Correct |
7 ms |
9932 KB |
Output is correct |
6 |
Correct |
8 ms |
9804 KB |
Output is correct |
7 |
Correct |
9 ms |
9804 KB |
Output is correct |
8 |
Correct |
7 ms |
9860 KB |
Output is correct |
9 |
Correct |
8 ms |
9828 KB |
Output is correct |
10 |
Correct |
8 ms |
9828 KB |
Output is correct |
11 |
Correct |
164 ms |
20540 KB |
Output is correct |
12 |
Correct |
138 ms |
20620 KB |
Output is correct |
13 |
Correct |
142 ms |
20524 KB |
Output is correct |
14 |
Correct |
146 ms |
20764 KB |
Output is correct |
15 |
Correct |
132 ms |
20516 KB |
Output is correct |
16 |
Correct |
140 ms |
20428 KB |
Output is correct |
17 |
Correct |
136 ms |
20708 KB |
Output is correct |
18 |
Correct |
133 ms |
20724 KB |
Output is correct |
19 |
Correct |
134 ms |
20412 KB |
Output is correct |
20 |
Correct |
151 ms |
20484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9932 KB |
Output is correct |
2 |
Correct |
8 ms |
9844 KB |
Output is correct |
3 |
Correct |
8 ms |
9804 KB |
Output is correct |
4 |
Correct |
9 ms |
9844 KB |
Output is correct |
5 |
Correct |
7 ms |
9932 KB |
Output is correct |
6 |
Correct |
8 ms |
9804 KB |
Output is correct |
7 |
Correct |
9 ms |
9804 KB |
Output is correct |
8 |
Correct |
7 ms |
9860 KB |
Output is correct |
9 |
Correct |
8 ms |
9828 KB |
Output is correct |
10 |
Correct |
8 ms |
9828 KB |
Output is correct |
11 |
Correct |
164 ms |
20540 KB |
Output is correct |
12 |
Correct |
138 ms |
20620 KB |
Output is correct |
13 |
Correct |
142 ms |
20524 KB |
Output is correct |
14 |
Correct |
146 ms |
20764 KB |
Output is correct |
15 |
Correct |
132 ms |
20516 KB |
Output is correct |
16 |
Correct |
140 ms |
20428 KB |
Output is correct |
17 |
Correct |
136 ms |
20708 KB |
Output is correct |
18 |
Correct |
133 ms |
20724 KB |
Output is correct |
19 |
Correct |
134 ms |
20412 KB |
Output is correct |
20 |
Correct |
151 ms |
20484 KB |
Output is correct |
21 |
Correct |
677 ms |
53516 KB |
Output is correct |
22 |
Correct |
621 ms |
53408 KB |
Output is correct |
23 |
Correct |
606 ms |
53024 KB |
Output is correct |
24 |
Correct |
612 ms |
52836 KB |
Output is correct |
25 |
Correct |
626 ms |
53256 KB |
Output is correct |
26 |
Correct |
651 ms |
52776 KB |
Output is correct |
27 |
Correct |
640 ms |
52548 KB |
Output is correct |
28 |
Correct |
639 ms |
53488 KB |
Output is correct |
29 |
Correct |
627 ms |
52584 KB |
Output is correct |
30 |
Correct |
642 ms |
53352 KB |
Output is correct |