Submission #1039918

# Submission time Handle Problem Language Result Execution time Memory
1039918 2024-07-31T12:25:17 Z Zicrus Horses (IOI15_horses) C++17
100 / 100
87 ms 50004 KB
#include <bits/stdc++.h>
#include "horses.h"
using namespace std;

typedef long long ll;

ll n, pow2;
vector<ll> segMul, segMax;
vector<bool> segOF;
vector<ll> x, y;
vector<ll> lnk;
vector<ll> effY;

#define MOD (1000000007)

int getPrev2Id(int i) {
    // TODO (inc)
    // If no 2, return id 0
    // i can be negative!!!
    if (i < 0) return 0;
    i += pow2;
    while (segMul[i] == 1 && !segOF[i]) {
        if (!(i & 1)) {
            if ((i & (i-1)) == 0) return 0;
            i--;
        }
        i /= 2;
    }
    // now search down
    while (i < pow2) {
        if (segMul[2*i+1] == 1 && !segOF[2*i+1]) i = 2*i;
        else i = 2*i+1;
    }
    return i-pow2;
}

int getMax(ll low, ll high) {
    low += pow2; high += pow2;
    ll res = 0;
    while (low <= high) {
        if (low & 1) res = max(res, segMax[low++]);
        if (!(high & 1)) res = max(res, segMax[high--]);
        low /= 2; high /= 2;
    }
    return res;
}

void setX(int pos, int val) {
    if (val == x[pos]) return;
    int savedPos = pos;
    int old = x[pos];
    x[pos] = val;
    pos += pow2;
    segMul[pos] = val;
    pos /= 2;
    while (pos) {
        segMul[pos] = segMul[2*pos] * segMul[2*pos+1];
        if (segOF[2*pos] || segOF[2*pos+1] || segMul[pos] > MOD) {
            segMul[pos] %= MOD;
            segOF[pos] = true;
        }
        else {
            segOF[pos] = false;
        }
        pos /= 2;
    }
    pos = savedPos;

    if (val == 1) {
        ll prev2 = getPrev2Id(pos-1);
        lnk[prev2] = lnk[pos];
        effY[prev2] = getMax(prev2, lnk[prev2]-1);
        return;
    }
    if (old > 1) {
        effY[pos] = getMax(pos, lnk[pos]-1);
        return;
    }
    if (pos == 0) {
        effY[pos] = getMax(pos, lnk[pos]-1);
        return;
    }
    ll prev2 = getPrev2Id(pos-1);
    lnk[pos] = lnk[prev2];
    effY[pos] = getMax(pos, lnk[pos]-1);
    lnk[prev2] = pos;
    effY[prev2] = getMax(prev2, lnk[prev2]-1);
    return;
}

void setY(int pos, int val) {
    ll i = pos;
    y[i] = val;
    i += pow2;
    segMax[i] = val;
    i /= 2;
    while (i) {
        segMax[i] = max(segMax[2*i], segMax[2*i+1]);
        i /= 2;
    }

    ll prev2 = getPrev2Id(pos-1);
    effY[prev2] = getMax(prev2, lnk[prev2]-1);
    effY[pos] = getMax(pos, lnk[pos]-1);
    return;
}

int getOFId(int i = 1, ll prod = 1) {
    if (i >= pow2) return i-pow2;
    if (segOF[2*i+1] || segMul[2*i+1] * prod >= MOD) return getOFId(2*i+1, prod);
    return getOFId(2*i, prod * segMul[2*i+1]);
}

int getMulMod(int i) {
    int low = pow2, high = pow2+i;
    ll res = 1;
    while (low <= high) {
        if (low & 1) res = (res * segMul[low++]) % MOD;
        if (!(high & 1)) res = (res * segMul[high--]) % MOD;
        low /= 2; high /= 2;
    }
    return res % MOD;
}

int calc() {
    ll ofId = getOFId();
    ll mx = effY[ofId];
    ll offCnt = getMulMod(ofId);
    ll tCnt = 1;
    ll mxRes = (effY[ofId] * offCnt) % MOD;
    ofId = lnk[ofId];
    if (!segOF[1]) {
        offCnt = 1;
        ofId = 0;
        mx = 0;
    }
    for (int i = ofId; i < n; i = lnk[i]) {
        offCnt *= x[i];
        offCnt %= MOD;
        tCnt *= x[i];
        ll val = tCnt * effY[i];
        if (val > mx) {
            mx = val;
            mxRes = (offCnt * effY[i]) % MOD;
        }
    }
    return mxRes;
}

int init(int N, int X[], int Y[]) {
	n = N; pow2 = 1ll << (ll)ceil(log2(n));
    x = vector<ll>(n);
    y = vector<ll>(n);
    lnk = vector<ll>(n);
    effY = vector<ll>(n);
    ll lastNon1 = n;
    for (int i = n-1; i >= 0; i--) {
        x[i] = X[i];
        y[i] = Y[i];
        lnk[i] = lastNon1;
        if (x[i] > 1) lastNon1 = i;
    }
    segMul = vector<ll>(2*pow2, 1);
    segMax = vector<ll>(2*pow2, 1);
    segOF = vector<bool>(2*pow2);
    for (int i = 0; i < n; i++) {
        segMul[pow2+i] = x[i];
        segMax[pow2+i] = y[i];
    }
    for (int i = pow2-1; i > 0; i--) {
        segMul[i] = segMul[2*i] * segMul[2*i+1];
        if (segOF[2*i] || segOF[2*i+1] || segMul[i] >= MOD) {
            segMul[i] %= MOD;
            segOF[i] = true;
        }
        segMax[i] = max(segMax[2*i], segMax[2*i+1]);
    }
    for (int i = 0; i < n; i = lnk[i]) {
        if (lnk[i] == i+1) effY[i] = y[i];
        else effY[i] = getMax(i, lnk[i]-1);
    }

    return calc();
}

