Submission #755283

# Submission time Handle Problem Language Result Execution time Memory
755283 2023-06-09T16:48:00 Z Rafi22 Comparing Plants (IOI20_plants) C++14
100 / 100
1710 ms 84236 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;

const int pot=1<<18,N=200007,L=18;

int n;

struct T
{
    pair<int,int> seg[2*pot+7];
    int lzy[2*pot+7];
    T()
    {
        memset(seg,0,sizeof seg);
        memset(lzy,0,sizeof lzy);
    }
    void push(int v)
    {
        lzy[2*v]+=lzy[v];
        lzy[2*v+1]+=lzy[v];
        seg[2*v].st+=lzy[v];
        seg[2*v+1].st+=lzy[v];
        lzy[v]=0;
    }
    void add(int v,int a,int b,int l,int r,int x)
    {
        if(a<=l&&b>=r)
        {
            lzy[v]+=x;
            seg[v].st+=x;
            return ;
        }
        if(r<a||l>b) return ;
        push(v);
        add(2*v,a,b,l,(l+r)/2,x);
        add(2*v+1,a,b,(l+r)/2+1,r,x);
        seg[v]=min(seg[2*v],seg[2*v+1]);
    }
};

vector<int>p;

int skokR[2*N][L];
int skokL[2*N][L];

void init(int k,vector<int>r)
{
    n=sz(r);
    p.resize(2*n+2);
    T R,ile;
    for(int i=0;i<n;i++) R.seg[i+pot]={r[i],i+1};
    for(int i=n+1;i<=pot;i++) R.seg[i+pot-1]={inf,i};
    for(int i=1;i<=pot;i++) ile.seg[i+pot-1]={inf,i};
    for(int i=pot-1;i>0;i--)
    {
        R.seg[i]=min(R.seg[2*i],R.seg[2*i+1]);
        ile.seg[i]=min(ile.seg[2*i],ile.seg[2*i+1]);
    }
    int it=0;
    while(it<n)
    {
        while(R.seg[1].st==0)
        {
            int i=R.seg[1].nd;
            R.add(1,i,i,1,pot,inf);
            ile.add(1,i,i,1,pot,-inf);
            if(i+k-1<=n) ile.add(1,i+1,i+k-1,1,pot,1);
            else
            {
                ile.add(1,i+1,n,1,pot,1);
                ile.add(1,1,i+k-1-n,1,pot,1);
            }
        }
        int i=ile.seg[1].nd;
        ile.add(1,i,i,1,pot,inf);
        p[i]=n-it++;
        if(i+k-1<=n) ile.add(1,i+1,i+k-1,1,pot,-1);
        else
        {
            ile.add(1,i+1,n,1,pot,-1);
            ile.add(1,1,i+k-1-n,1,pot,-1);
        }
        if(i-k+1>0) R.add(1,i-k+1,i-1,1,pot,-1);
        else
        {
            R.add(1,i-k+1+n,n,1,pot,-1);
            R.add(1,1,i-1,1,pot,-1);
        }
    }
    for(int i=n+1;i<=2*n;i++) p[i]=p[i-n];
    p[2*n+1]=inf;
    p[0]=inf;
    set<pair<int,int>>S;
    S.insert({inf,0});
    for(int i=1;i<=2*n;i++)
    {
        if(i-k>0) S.erase({p[i-k],i-k});
        pair<int,int>x=*S.upper_bound({p[i],i});
        if(x.st==inf) skokR[i][0]=i;
        else skokR[i][0]=x.nd;
        S.insert({p[i],i});
    }
    S.clear();
    S.insert({inf,0});
    for(int i=2*n;i>0;i--)
    {
        if(i+k<=2*n) S.erase({p[i+k],i+k});
        pair<int,int>x=*S.upper_bound({p[i],i});
        if(x.st==inf) skokL[i][0]=i;
        else skokL[i][0]=x.nd;
        S.insert({p[i],i});
    }
    for(int j=1;j<L;j++)
    {
        for(int i=1;i<=2*n;i++)
        {
            skokL[i][j]=skokL[skokL[i][j-1]][j-1];
            skokR[i][j]=skokR[skokR[i][j-1]][j-1];
        }
    }
  /*  for(int i=1;i<=2*n;i++) cout<<p[i]<<" ";
    cout<<endl;
    for(int i=1;i<=2*n;i++) cout<<skokL[i][0]<<" ";
    cout<<endl;
    for(int i=1;i<=2*n;i++) cout<<skokR[i][0]<<" ";
    cout<<endl;*/
}

