Submission #304046

# Submission time Handle Problem Language Result Execution time Memory
304046 2020-09-21T01:44:48 Z daniel920712 Comparing Plants (IOI20_plants) C++14
5 / 100
4000 ms 107000 KB
#include "plants.h"
#include <stdio.h>
#include <vector>
#include <utility>
#include <set>
using namespace std;
vector < int > all;
vector < int > ttt;
vector < int > Next[200005];
vector < pair < int , int > > con[200005];
int N,tt=0;
bool use[2000005]={0};
int Con[200005];
int what[200005];
int now=1;
struct A
{
    int l,r;
    int nxl,nxr;
    int small1,small2;
    int small3;
    int add1,add2;
    int con=0,con1;
}Node[2000005];
void build(int l,int r,int here)
{
    Node[here].l=l;
    Node[here].r=r;
    Node[here].add1=0;
    Node[here].add2=0;
    Node[here].small1=0;
    Node[here].con=0;
    Node[here].con1=0;
    if(l==r)
    {
        Node[here].small2=all[l];
        Node[here].small3=all[l];
        Node[here].con1=1;
        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].small2=min(Node[Node[here].nxl].small2,Node[Node[here].nxr].small2);
    Node[here].small1=min(Node[Node[here].nxl].small1,Node[Node[here].nxr].small1);
    Node[here].con1=0;
    if(Node[Node[here].nxl].small2==Node[here].small2) Node[here].con1+=Node[Node[here].nxl].con1;
    if(Node[Node[here].nxr].small2==Node[here].small2) Node[here].con1+=Node[Node[here].nxr].con1;
}
void UPD(int here)
{
    Node[Node[here].nxl].add1+=Node[here].add1;
    Node[Node[here].nxl].small1+=Node[here].add1;
    Node[Node[here].nxr].add1+=Node[here].add1;
    Node[Node[here].nxr].small1+=Node[here].add1;
    Node[here].add1=0;

    Node[Node[here].nxl].add2+=Node[here].add2;
    Node[Node[here].nxl].small2+=Node[here].add2;
    Node[Node[here].nxr].add2+=Node[here].add2;
    Node[Node[here].nxr].small2+=Node[here].add2;
    Node[here].add2=0;
}
void cha1(int l,int r,int con,int here)
{
    if(l>r) return ;
    if(l==Node[here].l&&r==Node[here].r)
    {
        Node[here].add1+=con;
        Node[here].small1+=con;
        return;
    }
    UPD(here);
    if(r<=(Node[here].l+Node[here].r)/2) cha1(l,r,con,Node[here].nxl);
    else if(l>(Node[here].l+Node[here].r)/2) cha1(l,r,con,Node[here].nxr);
    else
    {
        cha1(l,(Node[here].l+Node[here].r)/2,con,Node[here].nxl);
        cha1((Node[here].l+Node[here].r)/2+1,r,con,Node[here].nxr);
    }
    Node[here].small3=min(Node[Node[here].nxl].small3,Node[Node[here].nxr].small3);
    Node[here].small2=min(Node[Node[here].nxl].small2,Node[Node[here].nxr].small2);
    Node[here].small1=min(Node[Node[here].nxl].small1,Node[Node[here].nxr].small1);
    Node[here].con1=0;
    if(Node[Node[here].nxl].small2==Node[here].small2) Node[here].con1+=Node[Node[here].nxl].con1;
    if(Node[Node[here].nxr].small2==Node[here].small2) Node[here].con1+=Node[Node[here].nxr].con1;
}

void cha2(int l,int r,int con,int here)
{
    if(l>r) return ;
    if(l==Node[here].l&&r==Node[here].r)
    {
        Node[here].add2+=con;
        Node[here].small2+=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].small2=min(Node[Node[here].nxl].small2,Node[Node[here].nxr].small2);
    Node[here].small1=min(Node[Node[here].nxl].small1,Node[Node[here].nxr].small1);
    Node[here].con1=0;
    if(Node[Node[here].nxl].small2==Node[here].small2) Node[here].con1+=Node[Node[here].nxl].con1;
    if(Node[Node[here].nxr].small2==Node[here].small2) Node[here].con1+=Node[Node[here].nxr].con1;
}
int Find1(int where,int here)
{
    if(Node[here].l==where&&Node[here].r==where) return Node[here].small1;
    UPD(here);
    if(where<=(Node[here].l+Node[here].r)/2) return Find1(where,Node[here].nxl);
    else return Find1(where,Node[here].nxr);
}
int Find2(int where,int here)
{
    if(Node[here].l==where&&Node[here].r==where) return Node[here].small2;
    UPD(here);
    if(where<=(Node[here].l+Node[here].r)/2) return Find2(where,Node[here].nxl);
    else return Find2(where,Node[here].nxr);
}

