Submission #1026922

# Submission time Handle Problem Language Result Execution time Memory
1026922 2024-07-18T13:56:14 Z TraianDanciu Horses (IOI15_horses) C++17
100 / 100
792 ms 83516 KB
#include "horses.h"
#include <math.h>

#define MAXN 500000
#define MOD 1000000007

void euclid(long long a, long long b, long long &x, long long &y) {
  long long x0, y0;

  if(b == 0) {
    x = 1;
    y = 0;
  } else {
    euclid(b, a % b, x0, y0);
    x = y0;
    y = x0 - a / b * y0;
  }
}

struct Numar {
  double ln;
  int val;

  Numar(int _val) {
    val = _val;
    ln = log2(val);
  }

  Numar() {}

  inline Numar operator *(Numar oth) {
    Numar ret;
    ret.val = 1LL * val * oth.val % MOD;
    ret.ln = ln + oth.ln;
    return ret;
  }

  inline Numar operator /(Numar oth) {
    Numar ret;
    long long x, y;
    euclid(oth.val, MOD, x, y);
    x %= MOD;
    if(x < 0) {
      x += MOD;
    }
    ret.val = 1LL * val * x % MOD;
    ret.ln = ln - oth.ln;
    return ret;
  }

  inline bool operator <(Numar oth) {
    return ln < oth.ln;
  }
};

static inline Numar max(Numar a, Numar b) {
  return a < b ? b : a;
}

Numar aint[4 * MAXN], lazy[4 * MAXN], qval;
int n, x[MAXN], y[MAXN], qleft, qright;

void propagate(int node, int left, int right) {
  aint[node] = aint[node] * lazy[node];
  if(left < right) {
    lazy[2 * node + 1] = lazy[2 * node + 1] * lazy[node];
    lazy[2 * node + 2] = lazy[2 * node + 2] * lazy[node];
  }
  lazy[node] = Numar(1);
}

void _update(int node, int left, int right) {
  int middle;

  propagate(node, left, right);
  if(qleft <= left && right <= qright) {
    lazy[node] = lazy[node] * qval;
    propagate(node, left, right);
  } else {
    middle = (left + right) / 2;

    propagate(2 * node + 1, left, middle);
    propagate(2 * node + 2, middle + 1, right);

    if(qleft <= middle) {
      _update(2 * node + 1, left, middle);
    }
    if(middle < qright) {
      _update(2 * node + 2, middle + 1, right);
    }

    aint[node] = max(aint[2 * node + 1], aint[2 * node + 2]);
  }
}
void update(int _qleft, int _qright, Numar _qval) {
  qleft = _qleft;
  qright = _qright;
  qval = _qval;
  _update(0, 0, n - 1);
}

Numar _query(int node, int left, int right) {
  int middle;
  Numar ans;

  propagate(node, left, right);
  if(qleft <= left && right <= qright) {
    return aint[node];
  }

  middle = (left + right) / 2;
  if(qleft <= middle && middle < qright) {
    ans = max(_query(2 * node + 1, left, middle),
              _query(2 * node + 2, middle + 1, right));
  } else if(qleft <= middle) {
    ans = _query(2 * node + 1, left, middle);
  } else {
    ans = _query(2 * node + 2, middle + 1, right);
  }

  return ans;
}
Numar query(int _qleft, int _qright) {
  qleft = _qleft;
  qright = _qright;
  return _query(0, 0, n - 1);
}

int init(int _n, int _x[], int _y[]) {
  int i;

  n = _n;
  for(i = 0; i < 4 * n; i++) {
    aint[i] = lazy[i] = Numar(1);
  }

  for(i = 0; i < n; i++) {
    x[i] = _x[i];
    update(i, n - 1, Numar(x[i]));
    y[i] = _y[i];
    update(i, i, Numar(y[i]));
  }

  return query(0, n - 1).val;
}

int updateX(int pos, int val) {
  update(pos, n - 1, Numar(val) / Numar(x[pos]));
  x[pos] = val;
  return query(0, n - 1).val;
}

int updateY(int pos, int val) {
  update(pos, pos, Numar(val) / Numar(y[pos]));
  y[pos] = val;
  return query(0, n - 1).val;
}

Compilation message

horses.cpp: In member function 'Numar Numar::operator*(Numar)':
horses.cpp:33:35: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   33 |     ret.val = 1LL * val * oth.val % MOD;
      |                                   ^
horses.cpp: In member function 'Numar Numar::operator/(Numar)':
horses.cpp:46:29: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   46 |     ret.val = 1LL * val * x % MOD;
      |                             ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6576 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6580 KB Output is correct
16 Correct 1 ms 6488 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 2 ms 6488 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 2 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6584 KB Output is correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 2 ms 6612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 712 ms 74956 KB Output is correct
2 Correct 792 ms 81696 KB Output is correct
3 Correct 730 ms 74812 KB Output is correct
4 Correct 764 ms 77844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6576 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6576 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 2 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 2 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6604 KB Output is correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 651 ms 74280 KB Output is correct
34 Correct 644 ms 74244 KB Output is correct
35 Correct 623 ms 79188 KB Output is correct
36 Correct 668 ms 79212 KB Output is correct
37 Correct 615 ms 72980 KB Output is correct
38 Correct 632 ms 73468 KB Output is correct
39 Correct 589 ms 72732 KB Output is correct
40 Correct 596 ms 75824 KB Output is correct
41 Correct 541 ms 72528 KB Output is correct
42 Correct 582 ms 72948 KB Output is correct
43 Correct 576 ms 75824 KB Output is correct
44 Correct 584 ms 77216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6584 KB Output is correct
13 Correct 1 ms 6572 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6572 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 0 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 0 ms 6492 KB Output is correct
23 Correct 2 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 2 ms 6492 KB Output is correct
27 Correct 2 ms 6616 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 2 ms 6584 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Correct 2 ms 6488 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 677 ms 74824 KB Output is correct
34 Correct 703 ms 81772 KB Output is correct
35 Correct 677 ms 74924 KB Output is correct
36 Correct 748 ms 77880 KB Output is correct
37 Correct 621 ms 74212 KB Output is correct
38 Correct 599 ms 74064 KB Output is correct
39 Correct 606 ms 79188 KB Output is correct
40 Correct 620 ms 79164 KB Output is correct
41 Correct 583 ms 72784 KB Output is correct
42 Correct 605 ms 73300 KB Output is correct
43 Correct 650 ms 72788 KB Output is correct
44 Correct 600 ms 75600 KB Output is correct
45 Correct 607 ms 72536 KB Output is correct
46 Correct 583 ms 73040 KB Output is correct
47 Correct 610 ms 75864 KB Output is correct
48 Correct 666 ms 77140 KB Output is correct
49 Correct 727 ms 76884 KB Output is correct
50 Correct 717 ms 76884 KB Output is correct
51 Correct 725 ms 83516 KB Output is correct
52 Correct 754 ms 83028 KB Output is correct
53 Correct 681 ms 75096 KB Output is correct
54 Correct 671 ms 75712 KB Output is correct
55 Correct 653 ms 74008 KB Output is correct
56 Correct 681 ms 78676 KB Output is correct
57 Correct 683 ms 74596 KB Output is correct
58 Correct 693 ms 75064 KB Output is correct
59 Correct 641 ms 77140 KB Output is correct