Submission #349659

#TimeUsernameProblemLanguageResultExecution timeMemory
349659daniel920712Jousting tournament (IOI12_tournament)C++14
0 / 100
143 ms56812 KiB
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <vector>
#include <utility>
using namespace std;
int all[1000005];
struct A
{
    int l,r;
    int nxl,nxr;
    int con1;
    int con2;
    pair < long long , long long > ans;
    long long add;
    int big;
}Node[1000005];


int now=1;
int LL[100005];
int RR[100005];
void UPD(int here)
{
    Node[Node[here].nxl].add+=Node[here].add;
    Node[Node[here].nxr].add+=Node[here].add;
    Node[Node[here].nxl].ans.first+=Node[here].add;
    Node[Node[here].nxr].ans.first+=Node[here].add;
    Node[here].add=0;
}
pair < int , int > operator +(pair < int , int > a,pair < int , int > b)
{
    if(a.first>b.first) return a;
    if(a.first<b.first) return b;
    return make_pair(a.first,min(a.second,b.second));
}

void build(int l,int r,int here)
{
    Node[here].l=l;
    Node[here].r=r;
    Node[here].con1=(r-l+1);
    Node[here].con2=(r-l+1);
    Node[here].ans=make_pair(0,l);
    if(l==r)
    {
        Node[here].big=all[l];
        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].big=max(Node[Node[here].nxl].big,Node[Node[here].nxr].big);
}
int Find(int here,int con)
{
    //printf("%d %d %d %d\n",Node[here].l,Node[here].r,Node[here].con1,con);
    if(Node[here].l==Node[here].r) return Node[here].l;
    if(con<=Node[Node[here].nxl].con1) return Find(Node[here].nxl,con);
    else return Find(Node[here].nxr,con-Node[Node[here].nxl].con1);
}
int Find3(int here,int con)
{
    if(Node[here].l==Node[here].r) return Node[here].l;
    if(con<=Node[Node[here].nxl].con2) return Find3(Node[here].nxl,con);
    else return Find3(Node[here].nxr,con-Node[Node[here].nxl].con2);
}
int Find2(int l,int r,int here)
{
    if(Node[here].l==Node[here].r) return Node[here].big;
    if(r<=(Node[here].l+Node[here].r)/2) return Find2(l,r,Node[here].nxl);
    else if(l>(Node[here].l+Node[here].r)/2) return Find2(l,r,Node[here].nxr);
    else return max(Find2(l,(Node[here].l+Node[here].r)/2,Node[here].nxl),Find2((Node[here].l+Node[here].r)/2+1,r,Node[here].nxr));
}
void cha(int where,int here)
{
    Node[here].con1--;
    if(Node[here].l==Node[here].r) return;
    if(where<=(Node[here].l+Node[here].r)/2) cha(where,Node[here].nxl);
    else cha(where,Node[here].nxr);
}
void cha3(int where,int here)
{
    Node[here].con2--;
    if(Node[here].l==Node[here].r) return;
    if(where<=(Node[here].l+Node[here].r)/2) cha3(where,Node[here].nxl);
    else cha3(where,Node[here].nxr);
}
void cha2(int l,int r,long long con,int here)
{
    //printf("%d %d %d %d\n",l,r,Node[here].l,Node[here].r);
    if(Node[here].l==l&&Node[here].r==r)
    {
        Node[here].add+=con;
        Node[here].ans.first+=con;
        return;
    }
    UPD(here);
    if(r<=(Node[here].l+Node[here].r)/2) cha2(l,r,con,Node[here].nxl);
    else if(l>(Node[here].l+Node[here].r)/2) cha2(l,r,con,Node[here].nxr);
    else
    {
        cha2(l,(Node[here].l+Node[here].r)/2,con,Node[here].nxl);
        cha2((Node[here].l+Node[here].r)/2+1,r,con,Node[here].nxr);
    }
    Node[here].ans=Node[Node[here].nxl].ans+Node[Node[here].nxr].ans;
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E)
{
    int who,i,j,k,tt,big,how=0;
    long long ans=0,where=0;
    //printf("aa\n");
    for(i=0;i<N-1;i++) all[i]=K[i];
    //printf("aaa\n");
    build(0,N-1,0);
    //printf("cc\n");
    for(i=0;i<C;i++)
    {
        //printf("aa %d\n",i);
        LL[i]=Find(0,S[i]+1);
        RR[i]=Find3(0,E[i]+1);
        for(j=S[i];j<E[i];j++)
        {
            //printf("%d\n",S[i]+1,Find(S[i]+1));
            //printf("bb %d %d %d\n",j,Find(S[i]+1,0),Find3(S[i]+1,0));
            cha(Find(0,E[i]-(S[i]-j)+1),0);
            cha3(Find3(0,S[i]+1),0);
        }
        //printf("bb %d\n",i);
        //printf("aa %d %d\n",LL[i],RR[i]);
    }
    //printf("aaaa\n");
    for(i=0;i<C;i++)
    {
        //printf("aa %d\n",i);
        if(Find2(LL[i],RR[i]-1,0)>=R) cha2(LL[i],RR[i],-1e9,0);
        //printf("bb %d\n",i);
        cha2(LL[i],RR[i],1,0);
        //printf("cc %d\n",i);
        if(Node[0].ans.first>ans)
        {
            ans=Node[0].ans.first;
            where=Node[0].ans.second;
        }
        else if(Node[0].ans.first==ans) where=min(where,Node[0].ans.second);
        //printf("%lld %lld\n",Node[0].ans.first,Node[0].ans.second);

    }
    return (int) where;
}

Compilation message (stderr)

tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:113:9: warning: unused variable 'who' [-Wunused-variable]
  113 |     int who,i,j,k,tt,big,how=0;
      |         ^~~
tournament.cpp:113:17: warning: unused variable 'k' [-Wunused-variable]
  113 |     int who,i,j,k,tt,big,how=0;
      |                 ^
tournament.cpp:113:19: warning: unused variable 'tt' [-Wunused-variable]
  113 |     int who,i,j,k,tt,big,how=0;
      |                   ^~
tournament.cpp:113:22: warning: unused variable 'big' [-Wunused-variable]
  113 |     int who,i,j,k,tt,big,how=0;
      |                      ^~~
tournament.cpp:113:26: warning: unused variable 'how' [-Wunused-variable]
  113 |     int who,i,j,k,tt,big,how=0;
      |                          ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...