int updateX(int pos, int val) {
    setX(pos, val);
    ll val111 = effY[3493];
    ll id = find(y.begin(), y.end(), 999825195) - y.begin();
    ll v = calc();
	return v;
}

int updateY(int pos, int val) {
    bool hi = true;
    setY(pos, val);
    ll v = calc();
	return v;
}

Compilation message

horses.cpp: In function 'int getPrev2Id(int)':
horses.cpp:21:7: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   21 |     i += pow2;
      |     ~~^~~~~~~
horses.cpp:34:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   34 |     return i-pow2;
      |            ~^~~~~
horses.cpp: In function 'int getMax(ll, ll)':
horses.cpp:45:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   45 |     return res;
      |            ^~~
horses.cpp: In function 'void setX(int, int)':
horses.cpp:51:20: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
   51 |     int old = x[pos];
      |                    ^
horses.cpp:53:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   53 |     pos += pow2;
      |     ~~~~^~~~~~~
horses.cpp: In function 'int getOFId(int, ll)':
horses.cpp:109:28: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  109 |     if (i >= pow2) return i-pow2;
      |                           ~^~~~~
horses.cpp: In function 'int getMulMod(int)':
horses.cpp:115:15: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  115 |     int low = pow2, high = pow2+i;
      |               ^~~~
horses.cpp:115:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  115 |     int low = pow2, high = pow2+i;
      |                            ~~~~^~
horses.cpp:122:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  122 |     return res % MOD;
      |                ^
horses.cpp: In function 'int calc()':
horses.cpp:128:27: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  128 |     ll offCnt = getMulMod(ofId);
      |                           ^~~~
horses.cpp:137:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  137 |     for (int i = ofId; i < n; i = lnk[i]) {
      |                  ^~~~
horses.cpp:137:40: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
  137 |     for (int i = ofId; i < n; i = lnk[i]) {
      |                                        ^
horses.cpp:147:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  147 |     return mxRes;
      |            ^~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:157:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  157 |     for (int i = n-1; i >= 0; i--) {
      |                  ~^~
horses.cpp:170:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  170 |     for (int i = pow2-1; i > 0; i--) {
      |                  ~~~~^~
horses.cpp:178:37: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
  178 |     for (int i = 0; i < n; i = lnk[i]) {
      |                                     ^
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:191:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  191 |  return v;
      |         ^
horses.cpp:188:8: warning: unused variable 'val111' [-Wunused-variable]
  188 |     ll val111 = effY[3493];
      |        ^~~~~~
horses.cpp:189:8: warning: unused variable 'id' [-Wunused-variable]
  189 |     ll id = find(y.begin(), y.end(), 999825195) - y.begin();
      |        ^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:198:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  198 |  return v;
      |         ^
horses.cpp:195:10: warning: unused variable 'hi' [-Wunused-variable]
  195 |     bool hi = true;
      |          ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 436 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 440 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 456 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 41300 KB Output is correct
2 Correct 87 ms 50004 KB Output is correct
3 Correct 65 ms 41300 KB Output is correct
4 Correct 78 ms 45236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 440 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 528 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 30 ms 40512 KB Output is correct
34 Correct 29 ms 40508 KB Output is correct
35 Correct 51 ms 47332 KB Output is correct
36 Correct 48 ms 47416 KB Output is correct
37 Correct 22 ms 38744 KB Output is correct
38 Correct 24 ms 39660 KB Output is correct
39 Correct 18 ms 38544 KB Output is correct
40 Correct 35 ms 42324 KB Output is correct
41 Correct 19 ms 38492 KB Output is correct
42 Correct 19 ms 38740 KB Output is correct
43 Correct 33 ms 42832 KB Output is correct
44 Correct 38 ms 42968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 436 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 50 ms 41268 KB Output is correct
34 Correct 80 ms 49916 KB Output is correct
35 Correct 57 ms 41304 KB Output is correct
36 Correct 70 ms 45064 KB Output is correct
37 Correct 27 ms 40524 KB Output is correct
38 Correct 28 ms 40532 KB Output is correct
39 Correct 48 ms 47444 KB Output is correct
40 Correct 48 ms 47404 KB Output is correct
41 Correct 21 ms 38748 KB Output is correct
42 Correct 23 ms 39612 KB Output is correct
43 Correct 18 ms 38604 KB Output is correct
44 Correct 35 ms 42568 KB Output is correct
45 Correct 21 ms 38480 KB Output is correct
46 Correct 22 ms 38748 KB Output is correct
47 Correct 34 ms 42864 KB Output is correct
48 Correct 32 ms 42832 KB Output is correct
49 Correct 69 ms 42580 KB Output is correct
50 Correct 72 ms 42580 KB Output is correct
51 Correct 67 ms 49284 KB Output is correct
52 Correct 66 ms 48724 KB Output is correct
53 Correct 77 ms 40788 KB Output is correct
54 Correct 46 ms 41300 KB Output is correct
55 Correct 37 ms 39504 KB Output is correct
56 Correct 76 ms 44372 KB Output is correct
57 Correct 48 ms 40272 KB Output is correct
58 Correct 53 ms 40684 KB Output is correct
59 Correct 35 ms 42932 KB Output is correct