Submission #351471

# Submission time Handle Problem Language Result Execution time Memory
351471 2021-01-20T04:50:44 Z spatarel Horses (IOI15_horses) C++17
100 / 100
478 ms 47340 KB
#include "horses.h"
#include <stdio.h>
#include <algorithm>
#include <set>

const int MOD = 1000000007;
const int MAX_N = 500000;

int N;
int X[MAX_N];

long long allProdX = 1;

long long power(long long base, int exp) {
  if (exp == 1) {
    return base;
  } else if (exp % 2 == 0) {
    return power(base * base % MOD, exp / 2);
  } else {
    return power(base, exp - 1) * base % MOD;
  }
}

int inv(int value) {
  return (int)power(value, MOD - 2);
}

struct Index {
  int index;
  int maxRangeY;
};

bool operator < (const Index &a, const Index &b) {
  return a.index < b.index;
}

int segTreeN = 1 << 19;
int segTree[1 << 20];

void segTreeUpdate(int index, int newValue) {
  index += segTreeN;
  segTree[index] = newValue;
  while (index > 1) {
    index /= 2;
    segTree[index] = std::max(segTree[2 * index], segTree[2 * index + 1]);
  }
}

int segTreeRangeMax(int left, int right) {
  right++;
  left += segTreeN;
  right += segTreeN;
  int answer = 0;
  while (left < right) {
    if (left % 2 == 1) {
      answer = std::max(answer, segTree[left]);
      left++;
    }
    if (right % 2 == 1) {
      right--;
      answer = std::max(answer, segTree[right]);
    }
    left /= 2;
    right /= 2;
  }
  return answer;
}

std::set<Index> relevantXs;

struct Fraction {
  int x;
  int y;
};

bool operator <(const Fraction &a, const Fraction &b) {
  // return a.x / a.y < b.x / b.y;
  return (long long)a.x * b.y < (long long)b.x * a.y;
}

int query() {
  Fraction answer = Fraction{1, 1};
  long long productX = 1;
  auto it = relevantXs.rbegin();
  while (it != relevantXs.rend() && productX < MOD) {
    answer = std::max(answer, Fraction{it->maxRangeY, (int)productX});
    
    productX *= X[it->index];
    it++;
  }
  //printf("%lld %d %d\n", allProdX, answer.x, answer.y);
  return int(allProdX * answer.x % MOD * inv(answer.y) % MOD);
}

int init(int n, int x[], int y[]) {
	int maxY = 0;
  N = n;
  for (int i = N - 1; i >= 0; i--) {
    X[i] = x[i];
    allProdX = allProdX * X[i] % MOD;
    segTreeUpdate(i, y[i]);
    maxY = std::max(maxY, y[i]);
    if (X[i] > 1 || i == 0) {
      relevantXs.insert(Index{i, maxY});
      maxY = 0;
    }
  }
  return query();
}

void dump() {
  for (Index index : relevantXs) {
    printf("(%d, %d) ", index.index, index.maxRangeY);
  }
}

int updateX(int pos, int val) {
	//printf("updateX(%d, %d)\n", pos, val);
  allProdX = allProdX * inv(X[pos]) % MOD;
  if (pos > 0 && X[pos] > 1) {
    // removeX(pos);
    //printf("removeX(%d) ", pos);
    //fflush(stdout);
    auto it = relevantXs.find(Index{pos, 0});
    //printf("done (%d, %d)!\n", it->index, it->maxRangeY);
    //fflush(stdout);
    int maxY2 = it->maxRangeY;
    auto prevIt = prev(it);
    //printf("done (%d, %d)!\n", it->index, it->maxRangeY);
    //fflush(stdout);
    relevantXs.erase(it);
    //printf("done!\n");
    //fflush(stdout);
    int maxY1 = prevIt->maxRangeY;
    int previousIndex = prevIt->index;
    relevantXs.erase(prevIt);
    //printf("done!\n");
    //fflush(stdout);
    relevantXs.insert(Index{previousIndex, std::max(maxY1, maxY2)});
    //printf("done!\n");
    //fflush(stdout);
  }
  X[pos] = val;
  allProdX = allProdX * X[pos] % MOD;
  if (pos > 0 && X[pos] > 1) {
    // insertX(pos);
    //printf("insertX(%d) ", pos);
    //fflush(stdout);
    int rightIndex, leftIndex;
    auto it = relevantXs.lower_bound(Index{pos, 0});
    if (it == relevantXs.end()) {
      rightIndex = N - 1;
    } else {
      rightIndex = it->index - 1;
    }
    auto prevIt = prev(it);
    leftIndex = prevIt->index;
    relevantXs.erase(prevIt);
    relevantXs.insert(Index{leftIndex, 
        segTreeRangeMax(leftIndex, pos - 1)});
    relevantXs.insert(Index{pos, 
        segTreeRangeMax(pos, rightIndex)});
    //printf("done!\n");
    //fflush(stdout);
  }
  //dump();
  return query();
}