int compare_plants(int x,int y)
{
    x++,y++;
    bool is=0;
    int f=1;
    if(p[x]<p[y])
    {
        swap(x,y);
        f=-1;
    }
    int Y=y;
    for(int j=L-1;j>=0;j--) if(p[skokL[Y][j]]<=p[x]) Y=skokL[Y][j];
    if(x>y&&Y>=x) is=1;
    if(x<y&&Y>=x+n) is=1;
    Y=y+n;
    for(int j=L-1;j>=0;j--) if(p[skokR[Y][j]]<=p[x]) Y=skokR[Y][j];
    if(x<y&&Y<=x+n) is=1;
    if(x>y&&Y<=x) is=1;
    if(is) return f;
    else return 0;
}

/*
int main()
{
    /*int n,k;
    cin>>n>>k;
    vector<int>q(n);
    for(int i=0;i<n;i++) q[i]=i+1;
    random_shuffle(all(q));
    vector<int>r(n,0);
    for(int i=0;i<n;i++)
    {
        for(int j=i;j<i+k;j++) if(q[i]<q[j%n]) r[i]++;
    }
    for(int i=0;i<n;i++) cout<<q[i]<<" ";
    cout<<endl;
    for(int i=0;i<n;i++) cout<<r[i]<<" ";
    cout<<endl;
    init(k,r);
    int Q=50;
    while(Q--)
    {
        cout<<compare_plants(rand()%n,rand()%n)<<endl;
    }
    return 0;
}
*/

Compilation message

