[CSP-S Simulation Test]: cart (tree Keduo Li)

Title Description

$ visit_world $ has a store, the store sold $ N $ a commodity, the $ i $ a price of $ a [i] $ we call a positive integer $ K $ is wonderful, if and only if we can at the store purchase of goods in a plurality, and that the price of the falling interval $ [K, 2K] $ in.
Q: How many wonderful number.


Input Format

A first line integer $ N $.
The next line $ N $ integers, an array describing $ a [] $.


Output Format

Output line An integer that represents the answer.


Sample

Sample input:

3
1 2 3

Sample output:

6


Data range and tips

Sample explained:

We can prove that $ 1 \ leqslant K \ leqslant 6 $ are wonderful, except that the number is not wonderful.

data range:

Subtask $ 1 $ ($ 30 $ points): $ N \ leqslant 100, a_i \ leqslant 100 $.
Subtasks $ 2 $ ($ 20 $ points): $ N \ leqslant 100000, a_i \ leqslant 20 $.
Subtask $ 3 $ ($ 20 $ points): $ N \ leqslant 3, a_i \ leqslant 10 ^ 9 $.
Subtask $ 4 $ ($ 30 $ points): $ N \ leqslant 10 ^ 5, a_i \ leqslant 10 ^ 9 $.


answer

Positive solution will not just learning Keduo Li tree today, then hit it (unfortunately when the exam and will not ......)

Keduo Li is basically a tree board problem, may wish to look at Keduo Li took it in terms of tree it ~

Keduo Li tree principle is very simple, is to constantly add fill interval, and then merge (the intersection of two intervals have merged) finished adding, with $ set $ maintain these intervals just fine.

In terms of a few details:

  $ \ Alpha. $ Add interval time, at the same time if you want to add more intervals (such as this question), you need to enumerate all of the $ set $ in the original range, generating a new interval and place these intervals $ vector $ , if directly into them will cause an infinite loop (of course, is wrong).

  $ \ Beta. Compare before the interval when a $ merger, will be more convenient to deal with (see details of the code).

  $ \ Gamma. After the merger Note $ $ $ you the position of the pointer, you can find a half ......

Time complexity: $ \ Theta (n \ log \ log n) $ (random data).

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int N;
int a[100001];
set<pair<int,long long>>s;
vector<pair<int,long long>>v;
long long ans;
void add(int x)
{
	for(auto it=s.begin();it!=s.end();it++)
		v.push_back(make_pair((*it).first+x,(*it).second+2*x));
	while(v.size())
	{
		s.insert(v.back());
		v.pop_back ();
	}
}
void split()
{
	for(auto it=s.begin();;)
	{
		auto ti=it;it++;
		if(it==s.end())break;
		if((*ti).second>=(*it).first)
		{
			pair<int,long long> flag=make_pair((*ti).first,(*it).second);
			s.erase(it);s.erase(ti);s.insert(flag);
			it=s.lower_bound(flag);
		}
	}
}
int main ()
{
	scanf("%d",&N);
	s.insert(make_pair(0,0));
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&a[i]);
		add(a[i]);
		split();
	}
	for(auto it=s.begin();it!=s.end();it++)
	{
		if(!(*it).first)continue;
		ans+=((*it).second+1)/2-((*it).first-1)/2;
	}
	printf("%lld",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11689836.html