int updateY(int pos, int val) {
  //printf("updateY(%d, %d)\n", pos, val);
	segTreeUpdate(pos, val);
  long long productX = 1;
  int prevIndex = N;
  auto it = relevantXs.end();
  it--;
  while (productX < MOD) {
    if (it->index <= pos && pos < prevIndex) {
      // it->maxRangeY = segTreeRangeMax(it->index, prevIndex - 1);
      int index = it->index;
      relevantXs.erase(it);
      relevantXs.insert(Index{index,
          segTreeRangeMax(index, prevIndex - 1)});
      break;
    }
    productX *= X[it->index];
    prevIndex = it->index;
    it--;
  }
  //dump();
  return query();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 0 ms 364 KB Output is correct
22 Correct 0 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 269 ms 34668 KB Output is correct
2 Correct 382 ms 47340 KB Output is correct
3 Correct 346 ms 38636 KB Output is correct
4 Correct 457 ms 42220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 0 ms 364 KB Output is correct
22 Correct 0 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 2 ms 492 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 64 ms 14444 KB Output is correct
34 Correct 69 ms 14316 KB Output is correct
35 Correct 220 ms 44572 KB Output is correct
36 Correct 214 ms 44524 KB Output is correct
37 Correct 56 ms 12396 KB Output is correct
38 Correct 118 ms 25196 KB Output is correct
39 Correct 53 ms 12160 KB Output is correct
40 Correct 195 ms 39660 KB Output is correct
41 Correct 53 ms 12268 KB Output is correct
42 Correct 55 ms 12376 KB Output is correct
43 Correct 188 ms 40128 KB Output is correct
44 Correct 192 ms 40172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 0 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
28 Correct 2 ms 492 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 267 ms 38432 KB Output is correct
34 Correct 382 ms 47164 KB Output is correct
35 Correct 352 ms 38508 KB Output is correct
36 Correct 478 ms 42220 KB Output is correct
37 Correct 63 ms 14316 KB Output is correct
38 Correct 64 ms 14316 KB Output is correct
39 Correct 214 ms 44524 KB Output is correct
40 Correct 216 ms 44652 KB Output is correct
41 Correct 59 ms 12460 KB Output is correct
42 Correct 127 ms 25196 KB Output is correct
43 Correct 52 ms 12268 KB Output is correct
44 Correct 193 ms 39660 KB Output is correct
45 Correct 53 ms 12268 KB Output is correct
46 Correct 55 ms 12268 KB Output is correct
47 Correct 187 ms 40172 KB Output is correct
48 Correct 191 ms 40044 KB Output is correct
49 Correct 147 ms 17260 KB Output is correct
50 Correct 165 ms 17260 KB Output is correct
51 Correct 294 ms 46572 KB Output is correct
52 Correct 283 ms 46060 KB Output is correct
53 Correct 161 ms 15596 KB Output is correct
54 Correct 209 ms 29164 KB Output is correct
55 Correct 119 ms 13292 KB Output is correct
56 Correct 262 ms 41452 KB Output is correct
57 Correct 129 ms 13932 KB Output is correct
58 Correct 152 ms 14444 KB Output is correct
59 Correct 191 ms 40044 KB Output is correct