답안 #989051

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
989051 2024-05-27T11:05:39 Z crafticat 말 (IOI15_horses) C++17
20 / 100
632 ms 89632 KB
#include <bits/stdc++.h>

using namespace std;
constexpr int MOD = 1e9 + 7;
using ll = __int128;

vector<int> x, y;
set<int> exists;
ll global = 1;
int n;

ll myPOW(ll a, ll b) {
    if (b == 0) return 1;
    ll v = myPOW(a, b / 2);
    v *= v;
    v %= MOD;
    if (b % 2 == 1) v *= a;
    v %= MOD;
    return v;
}

ll inv(ll v) {
    return myPOW(v, MOD - 2);
}

struct Segment {
    Segment *left = nullptr, *right = nullptr;
    int l, r, mid;
    int val = 1;

    Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
        if (r - l > 1) {
            left = new Segment(l,mid);
            right = new Segment(mid,r);
        }
    }
    int q(int a, int b) {
        if (a >= r || b <= l) return 0;
        if (a <= l && b >= r) return val;

        return max(left->q(a,b), right->q(a,b));
    }
    void update(int pos, int u) {
        if (r - l <= 1) {
            val = u;
            return;
        }
        if (pos < mid)
            left->update(pos,u);
        else
            right->update(pos,u);
        val = max(left->val,right->val);
    }
};

Segment *seg;

int solve() {
    int cor = n - 1;
    ll mul = x[cor];
    stack<int> prop;
    prop.push(cor);

    while (mul < MOD) {
        auto it = exists.upper_bound(-cor);
        if (it == exists.end()) break;
        cor = -*it;
        mul *= x[cor];
        prop.push(cor);
    }

    ll left = global * inv(mul % MOD) % MOD;
    ll ans = 0;

    mul = 1;
    while (!prop.empty()) {
        auto c = prop.top(); prop.pop();
        ans = max(ans, mul * seg->q(cor,c));
        mul *= x[c];
        ans = max(ans, y[c] * mul);
        cor = c;
    }
    ans %= MOD;
    return (ans * left) % MOD;
}

int init(int N, int X[], int Y[]) {
    n = N;
    x.resize(N);
    y.resize(N);
    seg = new Segment(0,N);
    for (int i = 0; i < N; ++i) {
        x[i] = X[i];
        y[i] = Y[i];
        global *= x[i];
        global %= MOD;
        if (x[i] != 1) {
            exists.insert(-i);
        } else
            seg->update(i,y[i]);
    }

    return solve();
}

int updateX(int pos, int val) {
    if (exists.count(-pos)) {
        exists.erase(-pos);
    } else seg->update(pos,0);

    global *= inv(x[pos]);
    global %= MOD;
    x[pos] = val;
    global *= x[pos];
    global %= MOD;
    if (x[pos] != 1) {
        exists.insert(-pos);
    } else seg->update(pos,y[pos]);
    return solve();
}
int updateY(int pos, int val) {
    y[pos] = val;
    if (!exists.count(pos)) seg->update(pos,y[pos]);
    return solve();
}
#if DEBUG
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;

static inline int _read() {
    if (_charsNumber < 0) {
        exit(1);
    }
    if (!_charsNumber || _currentChar == _charsNumber) {
        _charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
        _currentChar = 0;
    }
    if (_charsNumber <= 0) {
        return -1;
    }
    return _buffer[_currentChar++];
}

static inline int _readInt() {
    int v; cin >> v;
    return v;
}


int main() {

	int N; N = _readInt();

	int *X = (int*)malloc(sizeof(int)*(unsigned int)N);
	int *Y = (int*)malloc(sizeof(int)*(unsigned int)N);

	for (int i = 0; i < N; i++) {
		X[i] = _readInt();
	}

	for (int i = 0; i < N; i++) {
		Y[i] = _readInt();
	}

	cout << init(N,X,Y) << endl;

	int M; M = _readInt();

	for (int i = 0; i < M; i++) {
		int type; type = _readInt();
		int pos; pos = _readInt();
		int val; val = _readInt();

		if (type == 1) {
			cout << updateX(pos,val) << endl;
		} else if (type == 2) {
            cout << updateY(pos,val) << endl;
		}
	}

	return 0;
}
#endif

Compilation message

horses.cpp: In constructor 'Segment::Segment(int, int)':
horses.cpp:31:24: warning: declaration of 'r' shadows a member of 'Segment' [-Wshadow]
   31 |     Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
      |                    ~~~~^
horses.cpp:28:12: note: shadowed declaration is here
   28 |     int l, r, mid;
      |            ^
horses.cpp:31:17: warning: declaration of 'l' shadows a member of 'Segment' [-Wshadow]
   31 |     Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
      |             ~~~~^
horses.cpp:28:9: note: shadowed declaration is here
   28 |     int l, r, mid;
      |         ^
horses.cpp: In constructor 'Segment::Segment(int, int)':
horses.cpp:31:24: warning: declaration of 'r' shadows a member of 'Segment' [-Wshadow]
   31 |     Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
      |                    ~~~~^
horses.cpp:28:12: note: shadowed declaration is here
   28 |     int l, r, mid;
      |            ^
horses.cpp:31:17: warning: declaration of 'l' shadows a member of 'Segment' [-Wshadow]
   31 |     Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
      |             ~~~~^
horses.cpp:28:9: note: shadowed declaration is here
   28 |     int l, r, mid;
      |         ^
horses.cpp: In constructor 'Segment::Segment(int, int)':
horses.cpp:31:24: warning: declaration of 'r' shadows a member of 'Segment' [-Wshadow]
   31 |     Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
      |                    ~~~~^
horses.cpp:28:12: note: shadowed declaration is here
   28 |     int l, r, mid;
      |            ^
horses.cpp:31:17: warning: declaration of 'l' shadows a member of 'Segment' [-Wshadow]
   31 |     Segment(int l, int r) : l(l), r(r), mid((r + l) / 2) {
      |             ~~~~^
horses.cpp:28:9: note: shadowed declaration is here
   28 |     int l, r, mid;
      |         ^
horses.cpp: In function 'int solve()':
horses.cpp:84:25: warning: conversion from 'll' {aka '__int128'} to 'int' may change value [-Wconversion]
   84 |     return (ans * left) % MOD;
      |            ~~~~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 436 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 Incorrect 0 ms 344 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Incorrect 0 ms 348 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 632 ms 83184 KB Output is correct
2 Correct 329 ms 89632 KB Output is correct
3 Correct 352 ms 82772 KB Output is correct
4 Correct 345 ms 85868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Incorrect 0 ms 348 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 0 ms 344 KB Output isn't correct
7 Halted 0 ms 0 KB -