void New(int here)
{
    if(Node[here].l==Node[here].r)
    {
        ttt.push_back(Node[here].l);
        return;
    }
    UPD(here);
    if(Node[Node[here].nxl].small2==0) New(Node[here].nxl);
    if(Node[Node[here].nxr].small2==0) New(Node[here].nxr);

}
void init(int k,vector<int> r)
{
    int a=0,b=0,i,j,ok=0,l,t,t2,x,ll,rr;
    all=r;
    N=r.size();
    if(k==2) tt=1;
    for(i=0;i<N;i++)
    {
        if(k==2)
        {
            if(r[(i+N-1)%N]!=r[i])
            {
                b=0;
                a++;
                con[i].push_back(make_pair(a,0));
                for(j=(i+1)%N;r[(j+N-1)%N]==r[i];j=(j+1)%N)
                {
                    if(j==N-1) ok=1;
                    if(r[i]==0) b--;
                    else b++;
                    con[j].push_back(make_pair(a,b));
                }
            }
        }
        else
        {
            build(0,N-1,0);
            ttt.clear();
            New(0);
            for(auto t:ttt)
            {
                //printf("bb %d\n",t);
                cha2(t,t,1e9,0);
                t2=(t+k-1)%N;
                if(t2>=t) cha1(t+1,t2,1,0);
                else
                {
                    cha1(t+1,N-1,1,0);
                    cha1(0,t2,1,0);
                }
            }
            for(i=0;i<N;i++)
            {
                for(j=0;j<N;j++)
                {
                    if(use[j]==0&&Find1(j,0)==0&&Find2(j,0)>N)
                    {
                        x=j;
                        break;
                    }
                }
                //printf("aa %d\n",x);
                what[x]=i;
                use[x]=1;
                t=(x+k-1)%N;
                if(t>=x) cha1(x+1,t,-1,0);
                else
                {
                    cha1(x+1,N-1,-1,0);
                    cha1(0,t,-1,0);
                }

                ll=(x-(k-1)+N)%N;
                rr=(x-1+N)%N;
                //printf("%d %d\n",ll,rr);
                if(rr>=ll) cha2(ll,rr,-1,0);
                else
                {
                    cha2(ll,N-1,-1,0);
                    cha2(0,rr,-1,0);
                }
                //if(r[rr]==0) printf("cc %d\n",rr);

                ttt.clear();
                New(0);
                for(auto t:ttt)
                {
                    //printf("bb %d\n",t);
                    cha2(t,t,1e9,0);
                    t2=(t+k-1)%N;
                    if(t2>=t) cha1(t+1,t2,1,0);
                    else
                    {
                        cha1(t+1,N-1,1,0);
                        cha1(0,t2,1,0);
                    }
                }

            }
        }
    }
	return;
}
bool F(int A,int B)
{
    if(A==B) return 1;
    if(use[A]) return 0;
    use[A]=1;
    for(auto i:Next[A]) if(F(i,B)) return 1;
    return 0;
}
int compare_plants(int x, int y)
{
    int i;
    if(tt)
    {
        for(auto i:con[x])
        {
            for(auto j:con[y])
            {
                if(i.first==j.first)
                {
                    if(i.second<j.second) return -1;
                    return 1;
                }
            }
        }

    }
    else
    {
        if(what[x]<what[y]) return 1;
        else return -1;
    }

	return 0;
}

Compilation message

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:142:21: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
  142 |     int a=0,b=0,i,j,ok=0,l,t,t2,x,ll,rr;
      |                     ^~
plants.cpp:142:26: warning: unused variable 'l' [-Wunused-variable]
  142 |     int a=0,b=0,i,j,ok=0,l,t,t2,x,ll,rr;
      |                          ^
