답안 #1003237

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003237 2024-06-20T08:08:37 Z Ausp3x 말 (IOI15_horses) C++17
100 / 100
175 ms 42320 KB
// 人外有人,天外有天
// author: Ausp3x
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include "horses.h"
typedef long long             lng;
typedef unsigned int          uint;
typedef unsigned long long    ulng;
using namespace std;
using namespace __gnu_pbds;
 
int const INF32 = 0x3f3f3f3f;
lng const INF64 = 0x3f3f3f3f3f3f3f3f;
 
int const MOD = 1000000007;

int nextPowOf2(int n) {
    n--;
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    n++;

    return n;
}

int N;
vector<int> X, Y;

int N2;
// actual prod, modulo prod
vector<pair<lng, lng>> x_seg;  
vector<int> i_seg;

lng queryXSeg(int l, int r, int opt) {
    l += N2;
    r += N2;

    lng res = 1;
    if (opt == 1) {
        while (l <= r) {
            if (l & 1) {
                res *= x_seg[l++].first;
                if (res > INF32)
                    res = INF32;
            } 
            if (!(r & 1)) {
                res *= x_seg[r--].first;
                if (res > INF32)
                    res = INF32;
            }

            l >>= 1;
            r >>= 1;
        }
    } else if (opt == 2) {
        while (l <= r) {
            if (l & 1)
                (res *= x_seg[l++].second) %= MOD;
            if (!(r & 1))
                (res *= x_seg[r--].second) %= MOD;

            l >>= 1;
            r >>= 1;
        }
    }

    return res;
}

void updateISeg(int i) {
    (i += N2) >>= 1;
    while (i > 0) {
        if (i_seg[2 * i + 1] == -1)
            i_seg[i] = i_seg[2 * i];
        else {
            if (Y[i_seg[2 * i]] >= queryXSeg(i_seg[2 * i] + 1, i_seg[2 * i + 1], 1) * Y[i_seg[2 * i + 1]])
                i_seg[i] = i_seg[2 * i];
            else
                i_seg[i] = i_seg[2 * i + 1];
        }

        i >>= 1;
    }

    return;
}

int init(int n, int x[], int y[]) {
    N = n;
    X.clear();
    X.resize(N);
    Y.clear();
    Y.resize(N);
    for (int i = 0; i < N; i++) {
        X[i] = x[i];
        Y[i] = y[i];
    }
    
    N2 = nextPowOf2(N);
    x_seg.clear();
    x_seg.resize(2 * N2, {-1, -1});
    for (int i = N2; i < N2 + N; i++) {
        x_seg[i].first = X[i - N2];
        x_seg[i].second = X[i - N2];
    }
    for (int d = N2 / 2; d >= 1; d >>= 1)
        for (int i = d; i < 2 * d; i++) {
            if (x_seg[2 * i + 1].first == -1)
                x_seg[i] = x_seg[2 * i];
            else {
                x_seg[i].first = x_seg[2 * i].first * x_seg[2 * i + 1].first;
                if (x_seg[i].first > INF32)
                    x_seg[i].first = INF32;
                (x_seg[i].second = x_seg[2 * i].second * x_seg[2 * i + 1].second) %= MOD;
            }
        }
    
    // for (auto [x, y] : x_seg)
    //     cout << x << ' ';
    // cout << endl;
    // for (auto [x, y] : x_seg)
    //     cout << y << ' ';
    // cout << endl;
    
    i_seg.clear();
    i_seg.resize(2 * N2, -1);
    for (int i = N2; i < N2 + N; i++)
        i_seg[i] = i - N2;
    for (int d = N2 / 2; d >= 1; d >>= 1)
        for (int i = d; i < 2 * d; i++) {
            if (i_seg[2 * i + 1] == -1)
                i_seg[i] = i_seg[2 * i];
            else {
                if (Y[i_seg[2 * i]] >= queryXSeg(i_seg[2 * i] + 1, i_seg[2 * i + 1], 1) * Y[i_seg[2 * i + 1]])
                    i_seg[i] = i_seg[2 * i];
                else
                    i_seg[i] = i_seg[2 * i + 1];
            }
        }
    
    // for (int x : i_seg)
    //     cout << x << ' ';
    // cout << endl;

    return queryXSeg(0, i_seg[1], 2) * Y[i_seg[1]] % MOD;
}

