Submission #796696

# Submission time Handle Problem Language Result Execution time Memory
796696 2023-07-28T15:46:44 Z jasmin Horses (IOI15_horses) C++17
100 / 100
493 ms 60816 KB
#include "horses.h"
#include<bits/stdc++.h>
using namespace std;

int n=0;
vector<int> x;
vector<int> y;

const long long MOD=1e9+7;

int MUL(long long a, long long b){
    return (a*b)%MOD;
}

struct maxsegtree{
    vector<int> tree;

    void init(){
        tree.assign(n*4, 0);
    }

    int update(int l, int r, int v, int x, int val){
        if(x<l || r<=x) return tree[v];
        if(l+1==r){
            return tree[v]=val;
        }
        int m=l+(r-l)/2;
        return tree[v]=max(update(l, m, v*2+1, x, val), update(m, r, v*2+2, x, val));
    }

    int query(int l, int r, int v, int ql, int qr){
        if(qr<=l || r<=ql) return 1;
        if(ql<=l && r<=qr){
            return tree[v];
        }
        int m=l+(r-l)/2;
        return max(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
    }
};
maxsegtree maxseg;

struct productsegtree{
    vector<int> tree;

    void init(){
        tree.assign(n*4, 1);
    }

    int update(int l, int r, int v, int x, int val){
        if(x<l || r<=x) return tree[v];
        if(l+1==r){
            return tree[v]=val;
        }
        int m=l+(r-l)/2;
        return tree[v] = MUL(update(l, m, v*2+1, x, val), update(m, r, v*2+2, x, val));
    }

    int query(int l, int r, int v, int ql, int qr){
        if(qr<=l || r<=ql) return 1;
        if(ql<=l && r<=qr){
            return tree[v];
        }
        int m=l+(r-l)/2;
        return MUL(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
    }
};
productsegtree pseg;

set<int> notone;

bool bigger(pair<long long, long long> a, pair<long long, long long> b){
    return a.first*b.second > a.second*b.first;
}

int solve(){

    auto it=notone.end();
    long long product=1;

    pair<int, long long> best={1, 1};
    int ind=n-1;
    bool mod=false;
    while(!mod && it!=notone.begin()){
        it=prev(it);
        int pos=*it;

        int yfac=maxseg.query(0, n, 0, pos, n);

        if(bigger({yfac, product}, best)){
            best={yfac, product};
            ind=pos;
        }

        product*=x[pos];
        if(product>MOD){

            product%=MOD;
            mod=true;
        }
    }

    //check first one
    int yfac=maxseg.query(0, n, 0, 0, n);
    product=pseg.query(0, n, 0, 1, n);
    if(!mod && bigger({yfac, product}, best)){

        best={yfac, product};
        ind = 0;
    }
    

    yfac=maxseg.query(0, n, 0, ind, n);
    int xfac=pseg.query(0, n, 0, 0, ind+1);

    int ans=MUL(xfac, yfac);
    return ans;
}

int init(int N, int X[], int Y[]) {
    
    n=N;
    x.assign(n, 0);
    for(int i=0; i<n; i++){
        x[i]=X[i];
    }
    y.assign(n, 0);
    for(int i=0; i<n; i++){
        y[i]=Y[i];
    }


    maxseg.init();
    for(int i=0; i<n; i++){
        maxseg.update(0, n, 0, i, y[i]);
    }
    pseg.init();
    for(int i=0; i<n; i++){
        pseg.update(0, n, 0, i, x[i]);
    }

    for(int i=0; i<n; i++){
        if(x[i]!=1){
            notone.insert(i);
        }
    }

    return solve();
}


int updateX(int pos, int val) {	

    if(x[pos]==1 && val!=1){
        notone.insert(pos);
    }
    if(x[pos]!=1 && val==1){
        notone.erase(pos);
    }

    x[pos]=val;
    pseg.update(0, n, 0, pos, val);
    
    return solve();
}

int updateY(int pos, int val) {
	
    y[pos]=val;
    maxseg.update(0, n, 0, pos, val);

    return solve();
}

Compilation message

horses.cpp: In function 'int MUL(long long int, long long int)':
horses.cpp:12:17: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   12 |     return (a*b)%MOD;
      |            ~~~~~^~~~
horses.cpp: In member function 'int maxsegtree::update(int, int, int, int, int)':
horses.cpp:22:41: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   22 |     int update(int l, int r, int v, int x, int val){
      |                                     ~~~~^
horses.cpp:6:13: note: shadowed declaration is here
    6 | vector<int> x;
      |             ^
horses.cpp: In member function 'int productsegtree::update(int, int, int, int, int)':
horses.cpp:49:41: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   49 |     int update(int l, int r, int v, int x, int val){
      |                                     ~~~~^
horses.cpp:6:13: note: shadowed declaration is here
    6 | vector<int> x;
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 304 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 300 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 312 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 493 ms 48260 KB Output is correct
2 Correct 353 ms 60816 KB Output is correct
3 Correct 322 ms 51932 KB Output is correct
4 Correct 347 ms 55888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 308 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 162 ms 27812 KB Output is correct
34 Correct 159 ms 27844 KB Output is correct
35 Correct 269 ms 58224 KB Output is correct
36 Correct 270 ms 58112 KB Output is correct
37 Correct 173 ms 26164 KB Output is correct
38 Correct 202 ms 38768 KB Output is correct
39 Correct 159 ms 25820 KB Output is correct
40 Correct 261 ms 53244 KB Output is correct
41 Correct 161 ms 25808 KB Output is correct
42 Correct 168 ms 25868 KB Output is correct
43 Correct 264 ms 53636 KB Output is correct
44 Correct 272 ms 53556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 492 ms 51992 KB Output is correct
34 Correct 349 ms 60720 KB Output is correct
35 Correct 356 ms 52040 KB Output is correct
36 Correct 367 ms 55828 KB Output is correct
37 Correct 164 ms 27868 KB Output is correct
38 Correct 159 ms 27816 KB Output is correct
39 Correct 288 ms 58100 KB Output is correct
40 Correct 286 ms 58088 KB Output is correct
41 Correct 169 ms 25996 KB Output is correct
42 Correct 209 ms 38748 KB Output is correct
43 Correct 150 ms 25856 KB Output is correct
44 Correct 263 ms 53148 KB Output is correct
45 Correct 163 ms 25944 KB Output is correct
46 Correct 172 ms 25876 KB Output is correct
47 Correct 276 ms 53608 KB Output is correct
48 Correct 264 ms 53744 KB Output is correct
49 Correct 249 ms 30928 KB Output is correct
50 Correct 246 ms 30984 KB Output is correct
51 Correct 419 ms 60032 KB Output is correct
52 Correct 337 ms 59496 KB Output is correct
53 Correct 396 ms 29180 KB Output is correct
54 Correct 333 ms 42712 KB Output is correct
55 Correct 219 ms 26816 KB Output is correct
56 Correct 343 ms 55124 KB Output is correct
57 Correct 318 ms 27504 KB Output is correct
58 Correct 385 ms 27980 KB Output is correct
59 Correct 245 ms 53552 KB Output is correct