Submission #371175

#TimeUsernameProblemLanguageResultExecution timeMemory
371175daniel920712Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
64 / 100
3085 ms231028 KiB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;
struct A
{
    int l,r;
    int nxl,nxr;
    pair < int , int > ans;
    vector < int > all;
}Node[2000005];
int all[2000005];
int now=1;
pair < int , int > operator +(pair < int , int > a,pair < int , int > b)
{
    pair < int , int > ans;
    ans.first=max(a.first,b.first);
    ans.second=max(a.second,b.second);
    return ans;
}
void build(int l,int r,int here)
{
    int i;
    Node[here].l=l;
    Node[here].r=r;
    for(i=l;i<=r;i++) Node[here].all.push_back(all[i]);
    sort(Node[here].all.begin(),Node[here].all.end());
    if(l==r)
    {
        Node[here].ans=make_pair(all[l],0);
        return;
    }
    Node[here].nxl=now++;
    Node[here].nxr=now++;
    build(l,(l+r)/2,Node[here].nxl);
    build((l+r)/2+1,r,Node[here].nxr);
    Node[here].ans=Node[Node[here].nxl].ans+Node[Node[here].nxr].ans;
}

int Find2(int l,int r,int con,int here)
{
    int a,b;
    if(l==Node[here].l&&r==Node[here].r)
    {
        if(lower_bound(Node[here].all.begin(),Node[here].all.end(),con)==Node[here].all.begin()) return -1;
        return *prev(lower_bound(Node[here].all.begin(),Node[here].all.end(),con));
    }
    if(r<=(Node[here].l+Node[here].r)/2) return Find2(l,r,con,Node[here].nxl);
    else if(l>(Node[here].l+Node[here].r)/2) return Find2(l,r,con,Node[here].nxr);
    else
    {
        a=Find2(l,(Node[here].l+Node[here].r)/2,con,Node[here].nxl);
        b=Find2((Node[here].l+Node[here].r)/2+1,r,con,Node[here].nxr);
        return max(a,b);
    }

}

void build2(int here)
{
    if(Node[here].l==Node[here].r) return;
    build2(Node[here].nxl);
    build2(Node[here].nxr);
    pair < int , int > t,a,b;
    int c;
    a=Node[Node[here].nxl].ans;
    b=Node[Node[here].nxr].ans;
    c=Find2((Node[here].l+Node[here].r)/2+1,Node[here].r,a.first,0);
    t=a+b;
    if(a.first>c&&c!=-1) t.second=max(t.second,a.first+c);
    Node[here].ans=t;
}
pair < int , int > Find(int l,int r,int here)
{
    pair < int , int > t,a,b;
    int c;
    if(Node[here].l==l&&Node[here].r==r) return Node[here].ans;
    if(r<=(Node[here].l+Node[here].r)/2) return Find(l,r,Node[here].nxl);
    else if(l>(Node[here].l+Node[here].r)/2) return Find(l,r,Node[here].nxr);
    else
    {
        a=Find(l,(Node[here].l+Node[here].r)/2,Node[here].nxl);
        b=Find((Node[here].l+Node[here].r)/2+1,r,Node[here].nxr);
        t=a+b;
        c=Find2((Node[here].l+Node[here].r)/2+1,r,a.first,0);
        if(a.first>c&&c!=-1) t.second=max(t.second,a.first+c);

        return t;
    }
}
int main()
{
    int N,M,x,y,z,t,i,j,big;
    scanf("%d %d",&N,&M);

    for(i=1;i<=N;i++) scanf("%d",&all[i]);
    build(1,N,0);
    build2(0);
    while(M--)
    {
        scanf("%d %d %d",&x,&y,&z);
        if(Find(x,y,0).second<=z) printf("1\n");
        else printf("0\n");
    }

   return 0;
}

Compilation message (stderr)

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:97:19: warning: unused variable 't' [-Wunused-variable]
   97 |     int N,M,x,y,z,t,i,j,big;
      |                   ^
sortbooks.cpp:97:23: warning: unused variable 'j' [-Wunused-variable]
   97 |     int N,M,x,y,z,t,i,j,big;
      |                       ^
sortbooks.cpp:97:25: warning: unused variable 'big' [-Wunused-variable]
   97 |     int N,M,x,y,z,t,i,j,big;
      |                         ^~~
sortbooks.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   98 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:100:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  100 |     for(i=1;i<=N;i++) scanf("%d",&all[i]);
      |                       ~~~~~^~~~~~~~~~~~~~
sortbooks.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  105 |         scanf("%d %d %d",&x,&y,&z);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...