Submission #710659

# Submission time Handle Problem Language Result Execution time Memory
710659 2023-03-15T13:51:04 Z ssense Horses (IOI15_horses) C++17
100 / 100
1457 ms 74664 KB
#include <bits/stdc++.h>
#include "horses.h"

using namespace std;

#define MOD 1000000007

int ng;
vector<int> xg;

long long power(long long n, long long pow, long long m) {if (pow == 0) return 1;if (pow % 2 == 0) {long long x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;}

template<typename T>
struct LazySegmentTree{
    int n;
    vector<T> t, lazy, a;
    T neutral, lazy_neutral;
    function<T(const T&, const T&)> merge;
    function<T(const T&, const T&)> upd;
    
    bool range_modif = false;
    LazySegmentTree(int _n, T _neutral, T _lazy_neutral, const function<T(const T&, const T&)> &_merge, const function<T(const T&, const T&)> &_upd, bool _range_modif){
        range_modif = _range_modif;
        init(_n, _neutral, _lazy_neutral, _merge, _upd);
    }
    LazySegmentTree(vector<T> _a, T _neutral, T _lazy_neutral, const function<T(const T&, const T&)> &_merge, const function<T(const T&, const T&)> &_upd, bool _range_modif){
        range_modif = _range_modif, a = _a;
        int _n = (int)a.size();
        init(_n, _neutral, _lazy_neutral, _merge, _upd);
        build(1, 0, n - 1);
    }
    
    void init(int _n, T _neutral, T _lazy_neutral, const function<T(const T&, const T&)> &_merge, const function<T(const T&, const T&)> &_upd){
        n = _n, neutral = _neutral, lazy_neutral = _lazy_neutral, merge = _merge, upd = _upd;
        t.assign(4 * n, neutral);
        lazy.assign(4 * n, lazy_neutral);
    }
    
    void build(int i, int l, int r){
        if(l == r){
            t[i] = a[l];
            return;
        }
        
        int mid = (l + r) >> 1;
        build(2 * i, l, mid);
        build(2 * i + 1, mid + 1, r);
        
        t[i] = merge(t[2 * i], t[2 * i + 1]);
    }
    
    void push(int i, int l, int r){
        if(lazy[i] == lazy_neutral)return;
        
        t[i] = upd(t[i], lazy[i] * (range_modif ? (r - l + 1) : 1));
        
        if(l != r){
            lazy[2 * i] = upd(lazy[2 * i], lazy[i]);
            lazy[2 * i + 1] = upd(lazy[2 * i + 1], lazy[i]);
        }
        lazy[i] = lazy_neutral;
    }
    
    void modif(int i, int l, int r, int tl, int tr, T val){
        push(i, l, r);
        if(l > tr || r < tl)return;
        
        if(l >= tl && r <= tr){
            lazy[i] = upd(lazy[i], val);
            push(i, l, r);
            return;
        }
        
        int mid = (l + r) >> 1;
        
        modif(2 * i, l, mid, tl, tr, val);
        modif(2 * i + 1, mid + 1, r, tl, tr, val);
        
        t[i] = merge(t[2 * i], t[2 * i + 1]);
    }
    T query(int i, int l, int r, int tl, int tr){
        push(i, l, r);
        if(l > tr || r < tl)return neutral;
        if(l >= tl && r <= tr)return t[i];
        
        int mid = (l + r) >> 1;
        
        T left = query(2 * i, l, mid, tl, tr);
        T right = query(2 * i + 1, mid + 1, r, tl, tr);
        
        return merge(left, right);
    }
    
    void modif(int l, int r, T val){
        modif(1, 0, n - 1, l, r, val);
    }
    void modif(int poz, T val){
        modif(1, 0, n - 1, poz, poz, val);
    }
    T query(int l, int r){
        return query(1, 0, n - 1, l, r);
    }
    T query(int poz){
        return query(1, 0, n - 1, poz, poz);
    }
    //n, neutral, lazy neutral, merge, upd, bool range update
};

int mergex(int a, int b)
{
    long long temp = a;
    temp*=b;
    return (int)(temp%MOD);
}

int updx(int a, int b)
{
    return b;
}

int mergey(int a, int b)
{
    return max(a, b);
}

int updy(int a, int b)
{
    return b;
}

LazySegmentTree<int> stx(500000, 1, -1, mergex, updx, 0);
LazySegmentTree<int> sty(500000, 0, -1, mergey, updy, 0);

set<int> s;

int init(int n, int* x, int* y)
{
    ng = n;
    s.insert(0);
    s.insert(ng);
    for(int i = 0; i < ng; i++)
    {
        stx.modif(i, x[i]);
        xg.push_back(x[i]);
        if(x[i] > 1)
        {
            s.insert(i);
        }
        sty.modif(i, y[i]);
    }
    auto it = s.end();
    it--;
    long long pref = 1;
    while(true)
    {
        if(*it == ng)
        {
            it--;
            continue;
        }
        pref*=xg[*it];
        if(pref > 1000000000)
        {
            break;
        }
        if(it == s.begin())
        {
            break;
        }
        it--;
    }
    auto first = it;
    int should_multiply = stx.query(0LL, *it);
    long long now = 1;
    long long mx = 0;
    for(; it != s.end(); it++)
    {
        if(it != first)
        {
            now*=xg[*it];
        }
        auto urmatorul = it;
        urmatorul++;
        if(urmatorul == s.end()){break;}
        long long now_range = now*sty.query(*it, (*urmatorul)-1);
        mx = max(mx, now_range);
    }
    mx%=MOD;
    return (int)((mx*should_multiply)%MOD);
}

int updateX(int pos, int val)
{
    if(stx.query(pos) == 1 && val != 1)
    {
        s.insert(pos);
    }
    else if(stx.query(pos) != 1 && val == 1)
    {
        if(pos!= 0)
        {
            s.erase(s.find(pos));
        }
    }
    xg[pos] = val;
    stx.modif(pos, val);
    auto it = s.end();
    it--;
    long long pref = 1;
    while(true)
    {
        if(*it == ng)
        {
            it--;
            continue;
        }
        pref*=xg[*it];
        if(pref > 1000000000)
        {
            break;
        }
        if(it == s.begin())
        {
            break;
        }
        it--;
    }
    auto first = it;
    long long should_multiply = stx.query(0LL, *it);
    long long now = 1;
    long long mx = 0;
    for(; it != s.end(); it++)
    {
        if(it != first)
        {
            now*=xg[*it];
        }
        auto urmatorul = it;
        urmatorul++;
        if(urmatorul == s.end()){break;}
        long long now_range = now*sty.query(*it, *urmatorul-1);
        mx = max(mx, now_range);
    }
    mx%=MOD;
    return (int)((mx*should_multiply)%MOD);
}

int updateY(int pos, int val)
{
    sty.modif(pos, val);
    auto it = s.end();
    it--;
    long long pref = 1;
    while(true)
    {
        if(*it == ng)
        {
            it--;
            continue;
        }
        pref*=xg[*it];
        if(pref > 1000000000)
        {
            break;
        }
        if(it == s.begin())
        {
            break;
        }
        it--;
    }
    auto first = it;
    long long should_multiply = stx.query(0LL, *it);
    long long now = 1;
    long long mx = 0;
    for(; it != s.end(); it++)
    {
        if(it != first)
        {
            now*=xg[*it];
        }
        auto urmatorul = it;
        urmatorul++;
        if(urmatorul == s.end()){break;}
        long long now_range = now*sty.query(*it, *urmatorul-1);
        mx = max(mx, now_range);
    }
    mx%=MOD;
    return (int)((mx*should_multiply)%MOD);
}

Compilation message

horses.cpp: In function 'int updx(int, int)':
horses.cpp:116:14: warning: unused parameter 'a' [-Wunused-parameter]
  116 | int updx(int a, int b)
      |          ~~~~^
horses.cpp: In function 'int updy(int, int)':
horses.cpp:126:14: warning: unused parameter 'a' [-Wunused-parameter]
  126 | int updy(int a, int b)
      |          ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 19 ms 31616 KB Output is correct
2 Correct 14 ms 31560 KB Output is correct
3 Correct 16 ms 31632 KB Output is correct
4 Correct 16 ms 31572 KB Output is correct
5 Correct 17 ms 31536 KB Output is correct
6 Correct 15 ms 31572 KB Output is correct
7 Correct 18 ms 31572 KB Output is correct
8 Correct 15 ms 31556 KB Output is correct
9 Correct 18 ms 31520 KB Output is correct
10 Correct 15 ms 31572 KB Output is correct
11 Correct 14 ms 31520 KB Output is correct
12 Correct 14 ms 31572 KB Output is correct
13 Correct 15 ms 31612 KB Output is correct
14 Correct 15 ms 31556 KB Output is correct
15 Correct 15 ms 31572 KB Output is correct
16 Correct 15 ms 31572 KB Output is correct
17 Correct 14 ms 31572 KB Output is correct
18 Correct 14 ms 31532 KB Output is correct
19 Correct 14 ms 31584 KB Output is correct
20 Correct 15 ms 31572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31632 KB Output is correct
2 Correct 14 ms 31572 KB Output is correct
3 Correct 14 ms 31572 KB Output is correct
4 Correct 14 ms 31632 KB Output is correct
5 Correct 14 ms 31612 KB Output is correct
6 Correct 15 ms 31592 KB Output is correct
7 Correct 16 ms 31572 KB Output is correct
8 Correct 17 ms 31540 KB Output is correct
9 Correct 15 ms 31596 KB Output is correct
10 Correct 17 ms 31572 KB Output is correct
11 Correct 15 ms 31556 KB Output is correct
12 Correct 17 ms 31540 KB Output is correct
13 Correct 18 ms 31524 KB Output is correct
14 Correct 15 ms 31548 KB Output is correct
15 Correct 16 ms 31628 KB Output is correct
16 Correct 14 ms 31572 KB Output is correct
17 Correct 14 ms 31572 KB Output is correct
18 Correct 15 ms 31608 KB Output is correct
19 Correct 15 ms 31572 KB Output is correct
20 Correct 15 ms 31572 KB Output is correct
21 Correct 15 ms 31608 KB Output is correct
22 Correct 15 ms 31572 KB Output is correct
23 Correct 21 ms 31620 KB Output is correct
24 Correct 22 ms 31600 KB Output is correct
25 Correct 19 ms 31688 KB Output is correct
26 Correct 18 ms 31784 KB Output is correct
27 Correct 31 ms 31572 KB Output is correct
28 Correct 18 ms 31700 KB Output is correct
29 Correct 17 ms 31572 KB Output is correct
30 Correct 22 ms 31724 KB Output is correct
31 Correct 21 ms 31660 KB Output is correct
32 Correct 23 ms 31672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1457 ms 61964 KB Output is correct
2 Correct 755 ms 62472 KB Output is correct
3 Correct 784 ms 62432 KB Output is correct
4 Correct 861 ms 62524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 19 ms 31616 KB Output is correct
3 Correct 16 ms 31580 KB Output is correct
4 Correct 15 ms 31548 KB Output is correct
5 Correct 19 ms 31568 KB Output is correct
6 Correct 16 ms 31572 KB Output is correct
7 Correct 14 ms 31572 KB Output is correct
8 Correct 14 ms 31548 KB Output is correct
9 Correct 14 ms 31572 KB Output is correct
10 Correct 14 ms 31572 KB Output is correct
11 Correct 18 ms 31584 KB Output is correct
12 Correct 18 ms 31540 KB Output is correct
13 Correct 16 ms 31572 KB Output is correct
14 Correct 19 ms 31548 KB Output is correct
15 Correct 16 ms 31576 KB Output is correct
16 Correct 20 ms 31632 KB Output is correct
17 Correct 15 ms 31572 KB Output is correct
18 Correct 17 ms 31572 KB Output is correct
19 Correct 16 ms 31572 KB Output is correct
20 Correct 15 ms 31576 KB Output is correct
21 Correct 14 ms 31572 KB Output is correct
22 Correct 15 ms 31572 KB Output is correct
23 Correct 19 ms 31660 KB Output is correct
24 Correct 18 ms 31632 KB Output is correct
25 Correct 19 ms 31636 KB Output is correct
26 Correct 18 ms 31700 KB Output is correct
27 Correct 25 ms 31572 KB Output is correct
28 Correct 25 ms 31616 KB Output is correct
29 Correct 18 ms 31560 KB Output is correct
30 Correct 21 ms 31624 KB Output is correct
31 Correct 21 ms 31572 KB Output is correct
32 Correct 24 ms 31620 KB Output is correct
33 Correct 355 ms 41912 KB Output is correct
34 Correct 363 ms 41568 KB Output is correct
35 Correct 559 ms 71964 KB Output is correct
36 Correct 600 ms 71848 KB Output is correct
37 Correct 448 ms 39704 KB Output is correct
38 Correct 431 ms 52540 KB Output is correct
39 Correct 374 ms 39708 KB Output is correct
40 Correct 525 ms 66984 KB Output is correct
41 Correct 364 ms 39732 KB Output is correct
42 Correct 419 ms 39788 KB Output is correct
43 Correct 476 ms 67316 KB Output is correct
44 Correct 536 ms 67296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 31572 KB Output is correct
2 Correct 15 ms 31572 KB Output is correct
3 Correct 17 ms 31572 KB Output is correct
4 Correct 18 ms 31548 KB Output is correct
5 Correct 19 ms 31548 KB Output is correct
6 Correct 17 ms 31568 KB Output is correct
7 Correct 15 ms 31572 KB Output is correct
8 Correct 15 ms 31528 KB Output is correct
9 Correct 15 ms 31572 KB Output is correct
10 Correct 15 ms 31548 KB Output is correct
11 Correct 15 ms 31572 KB Output is correct
12 Correct 15 ms 31572 KB Output is correct
13 Correct 14 ms 31572 KB Output is correct
14 Correct 14 ms 31572 KB Output is correct
15 Correct 15 ms 31560 KB Output is correct
16 Correct 17 ms 31516 KB Output is correct
17 Correct 15 ms 31572 KB Output is correct
18 Correct 16 ms 31528 KB Output is correct
19 Correct 15 ms 31572 KB Output is correct
20 Correct 15 ms 31572 KB Output is correct
21 Correct 15 ms 31572 KB Output is correct
22 Correct 15 ms 31584 KB Output is correct
23 Correct 17 ms 31572 KB Output is correct
24 Correct 17 ms 31584 KB Output is correct
25 Correct 19 ms 31828 KB Output is correct
26 Correct 16 ms 31700 KB Output is correct
27 Correct 31 ms 31572 KB Output is correct
28 Correct 22 ms 31676 KB Output is correct
29 Correct 24 ms 31564 KB Output is correct
30 Correct 18 ms 31700 KB Output is correct
31 Correct 24 ms 31652 KB Output is correct
32 Correct 34 ms 31572 KB Output is correct
33 Correct 1406 ms 65764 KB Output is correct
34 Correct 763 ms 74664 KB Output is correct
35 Correct 750 ms 65660 KB Output is correct
36 Correct 852 ms 69536 KB Output is correct
37 Correct 364 ms 41644 KB Output is correct
38 Correct 369 ms 41580 KB Output is correct
39 Correct 602 ms 71960 KB Output is correct
40 Correct 546 ms 71868 KB Output is correct
41 Correct 474 ms 39808 KB Output is correct
42 Correct 440 ms 52596 KB Output is correct
43 Correct 337 ms 39804 KB Output is correct
44 Correct 551 ms 67104 KB Output is correct
45 Correct 377 ms 39848 KB Output is correct
46 Correct 437 ms 39844 KB Output is correct
47 Correct 494 ms 67304 KB Output is correct
48 Correct 522 ms 67416 KB Output is correct
49 Correct 557 ms 44580 KB Output is correct
50 Correct 603 ms 44636 KB Output is correct
51 Correct 910 ms 73876 KB Output is correct
52 Correct 709 ms 73308 KB Output is correct
53 Correct 1424 ms 42944 KB Output is correct
54 Correct 779 ms 56500 KB Output is correct
55 Correct 545 ms 40916 KB Output is correct
56 Correct 723 ms 68792 KB Output is correct
57 Correct 936 ms 41380 KB Output is correct
58 Correct 1201 ms 41940 KB Output is correct
59 Correct 521 ms 67464 KB Output is correct