Submission #473607

# Submission time Handle Problem Language Result Execution time Memory
473607 2021-09-15T17:17:11 Z Haidara Monkey and Apple-trees (IZhO12_apple) C++17
100 / 100
559 ms 235732 KB
/** * * * * * * * * * * * * * **\
 *                             *
 *   Author: Haidara Nassour   *
 * Coded in: YYYY\M\D HH:MM:SS *
 *          Lang: C++          *
 *                             *
\** * * * * * * * * * * * * * **/
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//#define int long long
#define itn int
#define rep(i,x,n) for(int i=(x);i<(n);i++)
#define FOR(i,n) rep(i,0,n)
#define per(i,x,n) for(int i=(x);i>(n);i--)
#define ROF(i,x) for(int i=x;i>=0;i--)
#define v(i) vector< i >
#define p(i,j) pair< i , j >
#define pii pair<int,int>
#define m(i,j) map< i , j >
#define um(i,j) unordered_map< i , j >
#define max_heap(i) priority_queue< i >
#define min_heap(i) priority_queue< i , vector< i > ,greater< i > >
#define ff first
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ss second
#define pp push_back
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
#define debug(x) cout<<#x<<" ";
using namespace std;
using namespace __gnu_pbds;
void SIO(string name="")
{
    if(name!="")
    {
        freopen((name+".in").c_str(),"r",stdin);
        freopen((name+".out").c_str(),"w",stdout);
    }
}
template <class T> using o_set = tree<T, null_type, less<T>,rb_tree_tag, tree_order_statistics_node_update>;
///order_of_key = find index of element x ( returned val is integer )
///find_by_order = find value at index x ( returned val is pointer )
const double pi=2.0*acos(0.0);
const int inf=1LL<<62LL;
const int mod=1e9+7;
const int maxn=10000001;
struct node
{
    int sum,lazy,lc,rc,l,r;
    node():sum(0),lazy(0),rc(-1),lc(-1),l(0),r(1e9) {}
} st[maxn];
int nodecnt=2;
void create(int inx,int child)
{
    int mid=(st[inx].l+st[inx].r)/2;
    if(child==1)
    {
        st[inx].lc=nodecnt++;
        st[st[inx].lc].l=st[inx].l;
        st[st[inx].lc].r=mid;
    }
    else
    {
        st[inx].rc=nodecnt++;
        st[st[inx].rc].l=mid+1;
        st[st[inx].rc].r=st[inx].r;
    }
}
void pull(int inx)
{
    if(st[inx].lazy)
    {
        st[inx].sum=st[inx].r-st[inx].l+1;
        if(st[inx].l!=st[inx].r)
        {
            int mid=(st[inx].l+st[inx].r)/2;
            if(st[inx].lc==-1)
                create(inx,1);
            if(st[inx].rc==-1)
                create(inx,2);
            st[st[inx].lc].lazy=1;
            st[st[inx].rc].lazy=1;
        }
    }
    st[inx].lazy=0;
}
int push_up(int inx)
{
    int res1=st[st[inx].lc].sum;
    int res2=st[st[inx].rc].sum;
    if(st[st[inx].lc].lazy)
        res1=(st[inx].l+st[inx].r)/2-st[inx].l+1;
    if(st[st[inx].rc].lazy)
        res2=st[inx].r-(st[inx].l+st[inx].r)/2;
    return res1+res2;
}
void update(int ul,int ur,int inx)
{
    pull(inx);
    if(ul==st[inx].l&&st[inx].r==ur)
    {
        st[inx].lazy=1;
        pull(inx);
        return ;
    }
    int mid=(st[inx].l+st[inx].r)/2;
    if(st[inx].lc==-1)
        create(inx,1);
    if(st[inx].rc==-1)
        create(inx,2);
    if(ul>mid)
        update(ul,ur,st[inx].rc);
    else if(ur<mid+1)
        update(ul,ur,st[inx].lc);
    else
    {
        update(ul,mid,st[inx].lc);
        update(mid+1,ur,st[inx].rc);
    }
    st[inx].sum=push_up(inx);
}
int query(int ql,int qr,int inx)
{
    pull(inx);
    if(ql==st[inx].l&&st[inx].r==qr)
        return st[inx].sum;
    int mid=(st[inx].l+st[inx].r)/2;
    if(st[inx].lc==-1)
        create(inx,1);
    if(st[inx].rc==-1)
        create(inx,2);
    int res=0;
    if(ql>mid)
        res=query(ql,qr,st[inx].rc);
    else if(qr<mid+1)
        res=query(ql,qr,st[inx].lc);
    else
        res=query(ql,mid,st[inx].lc)+query(mid+1,qr,st[inx].rc);
    st[inx].sum=push_up(inx);
    return res;
}
signed main()
{
    SIO("");
    fast;
    int q;
    cin>>q;
    int c=0;
    while(q--)
    {
        int t;
        cin>>t;
        if(t==1)
        {
            int j,k;
            cin>>j>>k;
            c=query(j+c,k+c,1);
            cout<<c<<endl;
        }
        else
        {
            int j,k;
            cin>>j>>k;
            update(j+c,k+c,1);
        }
    }
}

Compilation message

apple.cpp:48:18: warning: overflow in conversion from 'long long int' to 'int' changes value from '4611686018427387904' to '0' [-Woverflow]
   48 | const int inf=1LL<<62LL;
      |               ~~~^~~~~~
apple.cpp: In constructor 'node::node()':
apple.cpp:53:21: warning: 'node::rc' will be initialized after [-Wreorder]
   53 |     int sum,lazy,lc,rc,l,r;
      |                     ^~
apple.cpp:53:18: warning:   'int node::lc' [-Wreorder]
   53 |     int sum,lazy,lc,rc,l,r;
      |                  ^~
apple.cpp:54:5: warning:   when initialized here [-Wreorder]
   54 |     node():sum(0),lazy(0),rc(-1),lc(-1),l(0),r(1e9) {}
      |     ^~~~
apple.cpp: In function 'void pull(int)':
apple.cpp:80:17: warning: unused variable 'mid' [-Wunused-variable]
   80 |             int mid=(st[inx].l+st[inx].r)/2;
      |                 ^~~
apple.cpp: In function 'void SIO(std::string)':
apple.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen((name+".in").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen((name+".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 112 ms 235076 KB Output is correct
2 Correct 127 ms 235044 KB Output is correct
3 Correct 113 ms 235084 KB Output is correct
4 Correct 150 ms 235144 KB Output is correct
5 Correct 138 ms 235076 KB Output is correct
6 Correct 137 ms 235084 KB Output is correct
7 Correct 141 ms 235056 KB Output is correct
8 Correct 281 ms 235236 KB Output is correct
9 Correct 448 ms 235484 KB Output is correct
10 Correct 453 ms 235428 KB Output is correct
11 Correct 476 ms 235492 KB Output is correct
12 Correct 465 ms 235516 KB Output is correct
13 Correct 443 ms 235520 KB Output is correct
14 Correct 457 ms 235680 KB Output is correct
15 Correct 527 ms 235588 KB Output is correct
16 Correct 538 ms 235576 KB Output is correct
17 Correct 466 ms 235648 KB Output is correct
18 Correct 456 ms 235588 KB Output is correct
19 Correct 559 ms 235652 KB Output is correct
20 Correct 534 ms 235732 KB Output is correct