rep(v, n)
	{
		int d = v + 1 - s, u = 0;
		if(d > 0)
		{
			int i = cnt.find(d);
			u = tot.sum(i) + (d - cnt.sum(i)) * (i + 1);
		}
		ans = min(ans, t + u);
		for(auto x : ord[v]) s--, t -= x, cnt.add(x, 1), tot.add(x, x);
	}
    void divide(int L, int R, int p) {
        if (R-L == 1) {
            ftree.add(1, n-index[L]-p);
        } else {
            int i = L;
            while(i < R) {
                while( index[i]+p >= n ) i++;
                int j = i + 1;
                while(j < R && index[j]+p < n && s[index[i]+p] == s[index[j]+p]) j++;
                ftree.add(j-i,j-i);
//                cout << i << ' ' << j << " pos: "  << p+1 << endl;
                divide(i, j, p+1);
                i = j;
            }
        }

    }
Exemple #3
0
int main (){
	ios::sync_with_stdio(false);
	cin>>n;
	int total=1;
	FenwickTree tree (n+5);
	long long count = 0;
	while(n--) {
		int t;
		cin>>t;
		if (t == 1) {
			int x,a;
			cin>>a>>x;
			tree.add(0,x);
			tree.add(a,-x);
			count += a*x;
		}
		else if (t == 2) {
Exemple #4
0
//-----------------------------------------------------------------------------
int calculateTakenPlace(FenwickTree& array_of_sums, int arrive_at_place) {
  if (array_of_sums.at(arrive_at_place) == 0) {
    array_of_sums.add(arrive_at_place, 1);
    return arrive_at_place;
  }
  int occupied_place = -2;
  if (arrive_at_place + 1 < array_of_sums.size()) {
    occupied_place = binarySearchForFreePlace(
            array_of_sums,
            arrive_at_place + 1,
            array_of_sums.size());
  }
  if (occupied_place == -2) {
    occupied_place = binarySearchForFreePlace(
        array_of_sums,
        0,
        arrive_at_place);
  }
  array_of_sums.add(occupied_place, 1);
  return occupied_place;
}
 void rangeAfterAdd(int pos, i64 v){
   delta.add(pos, v);
   delta_i.add(pos, v * (pos - 1));
 }