Submission #1166249

#TimeUsernameProblemLanguageResultExecution timeMemory
1166249HanksburgerGame (IOI13_game)C++20
63 / 100
2554 ms321536 KiB
#include "game.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
    node *leftchild1=0, *rightchild1=0, *leftchild2=0, *rightchild2=0;
    ll segtree=0;
} root;
ll large=1e9;
void update2(node& i, ll l1, ll r1, ll l2, ll r2, ll x, ll y, ll z)
{
    //cout << "update2 " << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << '\n';
    if (l2==r2)
    {
        if (l1==r1)
            i.segtree=z;
        else
            i.segtree=gcd(i.leftchild1==0?0:i.leftchild1->segtree, i.rightchild1==0?0:i.rightchild1->segtree);
        //cout << "segtree " << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << " = " << i.segtree << '\n';
        return;
    }
    ll mid=(l2+r2)/2;
    if (y<=mid)
    {
        if (i.leftchild2==0)
            i.leftchild2=new node;
        //cout << "here\n";
        if (i.leftchild1!=0)
            i.leftchild2->leftchild1=i.leftchild1->leftchild2;
        //cout << "here\n";
        if (i.rightchild1!=0)
            i.leftchild2->rightchild1=i.rightchild1->leftchild2;
        //cout << "here\n";
        update2(*i.leftchild2, l1, r1, l2, mid, x, y, z);
    }
    else
    {
        if (i.rightchild2==0)
            i.rightchild2=new node;
        if (i.leftchild1!=0)
            i.rightchild2->leftchild1=i.leftchild1->rightchild2;
        if (i.rightchild1!=0)
            i.rightchild2->rightchild1=i.rightchild1->rightchild2;
        update2(*i.rightchild2, l1, r1, mid+1, r2, x, y, z);
    }
    i.segtree=gcd(i.leftchild2==0?0:i.leftchild2->segtree, i.rightchild2==0?0:i.rightchild2->segtree);
    //cout << "segtree " << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << " = " << i.segtree << '\n';
}
void update1(node& i, ll l1, ll r1, ll x, ll y, ll z)
{
    //cout << "update1 " << l1 << ' ' << r1 << '\n';
    if (l1==r1)
    {
        update2(i, l1, r1, 0, large, x, y, z);
        return;
    }
    ll mid=(l1+r1)/2;
    if (x<=mid)
    {
        if (i.leftchild1==0)
            i.leftchild1=new node;
        update1(*i.leftchild1, l1, mid, x, y, z);
    }
    else
    {
        if (i.rightchild1==0)
            i.rightchild1=new node;
        update1(*i.rightchild1, mid+1, r1, x, y, z);
    }
    update2(i, l1, r1, 0, large, x, y, z);
}
ll query2(node& i, ll l1, ll r1, ll l2, ll r2, ll ql1, ll qr1, ll ql2, ll qr2)
{
    if (ql2<=l2 && r2<=qr2)
        return i.segtree;
    ll mid=(l2+r2)/2, res=0;
    if (l2<=qr2 && ql2<=mid && i.leftchild2!=0)
        res=gcd(res, query2(*i.leftchild2, l1, r1, l2, mid, ql1, qr1, ql2, qr2));
    if (mid<qr2 && ql2<=r2 && i.rightchild2!=0)
        res=gcd(res, query2(*i.rightchild2, l1, r1, mid+1, r2, ql1, qr1, ql2, qr2));
    return res;
}
ll query1(node& i, ll l1, ll r1, ll ql1, ll qr1, ll ql2, ll qr2)
{
    if (ql1<=l1 && r1<=qr1)
        return query2(i, l1, r1, 0, large, ql1, qr1, ql2, qr2);
    ll mid=(l1+r1)/2, res=0;
    if (l1<=qr1 && ql1<=mid && i.leftchild1!=0)
        res=gcd(res, query1(*i.leftchild1, l1, mid, ql1, qr1, ql2, qr2));
    if (mid<qr1 && ql1<=r1 && i.rightchild1!=0)
        res=gcd(res, query1(*i.rightchild1, mid+1, r1, ql1, qr1, ql2, qr2));
    return res;
}
void init(int R, int C) {}
void update(int P, int Q, ll K)
{
    update1(root, 0, large, P, Q, K);
}
ll calculate(int P, int Q, int U, int V)
{
    return query1(root, 0, large, P, U, Q, V);
}
#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...