Submission #403220

# Submission time Handle Problem Language Result Execution time Memory
403220 2021-05-12T23:38:06 Z MohamedAhmed04 Index (COCI21_index) C++14
110 / 110
846 ms 19440 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 2e5 + 10 ;

int arr[MAX] , L[MAX] , R[MAX] ;
int n , q ;
int cnt[MAX] , ans[MAX] ;

vector< pair<int , int> >vp(MAX) ;
vector<int>queries[MAX] ;

int bit[MAX] ;

void add(int i , int val)
{
	for(; i < MAX ; i += (i & (-i)))
		bit[i] += val ;
}

int get(int i)
{
	int sum = 0 ;
	for(; i > 0 ; i -= (i & (-i)))
		sum += bit[i] ;
	return sum ;
}

int query(int l , int r)
{
	return (get(r) - get(l - 1)) ; 
}

void init()
{
	for(int i = 1 ; i <= n ; ++i)
		queries[i].clear() ;
	memset(bit , 0 , sizeof(bit)) ;
	memset(cnt , 0 , sizeof(cnt)) ; 
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>q ;
	for(int i = 1 ; i <= n ; ++i)
		cin>>arr[i] ;
	for(int i = 1 ; i <= q ; ++i)
	{
		cin>>L[i]>>R[i] ;
		vp[i] = {1 , n} ;
	}
	bool ok = false ;
	while(!ok)
	{
		ok = true ;
		init() ;
		for(int i = 1 ; i <= q ; ++i)
		{
			if(vp[i].first > vp[i].second)
				continue ;
			ok = false ;
			queries[L[i]-1].push_back(-i) ;
			queries[R[i]].push_back(i) ;
		}
		for(int i = 1 ; i <= n ; ++i)
		{
			add(arr[i] , 1) ;
			for(auto &j : queries[i])
			{
				int sgn = 1 ;
				if(j < 0)
					j *= -1 , sgn = -1 ;
				cnt[j] += sgn * query((vp[j].first + vp[j].second) >> 1 , MAX-1) ;
			}
		}
		for(int i = 1 ; i <= q ; ++i)
		{
			if(vp[i].first > vp[i].second)
				continue ;
			int x = (vp[i].first + vp[i].second) >> 1 ;
			if(cnt[i] >= x)
				ans[i] = x , vp[i].first = x+1 ;
			else
				vp[i].second = x-1 ;
		}
	}
	for(int i = 1 ; i <= q ; ++i)
		cout<<ans[i]<<"\n" ;
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8140 KB Output is correct
2 Correct 8 ms 8216 KB Output is correct
3 Correct 7 ms 8204 KB Output is correct
4 Correct 8 ms 8140 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 8 ms 8140 KB Output is correct
7 Correct 8 ms 8140 KB Output is correct
8 Correct 7 ms 8152 KB Output is correct
9 Correct 7 ms 8144 KB Output is correct
10 Correct 7 ms 8140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8140 KB Output is correct
2 Correct 8 ms 8216 KB Output is correct
3 Correct 7 ms 8204 KB Output is correct
4 Correct 8 ms 8140 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 8 ms 8140 KB Output is correct
7 Correct 8 ms 8140 KB Output is correct
8 Correct 7 ms 8152 KB Output is correct
9 Correct 7 ms 8144 KB Output is correct
10 Correct 7 ms 8140 KB Output is correct
11 Correct 155 ms 10860 KB Output is correct
12 Correct 137 ms 10984 KB Output is correct
13 Correct 134 ms 10820 KB Output is correct
14 Correct 135 ms 10832 KB Output is correct
15 Correct 135 ms 10884 KB Output is correct
16 Correct 132 ms 10848 KB Output is correct
17 Correct 134 ms 10848 KB Output is correct
18 Correct 148 ms 10932 KB Output is correct
19 Correct 134 ms 10848 KB Output is correct
20 Correct 132 ms 10800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8140 KB Output is correct
2 Correct 8 ms 8216 KB Output is correct
3 Correct 7 ms 8204 KB Output is correct
4 Correct 8 ms 8140 KB Output is correct
5 Correct 7 ms 8148 KB Output is correct
6 Correct 8 ms 8140 KB Output is correct
7 Correct 8 ms 8140 KB Output is correct
8 Correct 7 ms 8152 KB Output is correct
9 Correct 7 ms 8144 KB Output is correct
10 Correct 7 ms 8140 KB Output is correct
11 Correct 155 ms 10860 KB Output is correct
12 Correct 137 ms 10984 KB Output is correct
13 Correct 134 ms 10820 KB Output is correct
14 Correct 135 ms 10832 KB Output is correct
15 Correct 135 ms 10884 KB Output is correct
16 Correct 132 ms 10848 KB Output is correct
17 Correct 134 ms 10848 KB Output is correct
18 Correct 148 ms 10932 KB Output is correct
19 Correct 134 ms 10848 KB Output is correct
20 Correct 132 ms 10800 KB Output is correct
21 Correct 810 ms 19384 KB Output is correct
22 Correct 819 ms 19396 KB Output is correct
23 Correct 802 ms 19388 KB Output is correct
24 Correct 846 ms 19440 KB Output is correct
25 Correct 813 ms 19372 KB Output is correct
26 Correct 799 ms 19396 KB Output is correct
27 Correct 827 ms 19388 KB Output is correct
28 Correct 771 ms 19376 KB Output is correct
29 Correct 805 ms 19380 KB Output is correct
30 Correct 740 ms 19396 KB Output is correct