int updateX(int i, int v) {
    X[i] = v;

    int ii = i + N2;
    x_seg[ii] = {v, v};
    ii >>= 1;
    while (ii > 0) {
        if (x_seg[2 * ii + 1].first == -1)
            x_seg[ii] = x_seg[2 * ii];
        else {
            x_seg[ii].first = x_seg[2 * ii].first * x_seg[2 * ii + 1].first;
            if (x_seg[ii].first > INF32)
                x_seg[ii].first = INF32;
            (x_seg[ii].second = x_seg[2 * ii].second * x_seg[2 * ii + 1].second) %= MOD;
        }

        ii >>= 1;
    }

    updateISeg(i);

    return queryXSeg(0, i_seg[1], 2) * Y[i_seg[1]] % MOD;
}

int updateY(int i, int v) {
    Y[i] = v;

    updateISeg(i);

    return queryXSeg(0, i_seg[1], 2) * Y[i_seg[1]] % MOD;
}

/*
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int x[n];
        for (int i = 0; i < n; i++) {
            cin >> x[i];
        }
        int y[n];
        for (int i = 0; i < n; i++) {
            cin >> y[i];
        }

        cout << init(n, x, y) << endl;

        int m;
        cin >> m;
        while (m--) {
            int opt, pos, val;
            cin >> opt >> pos >> val;
            if (opt == 1) {
                cout << opt << ' ' << pos << ' ' << val << ' ';
                cout << updateX(pos, val) << endl;
            } else if (opt == 2) {
                cout << opt << ' ' << pos << ' ' << val << ' ';
                cout << updateY(pos, val) << endl;
            }
        }
    }

    return 0;
}
//*/

Compilation message

horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:149:52: warning: conversion from 'lng' {aka 'long long int'} to 'int' may change value [-Wconversion]
  149 |     return queryXSeg(0, i_seg[1], 2) * Y[i_seg[1]] % MOD;
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:173:52: warning: conversion from 'lng' {aka 'long long int'} to 'int' may change value [-Wconversion]
  173 |     return queryXSeg(0, i_seg[1], 2) * Y[i_seg[1]] % MOD;
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:181:52: warning: conversion from 'lng' {aka 'long long int'} to 'int' may change value [-Wconversion]
  181 |     return queryXSeg(0, i_seg[1], 2) * Y[i_seg[1]] % MOD;
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 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 432 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 436 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 344 KB Output is correct
13 Correct 0 ms 344 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 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 360 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 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 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 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 520 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 33548 KB Output is correct
2 Correct 139 ms 42320 KB Output is correct
3 Correct 113 ms 33536 KB Output is correct
4 Correct 120 ms 37204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 436 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 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 1 ms 348 KB Output is correct
13 Correct 0 ms 432 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 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 1 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 1 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 1 ms 600 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 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 43 ms 32776 KB Output is correct
34 Correct 44 ms 32596 KB Output is correct
35 Correct 58 ms 39796 KB Output is correct
36 Correct 57 ms 39504 KB Output is correct
37 Correct 32 ms 30812 KB Output is correct
38 Correct 30 ms 31836 KB Output is correct
39 Correct 27 ms 30800 KB Output is correct
40 Correct 47 ms 34684 KB Output is correct
41 Correct 34 ms 30808 KB Output is correct
42 Correct 28 ms 30808 KB Output is correct
43 Correct 37 ms 35164 KB Output is correct
44 Correct 37 ms 35152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 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 1 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 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 432 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 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 1 ms 348 KB Output is correct
31 Correct 1 ms 500 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 60 ms 29528 KB Output is correct
34 Correct 141 ms 29632 KB Output is correct
35 Correct 125 ms 29604 KB Output is correct
36 Correct 136 ms 29684 KB Output is correct
37 Correct 44 ms 28760 KB Output is correct
38 Correct 44 ms 32788 KB Output is correct
39 Correct 65 ms 39532 KB Output is correct
40 Correct 61 ms 39692 KB Output is correct
41 Correct 32 ms 30800 KB Output is correct
42 Correct 33 ms 31824 KB Output is correct
43 Correct 31 ms 30804 KB Output is correct
44 Correct 54 ms 34804 KB Output is correct
45 Correct 27 ms 30736 KB Output is correct
46 Correct 27 ms 30740 KB Output is correct
47 Correct 37 ms 35132 KB Output is correct
48 Correct 47 ms 34988 KB Output is correct
49 Correct 149 ms 34644 KB Output is correct
50 Correct 160 ms 34640 KB Output is correct
51 Correct 86 ms 41480 KB Output is correct
52 Correct 98 ms 41028 KB Output is correct
53 Correct 175 ms 33036 KB Output is correct
54 Correct 108 ms 33520 KB Output is correct
55 Correct 97 ms 31880 KB Output is correct
56 Correct 116 ms 36412 KB Output is correct
57 Correct 96 ms 32404 KB Output is correct
58 Correct 80 ms 32876 KB Output is correct
59 Correct 36 ms 35040 KB Output is correct