Submission #283295

# Submission time Handle Problem Language Result Execution time Memory
283295 2020-08-25T13:53:24 Z stoyan_malinin Horses (IOI15_horses) C++14
100 / 100
671 ms 138748 KB
#include "horses.h"
//#include "grader.cpp"

#include <cmath>
#include <vector>
#include <iostream>

#define double long double

using namespace std;

const int MAXN = 5e5 + 5;
const long long mod = 1e9 + 7;

int n;
double lnPref[MAXN];
int x[MAXN], y[MAXN];

struct NodeInfo
{
    int bestInd;
    double val, lazy;
    long long product;

    NodeInfo(){}
    NodeInfo(int x, double val, int ind)
    {
        this->product = x%mod;
        this->bestInd = ind;
        this->val = val;
        this->lazy = 0;
    }
};

NodeInfo Merge(NodeInfo A, NodeInfo B)
{
    NodeInfo C;
    C.lazy = 0;

    if(A.val<B.val) swap(A, B);
    C.val = A.val;C.bestInd = A.bestInd;

    C.product = (A.product*B.product)%mod;

    return C;
}

struct SegmentTree
{
    NodeInfo info;

    int l, r;
    SegmentTree *L, *R;

    SegmentTree(){}
    SegmentTree(int l, int r)
    {
        this->l = l;
        this->r = r;

        this->L = nullptr;
        this->R = nullptr;
    }

    void build()
    {
        if(l==r)
        {
            this->info = NodeInfo(x[l], lnPref[l]+log(y[l]), l);
            return;
        }

        L = new SegmentTree(l, (l+r)/2);
        R = new SegmentTree((l+r)/2+1, r);

        L->build();
        R->build();
        info = Merge(L->info, R->info);
    }

    void updateLazy()
    {
        info.val += info.lazy;
        if(l!=r)
        {
            L->info.lazy += info.lazy;
            R->info.lazy += info.lazy;
        }

        info.lazy = 0;
    }

    void updateVal(int ql, int qr, double change)
    {
        updateLazy();

        if(l>=ql && r<=qr)
        {
            info.val += change;
            if(l!=r)
            {
                L->info.lazy += change;
                R->info.lazy += change;
            }

            return;
        }
        if(r<ql || l>qr) return;

        L->updateVal(ql, qr, change);
        R->updateVal(ql, qr, change);
        info = Merge(L->info, R->info);
    }

    void updateProduct(int q, int newVal)
    {
        updateLazy();

        if(l==r && l==q)
        {
            info.product = newVal;
            return;
        }
        if(r<q || l>q) return;

        L->updateProduct(q, newVal);
        R->updateProduct(q, newVal);
        info = Merge(L->info, R->info);
    }

    long long queryProduct(int ql, int qr)
    {
        updateLazy();

        if(l>=ql && r<=qr) return info.product;
        if(r<ql || l>qr) return 1;

        return (L->queryProduct(ql, qr)*R->queryProduct(ql, qr))%mod;
    }

    void print()
    {
        cout << l << " " << r << " -> " << info.val << " " << info.bestInd << '\n';
        if(l!=r)
        {
            L->print();
            R->print();
        }
    }
};

SegmentTree *T;

long long evalInd(int ind)
{
    long long ans = T->queryProduct(0, ind);
    return (ans*y[ind])%mod;
}

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

    lnPref[0] = log(x[0]);
    for(int i = 1;i<n;i++) lnPref[i] = lnPref[i-1] + log(x[i]);

    T = new SegmentTree(0, n-1);
    T->build();

    //T->print();

    return evalInd(T->info.bestInd);
}

int updateX(int pos, int val)
{
	double change = log(val) - log(x[pos]);
	x[pos] = val;

    T->updateVal(pos, n-1, change);
    T->updateProduct(pos, x[pos]);

    return evalInd(T->info.bestInd);
}

int updateY(int pos, int val)
{
    double change = log(val) - log(y[pos]);
	y[pos] = val;

    T->updateVal(pos, pos, change);
    //cout << T->info.bestInd << " " << change << '\n';

    //T->print();

    return evalInd(T->info.bestInd);
}

Compilation message

horses.cpp: In constructor 'NodeInfo::NodeInfo(int, long double, int)':
horses.cpp:27:5: warning: declaration of 'val' shadows a member of 'NodeInfo' [-Wshadow]
   27 |     {
      |     ^
horses.cpp:22:12: note: shadowed declaration is here
   22 |     double val, lazy;
      |            ^~~
horses.cpp:27:5: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   27 |     {
      |     ^
horses.cpp:17:5: note: shadowed declaration is here
   17 | int x[MAXN], y[MAXN];
      |     ^
horses.cpp: In constructor 'NodeInfo::NodeInfo(int, long double, int)':
horses.cpp:32:5: warning: declaration of 'val' shadows a member of 'NodeInfo' [-Wshadow]
   32 |     }
      |     ^
horses.cpp:22:12: note: shadowed declaration is here
   22 |     double val, lazy;
      |            ^~~
horses.cpp:32:5: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   32 |     }
      |     ^
