Submission #348817

#TimeUsernameProblemLanguageResultExecution timeMemory
348817SprdaloHorses (IOI15_horses)C++17
0 / 100
175 ms81752 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

//Jako je vazno da uvek bude levo + desno dete
//Ako je potrebno drugacije (zasto?) menjati node operator+
template<int MAXN>
struct segtree{
    
    struct update{
        double x;

        update(double x = 0) : x(x){}

        update& operator+=(const update& other){
            x += other.x;
            return *this;
        }

        update operator+(const update& other){
            update tmp = *this;
            return tmp += other;
        }
    };

    struct node{
        double x;
        int l, r;
        update y;

        node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}

        node& operator+= (const node& other){
            x = max(x, other.x);
            return *this;
        }

        node& operator+ (const node& other){
            node tmp = *this;
            return tmp += other;
        }

        node& operator+= (const update& other){
            x += other.x * (r - l + 1);
            return *this;
        }

        node& operator+ (const update& other){
            node tmp = *this;
            tmp.r = other.r;
            return tmp += other;
        }
    };

    node a[2 * MAXN];
    update lazy[2 * MAXN];

    void init(vd t){
        int N = t.size();
        for (int i = 1; i <= MAXN; ++i){
            if (i <= N)
                a[i+MAXN-1] = node{t[i-1],i,i};
            else
                a[i+MAXN-1] = node{0,i,i};
        }
        for (int i = MAXN - 1; i > -1; --i){
            a[i] = a[2 * i] + a[2 * i + 1];
            a[i].l = a[2 * i].l;
            a[i].r = a[2 * i + 1].r;
        }
    }

    void push(int i){
        if (lazy[i].x == 0) return;

        a[i] += lazy[i];
        if (i < MAXN){
            lazy[2 * i] += lazy[i];
            lazy[2 * i + 1] += lazy[i];
        }

        lazy[i] = update{};
    }

    void add(int l, int r, update v, int pos = 1, int L = 1, int R = MAXN){
        push(pos);

        if (l > R || L > r) return;
        if (l <= L && R <= r){
            lazy[pos] += v;
            push(pos);
            return;
        }

        int mid = (L + R) / 2;
        add(l,r,v,2*pos,L,mid);
        add(l,r,v,2*pos+1,mid+1,R);

        a[pos] = a[pos * 2] + a[pos * 2 + 1];
    }

    node get(int l, int r, int pos = 1, int L = 1, int R = MAXN){
        push(pos);
        if (l > R || L > r) return node{};
        if (l <= L && R <= r) return a[pos];

        int mid = (L + R) / 2;
        return get(l,r,2*pos,L,mid) + get(l,r,2*pos+1,mid+1,R);
    }
};
segtree<131072*8> drvo;

const int mod = 1e9 + 7;

void f(ll& x){
    if (x >= mod)
        x %= mod;
}

ll g(ll x){
    f(x);
    return x;
}

vi X, Y;
int N;

int init(int n, int x[], int y[]){
    N = n; X = vi(n);Y=vi(n);
    vd t;
    ld c = 0;
    for (int i = 0; i < n; ++i){
        X[i]=x[i];
        Y[i] = y[i];

        c += log(1.0*x[i]) + log(1.0 * y[i]);
        t.push_back(c);
        c -= log(1.0*y[i]);
    }

    drvo.init(t);

    return round(exp(drvo.get(1,n).x));
}

int updateX(int pos, int val){
    
    ld c = -1 * log(X[pos]);
    X[pos] = val;
    c += log(val);

    drvo.add(pos+1, N, c);
    return round(exp(drvo.get(1,N).x));
}

int updateY(int pos, int val){
    ld c = -1*log(Y[pos]);
    Y[pos] = val;
    c += log(val);

    drvo.add(pos+1,pos+1,c);
    return round(exp(drvo.get(1,N).x));
}

int man()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    

    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) << '\n';

    int m;
    cin >> m;

    for (int i = 0; i < m; ++i){
        int type, pos, val;
        cin >> type >> pos >> val;

        if (type == 1)
            cout << updateX(pos, val) << '\n';
        else
            cout << updateY(pos, val) << '\n';
    }
}

Compilation message (stderr)

horses.cpp: In constructor 'segtree<MAXN>::update::update(double)':
horses.cpp:26:30: warning: declaration of 'x' shadows a member of 'segtree<MAXN>::update' [-Wshadow]
   26 |         update(double x = 0) : x(x){}
      |                              ^
horses.cpp:24:16: note: shadowed declaration is here
   24 |         double x;
      |                ^
horses.cpp: In constructor 'segtree<MAXN>::node::node(double, int, int)':
horses.cpp:44:50: warning: declaration of 'r' shadows a member of 'segtree<MAXN>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                  ^
horses.cpp:41:16: note: shadowed declaration is here
   41 |         int l, r;
      |                ^