plants.cpp:166:5: warning: "/*" within comment [-Wcomment]
  166 |     /*int n,k;
      |      
plants.cpp: In constructor 'T::T()':
plants.cpp:26:32: warning: 'void* memset(void*, int, size_t)' clearing an object of type 'struct std::pair<int, int>' with no trivial copy-assignment; use assignment instead [-Wclass-memaccess]
   26 |         memset(seg,0,sizeof seg);
      |                                ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/bits/char_traits.h:39,
                 from /usr/include/c++/10/ios:40,
                 from /usr/include/c++/10/istream:38,
                 from /usr/include/c++/10/sstream:38,
                 from /usr/include/c++/10/complex:45,
                 from /usr/include/c++/10/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
                 from plants.cpp:1:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<int, int>' declared here
  211 |     struct pair
      |            ^~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12588 KB Output is correct
2 Correct 9 ms 12616 KB Output is correct
3 Correct 8 ms 12628 KB Output is correct
4 Correct 8 ms 12612 KB Output is correct
5 Correct 8 ms 12616 KB Output is correct
6 Correct 93 ms 15624 KB Output is correct
7 Correct 169 ms 21572 KB Output is correct
8 Correct 609 ms 74720 KB Output is correct
9 Correct 607 ms 74708 KB Output is correct
10 Correct 687 ms 74716 KB Output is correct
11 Correct 663 ms 74700 KB Output is correct
12 Correct 653 ms 74700 KB Output is correct
13 Correct 747 ms 74708 KB Output is correct
14 Correct 709 ms 74708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12628 KB Output is correct
2 Correct 8 ms 12592 KB Output is correct
3 Correct 10 ms 12616 KB Output is correct
4 Correct 9 ms 12544 KB Output is correct
5 Correct 9 ms 12592 KB Output is correct
6 Correct 18 ms 12948 KB Output is correct
7 Correct 129 ms 17392 KB Output is correct
8 Correct 10 ms 12756 KB Output is correct
9 Correct 15 ms 13012 KB Output is correct
10 Correct 138 ms 17320 KB Output is correct
11 Correct 115 ms 17228 KB Output is correct
12 Correct 116 ms 17308 KB Output is correct
13 Correct 129 ms 17340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12628 KB Output is correct
2 Correct 8 ms 12592 KB Output is correct
3 Correct 10 ms 12616 KB Output is correct
4 Correct 9 ms 12544 KB Output is correct
5 Correct 9 ms 12592 KB Output is correct
6 Correct 18 ms 12948 KB Output is correct
7 Correct 129 ms 17392 KB Output is correct
8 Correct 10 ms 12756 KB Output is correct
9 Correct 15 ms 13012 KB Output is correct
10 Correct 138 ms 17320 KB Output is correct
11 Correct 115 ms 17228 KB Output is correct
12 Correct 116 ms 17308 KB Output is correct
13 Correct 129 ms 17340 KB Output is correct
14 Correct 200 ms 22112 KB Output is correct
15 Correct 1710 ms 80460 KB Output is correct
16 Correct 199 ms 22072 KB Output is correct
17 Correct 1617 ms 80576 KB Output is correct
18 Correct 1090 ms 79540 KB Output is correct
19 Correct 1115 ms 79420 KB Output is correct
20 Correct 1512 ms 84236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12628 KB Output is correct
2 Correct 8 ms 12628 KB Output is correct
3 Correct 110 ms 16248 KB Output is correct
4 Correct 805 ms 74672 KB Output is correct
5 Correct 919 ms 74676 KB Output is correct
6 Correct 1170 ms 74680 KB Output is correct
7 Correct 1330 ms 75188 KB Output is correct
8 Correct 1660 ms 79052 KB Output is correct
9 Correct 879 ms 74716 KB Output is correct
10 Correct 873 ms 74764 KB Output is correct
11 Correct 735 ms 74920 KB Output is correct
12 Correct 810 ms 74736 KB Output is correct
13 Correct 1096 ms 77460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12628 KB Output is correct
2 Correct 8 ms 12588 KB Output is correct
3 Correct 8 ms 12588 KB Output is correct
4 Correct 9 ms 12628 KB Output is correct
5 Correct 8 ms 12628 KB Output is correct
6 Correct 11 ms 12756 KB Output is correct
7 Correct 27 ms 13548 KB Output is correct
8 Correct 34 ms 13644 KB Output is correct
9 Correct 37 ms 13584 KB Output is correct
10 Correct 28 ms 13644 KB Output is correct
11 Correct 27 ms 13644 KB Output is correct
12 Correct 29 ms 13616 KB Output is correct
13 Correct 28 ms 13644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12520 KB Output is correct
2 Correct 8 ms 12628 KB Output is correct
3 Correct 8 ms 12628 KB Output is correct
4 Correct 9 ms 12628 KB Output is correct
5 Correct 12 ms 12884 KB Output is correct
6 Correct 788 ms 76684 KB Output is correct
7 Correct 1056 ms 76920 KB Output is correct
8 Correct 1352 ms 77484 KB Output is correct
9 Correct 1531 ms 81500 KB Output is correct
10 Correct 762 ms 76580 KB Output is correct
11 Correct 1128 ms 81000 KB Output is correct
12 Correct 768 ms 76468 KB Output is correct
13 Correct 832 ms 76676 KB Output is correct
14 Correct 1062 ms 76972 KB Output is correct
15 Correct 1248 ms 77504 KB Output is correct
16 Correct 736 ms 76512 KB Output is correct
17 Correct 791 ms 76644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12588 KB Output is correct
2 Correct 9 ms 12616 KB Output is correct
3 Correct 8 ms 12628 KB Output is correct
4 Correct 8 ms 12612 KB Output is correct
5 Correct 8 ms 12616 KB Output is correct
6 Correct 93 ms 15624 KB Output is correct
7 Correct 169 ms 21572 KB Output is correct
8 Correct 609 ms 74720 KB Output is correct
9 Correct 607 ms 74708 KB Output is correct
10 Correct 687 ms 74716 KB Output is correct
11 Correct 663 ms 74700 KB Output is correct
12 Correct 653 ms 74700 KB Output is correct
13 Correct 747 ms 74708 KB Output is correct
14 Correct 709 ms 74708 KB Output is correct
15 Correct 8 ms 12628 KB Output is correct
16 Correct 8 ms 12592 KB Output is correct
17 Correct 10 ms 12616 KB Output is correct
18 Correct 9 ms 12544 KB Output is correct
19 Correct 9 ms 12592 KB Output is correct
20 Correct 18 ms 12948 KB Output is correct
21 Correct 129 ms 17392 KB Output is correct
22 Correct 10 ms 12756 KB Output is correct
23 Correct 15 ms 13012 KB Output is correct
24 Correct 138 ms 17320 KB Output is correct
25 Correct 115 ms 17228 KB Output is correct
26 Correct 116 ms 17308 KB Output is correct
27 Correct 129 ms 17340 KB Output is correct
28 Correct 200 ms 22112 KB Output is correct
29 Correct 1710 ms 80460 KB Output is correct
30 Correct 199 ms 22072 KB Output is correct
31 Correct 1617 ms 80576 KB Output is correct
32 Correct 1090 ms 79540 KB Output is correct
33 Correct 1115 ms 79420 KB Output is correct
34 Correct 1512 ms 84236 KB Output is correct
35 Correct 8 ms 12628 KB Output is correct
36 Correct 8 ms 12628 KB Output is correct
37 Correct 110 ms 16248 KB Output is correct
38 Correct 805 ms 74672 KB Output is correct
39 Correct 919 ms 74676 KB Output is correct
40 Correct 1170 ms 74680 KB Output is correct
41 Correct 1330 ms 75188 KB Output is correct
42 Correct 1660 ms 79052 KB Output is correct
43 Correct 879 ms 74716 KB Output is correct
44 Correct 873 ms 74764 KB Output is correct
45 Correct 735 ms 74920 KB Output is correct
46 Correct 810 ms 74736 KB Output is correct
47 Correct 1096 ms 77460 KB Output is correct
48 Correct 7 ms 12628 KB Output is correct
49 Correct 8 ms 12588 KB Output is correct
50 Correct 8 ms 12588 KB Output is correct
51 Correct 9 ms 12628 KB Output is correct
52 Correct 8 ms 12628 KB Output is correct
53 Correct 11 ms 12756 KB Output is correct
54 Correct 27 ms 13548 KB Output is correct
55 Correct 34 ms 13644 KB Output is correct
56 Correct 37 ms 13584 KB Output is correct
57 Correct 28 ms 13644 KB Output is correct
58 Correct 27 ms 13644 KB Output is correct
59 Correct 29 ms 13616 KB Output is correct
60 Correct 28 ms 13644 KB Output is correct
61 Correct 120 ms 17716 KB Output is correct
62 Correct 170 ms 23464 KB Output is correct
63 Correct 721 ms 77424 KB Output is correct
64 Correct 858 ms 77544 KB Output is correct
65 Correct 1169 ms 77668 KB Output is correct
66 Correct 1261 ms 78468 KB Output is correct
67 Correct 1538 ms 82436 KB Output is correct
68 Correct 871 ms 77384 KB Output is correct
69 Correct 1248 ms 81788 KB Output is correct
70 Correct 835 ms 77340 KB Output is correct
71 Correct 924 ms 77544 KB Output is correct
72 Correct 1185 ms 77788 KB Output is correct
73 Correct 1304 ms 78412 KB Output is correct
74 Correct 734 ms 77588 KB Output is correct
75 Correct 907 ms 77568 KB Output is correct