horses.cpp:17:5: note: shadowed declaration is here
   17 | int x[MAXN], y[MAXN];
      |     ^
horses.cpp: In constructor 'NodeInfo::NodeInfo(int, long double, int)':
horses.cpp:32:5: warning: declaration of 'val' shadows a member of 'NodeInfo' [-Wshadow]
   32 |     }
      |     ^
horses.cpp:22:12: note: shadowed declaration is here
   22 |     double val, lazy;
      |            ^~~
horses.cpp:32:5: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   32 |     }
      |     ^
horses.cpp:17:5: note: shadowed declaration is here
   17 | int x[MAXN], y[MAXN];
      |     ^
horses.cpp: In constructor 'SegmentTree::SegmentTree(int, int)':
horses.cpp:57:5: warning: declaration of 'r' shadows a member of 'SegmentTree' [-Wshadow]
   57 |     {
      |     ^
horses.cpp:52:12: note: shadowed declaration is here
   52 |     int l, r;
      |            ^
horses.cpp:57:5: warning: declaration of 'l' shadows a member of 'SegmentTree' [-Wshadow]
   57 |     {
      |     ^
horses.cpp:52:9: note: shadowed declaration is here
   52 |     int l, r;
      |         ^
horses.cpp: In constructor 'SegmentTree::SegmentTree(int, int)':
horses.cpp:63:5: warning: declaration of 'r' shadows a member of 'SegmentTree' [-Wshadow]
   63 |     }
      |     ^
horses.cpp:52:12: note: shadowed declaration is here
   52 |     int l, r;
      |            ^
horses.cpp:63:5: warning: declaration of 'l' shadows a member of 'SegmentTree' [-Wshadow]
   63 |     }
      |     ^
horses.cpp:52:9: note: shadowed declaration is here
   52 |     int l, r;
      |         ^
horses.cpp: In constructor 'SegmentTree::SegmentTree(int, int)':
horses.cpp:63:5: warning: declaration of 'r' shadows a member of 'SegmentTree' [-Wshadow]
   63 |     }
      |     ^
horses.cpp:52:12: note: shadowed declaration is here
   52 |     int l, r;
      |            ^
horses.cpp:63:5: warning: declaration of 'l' shadows a member of 'SegmentTree' [-Wshadow]
   63 |     }
      |     ^
horses.cpp:52:9: note: shadowed declaration is here
   52 |     int l, r;
      |         ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:177:19: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  177 |     return evalInd(T->info.bestInd);
      |            ~~~~~~~^~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:188:19: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  188 |     return evalInd(T->info.bestInd);
      |            ~~~~~~~^~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:201:19: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  201 |     return evalInd(T->info.bestInd);
      |            ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 380 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 3 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 127096 KB Output is correct
2 Correct 571 ms 127224 KB Output is correct
3 Correct 501 ms 127100 KB Output is correct
4 Correct 671 ms 126968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 380 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 203 ms 125956 KB Output is correct
34 Correct 197 ms 126200 KB Output is correct
35 Correct 214 ms 126328 KB Output is correct
36 Correct 206 ms 126232 KB Output is correct
37 Correct 165 ms 126204 KB Output is correct
38 Correct 196 ms 126200 KB Output is correct
39 Correct 163 ms 126072 KB Output is correct
40 Correct 185 ms 126228 KB Output is correct
41 Correct 138 ms 126196 KB Output is correct
42 Correct 148 ms 126240 KB Output is correct
43 Correct 153 ms 126156 KB Output is correct
44 Correct 158 ms 131988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 380 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 3 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 2 ms 640 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 351 ms 127352 KB Output is correct
34 Correct 565 ms 127096 KB Output is correct
35 Correct 498 ms 126904 KB Output is correct
36 Correct 611 ms 126984 KB Output is correct
37 Correct 200 ms 126028 KB Output is correct
38 Correct 191 ms 126028 KB Output is correct
39 Correct 217 ms 126088 KB Output is correct
40 Correct 213 ms 126072 KB Output is correct
41 Correct 166 ms 126072 KB Output is correct
42 Correct 227 ms 126228 KB Output is correct
43 Correct 149 ms 125944 KB Output is correct
44 Correct 199 ms 126072 KB Output is correct
45 Correct 142 ms 126104 KB Output is correct
46 Correct 172 ms 126040 KB Output is correct
47 Correct 161 ms 125944 KB Output is correct
48 Correct 157 ms 131960 KB Output is correct
49 Correct 600 ms 131704 KB Output is correct
50 Correct 501 ms 131660 KB Output is correct
51 Correct 455 ms 138748 KB Output is correct
52 Correct 373 ms 138104 KB Output is correct
53 Correct 563 ms 130056 KB Output is correct
54 Correct 511 ms 130452 KB Output is correct
55 Correct 456 ms 128760 KB Output is correct
56 Correct 512 ms 133496 KB Output is correct
57 Correct 321 ms 129372 KB Output is correct
58 Correct 397 ms 129828 KB Output is correct
59 Correct 175 ms 131960 KB Output is correct