plants.cpp: In function 'int compare_plants(int, int)':
plants.cpp:243:9: warning: unused variable 'i' [-Wunused-variable]
  243 |     int i;
      |         ^
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:203:22: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  203 |                 rr=(x-1+N)%N;
      |                     ~^~
# Verdict Execution time Memory Grader output
1 Correct 52 ms 95740 KB Output is correct
2 Correct 52 ms 95736 KB Output is correct
3 Correct 51 ms 95736 KB Output is correct
4 Correct 52 ms 95736 KB Output is correct
5 Correct 54 ms 95864 KB Output is correct
6 Correct 120 ms 98604 KB Output is correct
7 Correct 139 ms 99448 KB Output is correct
8 Correct 189 ms 107000 KB Output is correct
9 Correct 191 ms 106744 KB Output is correct
10 Correct 183 ms 106744 KB Output is correct
11 Correct 191 ms 106744 KB Output is correct
12 Correct 184 ms 106744 KB Output is correct
13 Correct 184 ms 106904 KB Output is correct
14 Correct 183 ms 106812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 95736 KB Output is correct
2 Correct 52 ms 95736 KB Output is correct
3 Correct 51 ms 95736 KB Output is correct
4 Correct 51 ms 95768 KB Output is correct
5 Correct 52 ms 95864 KB Output is correct
6 Correct 122 ms 95864 KB Output is correct
7 Correct 2185 ms 98828 KB Output is correct
8 Correct 55 ms 95992 KB Output is correct
9 Correct 119 ms 96032 KB Output is correct
10 Correct 2119 ms 98936 KB Output is correct
11 Correct 146 ms 98808 KB Output is correct
12 Execution timed out 4074 ms 98296 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 95736 KB Output is correct
2 Correct 52 ms 95736 KB Output is correct
3 Correct 51 ms 95736 KB Output is correct
4 Correct 51 ms 95768 KB Output is correct
5 Correct 52 ms 95864 KB Output is correct
6 Correct 122 ms 95864 KB Output is correct
7 Correct 2185 ms 98828 KB Output is correct
8 Correct 55 ms 95992 KB Output is correct
9 Correct 119 ms 96032 KB Output is correct
10 Correct 2119 ms 98936 KB Output is correct
11 Correct 146 ms 98808 KB Output is correct
12 Execution timed out 4074 ms 98296 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 95736 KB Output is correct
2 Correct 51 ms 95864 KB Output is correct
3 Correct 313 ms 98656 KB Output is correct
4 Execution timed out 4035 ms 101160 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 95736 KB Output is correct
2 Correct 53 ms 95736 KB Output is correct
3 Correct 52 ms 95740 KB Output is correct
4 Correct 52 ms 95780 KB Output is correct
5 Incorrect 52 ms 95740 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 58 ms 95864 KB Output is correct
2 Correct 51 ms 95740 KB Output is correct
3 Correct 51 ms 95864 KB Output is correct
4 Incorrect 55 ms 95864 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 95740 KB Output is correct
2 Correct 52 ms 95736 KB Output is correct
3 Correct 51 ms 95736 KB Output is correct
4 Correct 52 ms 95736 KB Output is correct
5 Correct 54 ms 95864 KB Output is correct
6 Correct 120 ms 98604 KB Output is correct
7 Correct 139 ms 99448 KB Output is correct
8 Correct 189 ms 107000 KB Output is correct
9 Correct 191 ms 106744 KB Output is correct
10 Correct 183 ms 106744 KB Output is correct
11 Correct 191 ms 106744 KB Output is correct
12 Correct 184 ms 106744 KB Output is correct
13 Correct 184 ms 106904 KB Output is correct
14 Correct 183 ms 106812 KB Output is correct
15 Correct 52 ms 95736 KB Output is correct
16 Correct 52 ms 95736 KB Output is correct
17 Correct 51 ms 95736 KB Output is correct
18 Correct 51 ms 95768 KB Output is correct
19 Correct 52 ms 95864 KB Output is correct
20 Correct 122 ms 95864 KB Output is correct
21 Correct 2185 ms 98828 KB Output is correct
22 Correct 55 ms 95992 KB Output is correct
23 Correct 119 ms 96032 KB Output is correct
24 Correct 2119 ms 98936 KB Output is correct
25 Correct 146 ms 98808 KB Output is correct
26 Execution timed out 4074 ms 98296 KB Time limit exceeded
27 Halted 0 ms 0 KB -