• + 0 comments

    C# solution based on editorial (witch does not work out of the box)

    public class BIT  
    {
    	private readonly long _max;
    	private readonly long[] _tree;
    
    	public BIT(long max)
    	{
    		_max = max;
    		_tree = new long[max];
    	}
    
    	public long get(int idx)
    	{
    		return query(idx) - query(idx - 1);
    	}
    
    	public void set(int idx, long val)
    	{
    		long curr = get(idx);
    		while (idx <= _max)
    		{
    			_tree[idx] += val-curr;
    			idx += (idx & -idx);
    		}
    	}
    
    	public long query(int idx)
    	{
    		long sum = 0;
    		while (idx > 0)
    		{
    			if (idx < _max)
    			{
    				sum += _tree[idx];
    			}
    
    			idx -= (idx & -idx);
    		}
    
    		return sum;
    	}
    }
    
    public static long solve(List<long> arr)
    {
    	var max = arr.Count;
    	long[] h = new long[max];
    	var S = new SortedSet<long>();
    	var m = new Dictionary<long, long>();
    	var a = new BIT(max);
    	var b = new BIT(max);
    	var c = new BIT(max);
    
    	foreach (var l in arr)
    	{
    		S.Add(l);
    	}
    
    	long cnt = 1;
    	foreach (var l in S)
    	{
    		m.Add(l, cnt++);
    	}
    
    	for (int i = 0; i < arr.Count; i++)
    	{
    		int indx = Convert.ToInt32(m[arr[i]]);
    		h[i] = m[arr[i]];
    		a.set(indx, 1);
    		b.set(indx, a.query(indx - 1));
    		c.set(indx, b.query(indx - 1));
    	}
    
    	return c.query(max);
    
    }