horses.cpp:44:50: warning: declaration of 'l' shadows a member of 'segtree<MAXN>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                  ^
horses.cpp:41:13: note: shadowed declaration is here
   41 |         int l, r;
      |             ^
horses.cpp:44:50: warning: declaration of 'x' shadows a member of 'segtree<MAXN>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                  ^
horses.cpp:40:16: note: shadowed declaration is here
   40 |         double x;
      |                ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:150:21: warning: conversion from 'ld' {aka 'long double'} to 'std::vector<double>::value_type' {aka 'double'} may change value [-Wfloat-conversion]
  150 |         t.push_back(c);
      |                     ^
horses.cpp:156:17: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
  156 |     return round(exp(drvo.get(1,n).x));
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:165:24: warning: conversion from 'ld' {aka 'long double'} to 'double' may change value [-Wfloat-conversion]
  165 |     drvo.add(pos+1, N, c);
      |                        ^
horses.cpp:166:17: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
  166 |     return round(exp(drvo.get(1,N).x));
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:174:26: warning: conversion from 'ld' {aka 'long double'} to 'double' may change value [-Wfloat-conversion]
  174 |     drvo.add(pos+1,pos+1,c);
      |                          ^
horses.cpp:175:17: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
  175 |     return round(exp(drvo.get(1,N).x));
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int man()':
horses.cpp:210:1: warning: no return statement in function returning non-void [-Wreturn-type]
  210 | }
      | ^
horses.cpp: In instantiation of 'segtree<MAXN>::node::node(double, int, int) [with int MAXN = 1048576]':
horses.cpp:21:8:   required from here
horses.cpp:44:9: warning: declaration of 'r' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |         ^~~~
horses.cpp:41:16: note: shadowed declaration is here
   41 |         int l, r;
      |                ^
horses.cpp:44:9: warning: declaration of 'l' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |         ^~~~
horses.cpp:41:13: note: shadowed declaration is here
   41 |         int l, r;
      |             ^
horses.cpp:44:9: warning: declaration of 'x' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |         ^~~~
horses.cpp:40:16: note: shadowed declaration is here
   40 |         double x;
      |                ^
horses.cpp:44:70: warning: declaration of 'r' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                                      ^
horses.cpp:41:16: note: shadowed declaration is here
   41 |         int l, r;
      |                ^
horses.cpp:44:70: warning: declaration of 'l' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                                      ^
horses.cpp:41:13: note: shadowed declaration is here
   41 |         int l, r;
      |             ^
horses.cpp:44:70: warning: declaration of 'x' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                                      ^
horses.cpp:40:16: note: shadowed declaration is here
   40 |         double x;
      |                ^
horses.cpp:44:70: warning: declaration of 'r' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                                      ^
horses.cpp:41:16: note: shadowed declaration is here
   41 |         int l, r;
      |                ^
horses.cpp:44:70: warning: declaration of 'l' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                                      ^
horses.cpp:41:13: note: shadowed declaration is here
   41 |         int l, r;
      |             ^
horses.cpp:44:70: warning: declaration of 'x' shadows a member of 'segtree<1048576>::node' [-Wshadow]
   44 |         node(double x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}
      |                                                                      ^
horses.cpp:40:16: note: shadowed declaration is here
   40 |         double x;
      |                ^
horses.cpp: In instantiation of 'segtree<MAXN>::update::update(double) [with int MAXN = 1048576]':
horses.cpp:21:8:   required from here
horses.cpp:26:9: warning: declaration of 'x' shadows a member of 'segtree<1048576>::update' [-Wshadow]
   26 |         update(double x = 0) : x(x){}
      |         ^~~~~~
horses.cpp:24:16: note: shadowed declaration is here
   24 |         double x;
      |                ^
horses.cpp:26:37: warning: declaration of 'x' shadows a member of 'segtree<1048576>::update' [-Wshadow]
   26 |         update(double x = 0) : x(x){}
      |                                     ^
horses.cpp:24:16: note: shadowed declaration is here
   24 |         double x;
      |                ^
horses.cpp:26:37: warning: declaration of 'x' shadows a member of 'segtree<1048576>::update' [-Wshadow]
   26 |         update(double x = 0) : x(x){}
      |                                     ^
horses.cpp:24:16: note: shadowed declaration is here
   24 |         double x;
      |                ^
horses.cpp: In instantiation of 'void segtree<MAXN>::init(vd) [with int MAXN = 1048576; vd = std::vector<double>]':
horses.cpp:154:16:   required from here
horses.cpp:72:23: warning: conversion from 'std::vector<double>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   72 |         int N = t.size();
      |                 ~~~~~~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...