Submission #305669

# Submission time Handle Problem Language Result Execution time Memory
305669 2020-09-23T18:58:20 Z baluteshih Comparing Plants (IOI20_plants) C++14
100 / 100
1818 ms 148708 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define X first
#define Y second
#define ALL(v) v.begin(),v.end()
#define SZ(a) ((int)a.size())
#define pb push_back

const int INF=1e9;
struct node
{
    int pa;
    ll dis;
    node operator+(const node &a)const{
        return node{a.pa,dis+a.dis};
    }
}Rtr[200005][20],Ltr[200005][20];
pii seg3[1600005];
int seg1[800005],seg2[800005],lazy1[800005],lazy2[800005];
int layer[400005],pl[200005];

void build(int l,int r,int rt,const vector<int> &v,int *seg)
{
    if(l==r)
        return seg[rt]=v[l],void();
    int mid=(l+r)>>1;
    build(l,mid,rt<<1,v,seg),build(mid+1,r,rt<<1|1,v,seg);
    seg[rt]=min(seg[rt<<1],seg[rt<<1|1]);
}

void down(int rt,int *seg,int *lazy)
{
    if(lazy[rt])
    {
        seg[rt<<1]+=lazy[rt],lazy[rt<<1]+=lazy[rt];
        seg[rt<<1|1]+=lazy[rt],lazy[rt<<1|1]+=lazy[rt];
        lazy[rt]=0;
    }
}

void modify(int L,int R,int l,int r,int rt,int val,int *seg,int *lazy)
{
    if(l!=r) down(rt,seg,lazy);
    if(L<=l&&R>=r)
        return lazy[rt]+=val,seg[rt]+=val,void();
    int mid=(l+r)>>1;
    if(L<=mid)
        modify(L,R,l,mid,rt<<1,val,seg,lazy);
    if(R>mid)
        modify(L,R,mid+1,r,rt<<1|1,val,seg,lazy);
    seg[rt]=min(seg[rt<<1],seg[rt<<1|1]);
}

int query(int l,int r,int rt,int *seg,int *lazy)
{
    if(l!=r) down(rt,seg,lazy);
    if(l==r)
        return l;
    int mid=(l+r)>>1;
    if(seg[rt<<1]<=seg[rt<<1|1])
        return query(l,mid,rt<<1,seg,lazy);
    return query(mid+1,r,rt<<1|1,seg,lazy);
}

void build2(int l,int r,int rt)
{
    seg3[rt]=pii(INF,l);
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build2(l,mid,rt<<1),build2(mid+1,r,rt<<1|1);
}

void modify2(int x,int l,int r,int rt,int val)
{
    if(l==r)
        return seg3[rt]=pii(val,l),void();
    int mid=(l+r)>>1;
    if(x<=mid)
        modify2(x,l,mid,rt<<1,val);
    else
        modify2(x,mid+1,r,rt<<1|1,val);
    seg3[rt]=min(seg3[rt<<1],seg3[rt<<1|1]);
}

pii query2(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
        return seg3[rt];
    int mid=(l+r)>>1;
    if(R<=mid)
        return query2(L,R,l,mid,rt<<1);
    if(L>mid)
        return query2(L,R,mid+1,r,rt<<1|1);
    return min(query2(L,R,l,mid,rt<<1),query2(L,R,mid+1,r,rt<<1|1));
}

int arr[200005],K,n,L;

void init(int k, vector<int> r)
{
    n=SZ(r),K=k,L=__lg(n);
	build(0,n-1,1,r,seg1),build(0,n-1,1,vector<int>(n,INF),seg2);
    for(int i=0;i<n;++i)
    {
        while(seg1[1]==0)
        {
            int u=query(0,n-1,1,seg1,lazy1);
            modify(u,u,0,n-1,1,INF,seg1,lazy1),modify(u,u,0,n-1,1,-INF,seg2,lazy2);
            if(u+k<=n)
                modify(u+1,u+k-1,0,n-1,1,1,seg2,lazy2);
            else
            {
                if(u<n-1)
                    modify(u+1,n-1,0,n-1,1,1,seg2,lazy2);
                modify(0,k-n+u-1,0,n-1,1,1,seg2,lazy2);
            }
        }
        int u=query(0,n-1,1,seg2,lazy2);
        if(u>=k-1)
            modify(u-k+1,u,0,n-1,1,-1,seg1,lazy1);
        else
            modify(0,u,0,n-1,1,-1,seg1,lazy1),modify(n-(k-u-1),n-1,0,n-1,1,-1,seg1,lazy1);
        if(u+k<=n)
            modify(u+1,u+k-1,0,n-1,1,-1,seg2,lazy2);
        else
        {
            if(u<n-1)
                modify(u+1,n-1,0,n-1,1,-1,seg2,lazy2);
            modify(0,k-n+u-1,0,n-1,1,-1,seg2,lazy2);
        }
        modify(u,u,0,n-1,1,INF,seg2,lazy2),layer[u]=i,pl[i]=u;
    }
    for(int i=0;i<n;++i)
        layer[i+n]=layer[i];
    build2(0,2*n-1,1);
    for(int i=n-1;i>=0;--i)
    {
        int u=pl[i];
        pii tmp=query2(u+n-k+1,u+n,0,2*n-1,1);
        if(tmp.X==INF)
            Ltr[u][0]=node{u,0};
        else 
            Ltr[u][0]=node{tmp.Y%n,u+n-tmp.Y};
        tmp=query2(u,u+k-1,0,2*n-1,1);
        if(tmp.X==INF)
            Rtr[u][0]=node{u,0};
        else
            Rtr[u][0]=node{tmp.Y%n,tmp.Y-u};
        modify2(u,0,2*n-1,1,layer[u]),modify2(u+n,0,2*n-1,1,layer[u]);
    }
    for(int t=1;t<=L;++t)
        for(int i=0;i<n;++i)
            Ltr[i][t]=Ltr[i][t-1]+Ltr[Ltr[i][t-1].pa][t-1],Rtr[i][t]=Rtr[i][t-1]+Rtr[Rtr[i][t-1].pa][t-1];
}

int compare_plants(int x, int y)
{
    if(layer[x]==layer[y]) return 0;
    int rt=1,tar,nw;
    ll dis;
    if(layer[x]>layer[y])
        swap(x,y),rt=-1;
    if(x>y)
        tar=y+n;
    else
        tar=y;
    nw=x;
    if(nw+K>tar)
        return rt;
    dis=0;
    for(int i=L;i>=0;--i)
        if(x+dis+Rtr[nw][i].dis+K<=tar)
            dis+=Rtr[nw][i].dis,nw=Rtr[nw][i].pa;
    dis+=Rtr[nw][0].dis,nw=Rtr[nw][0].pa;
    if(layer[y]>layer[nw]&&x+dis+K>tar)
        return rt;
    nw=x;
    if(x<y)
        tar=y-n;
    else
        tar=y;
    if(nw-K<tar)
        return rt;
    dis=0;
    for(int i=L;i>=0;--i)
        if(x+dis-Ltr[nw][i].dis-K>=tar)
            dis-=Ltr[nw][i].dis,nw=Ltr[nw][i].pa;
    dis-=Ltr[nw][0].dis,nw=Ltr[nw][0].pa;
    if(layer[y]>layer[nw]&&x+dis-K<tar)
        return rt;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 70 ms 3320 KB Output is correct
7 Correct 212 ms 18140 KB Output is correct
8 Correct 852 ms 147328 KB Output is correct
9 Correct 865 ms 147276 KB Output is correct
10 Correct 887 ms 147304 KB Output is correct
11 Correct 908 ms 147304 KB Output is correct
12 Correct 909 ms 147300 KB Output is correct
13 Correct 971 ms 147300 KB Output is correct
14 Correct 1033 ms 147304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 5 ms 1152 KB Output is correct
7 Correct 97 ms 7032 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 5 ms 1152 KB Output is correct
10 Correct 96 ms 7032 KB Output is correct
11 Correct 106 ms 7032 KB Output is correct
12 Correct 97 ms 7068 KB Output is correct
13 Correct 92 ms 7032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 5 ms 1152 KB Output is correct
7 Correct 97 ms 7032 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 5 ms 1152 KB Output is correct
10 Correct 96 ms 7032 KB Output is correct
11 Correct 106 ms 7032 KB Output is correct
12 Correct 97 ms 7068 KB Output is correct
13 Correct 92 ms 7032 KB Output is correct
14 Correct 198 ms 18400 KB Output is correct
15 Correct 1780 ms 148368 KB Output is correct
16 Correct 213 ms 18268 KB Output is correct
17 Correct 1818 ms 148580 KB Output is correct
18 Correct 1138 ms 148324 KB Output is correct
19 Correct 1126 ms 148504 KB Output is correct
20 Correct 1591 ms 148332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 111 ms 4660 KB Output is correct
4 Correct 1111 ms 148200 KB Output is correct
5 Correct 1210 ms 148328 KB Output is correct
6 Correct 1487 ms 148324 KB Output is correct
7 Correct 1643 ms 148324 KB Output is correct
8 Correct 1744 ms 148580 KB Output is correct
9 Correct 1081 ms 148452 KB Output is correct
10 Correct 1110 ms 148452 KB Output is correct
11 Correct 990 ms 147304 KB Output is correct
12 Correct 1172 ms 148324 KB Output is correct
13 Correct 1219 ms 148580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 20 ms 1272 KB Output is correct
8 Correct 19 ms 1280 KB Output is correct
9 Correct 21 ms 1272 KB Output is correct
10 Correct 18 ms 1280 KB Output is correct
11 Correct 20 ms 1280 KB Output is correct
12 Correct 20 ms 1408 KB Output is correct
13 Correct 17 ms 1280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 4 ms 1152 KB Output is correct
6 Correct 1055 ms 148276 KB Output is correct
7 Correct 1369 ms 148448 KB Output is correct
8 Correct 1575 ms 148328 KB Output is correct
9 Correct 1743 ms 148580 KB Output is correct
10 Correct 984 ms 148584 KB Output is correct
11 Correct 1363 ms 148576 KB Output is correct
12 Correct 973 ms 148328 KB Output is correct
13 Correct 1120 ms 148452 KB Output is correct
14 Correct 1371 ms 148480 KB Output is correct
15 Correct 1650 ms 148708 KB Output is correct
16 Correct 964 ms 148324 KB Output is correct
17 Correct 1067 ms 148404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 70 ms 3320 KB Output is correct
7 Correct 212 ms 18140 KB Output is correct
8 Correct 852 ms 147328 KB Output is correct
9 Correct 865 ms 147276 KB Output is correct
10 Correct 887 ms 147304 KB Output is correct
11 Correct 908 ms 147304 KB Output is correct
12 Correct 909 ms 147300 KB Output is correct
13 Correct 971 ms 147300 KB Output is correct
14 Correct 1033 ms 147304 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 416 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 5 ms 1152 KB Output is correct
21 Correct 97 ms 7032 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 5 ms 1152 KB Output is correct
24 Correct 96 ms 7032 KB Output is correct
25 Correct 106 ms 7032 KB Output is correct
26 Correct 97 ms 7068 KB Output is correct
27 Correct 92 ms 7032 KB Output is correct
28 Correct 198 ms 18400 KB Output is correct
29 Correct 1780 ms 148368 KB Output is correct
30 Correct 213 ms 18268 KB Output is correct
31 Correct 1818 ms 148580 KB Output is correct
32 Correct 1138 ms 148324 KB Output is correct
33 Correct 1126 ms 148504 KB Output is correct
34 Correct 1591 ms 148332 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 0 ms 384 KB Output is correct
37 Correct 111 ms 4660 KB Output is correct
38 Correct 1111 ms 148200 KB Output is correct
39 Correct 1210 ms 148328 KB Output is correct
40 Correct 1487 ms 148324 KB Output is correct
41 Correct 1643 ms 148324 KB Output is correct
42 Correct 1744 ms 148580 KB Output is correct
43 Correct 1081 ms 148452 KB Output is correct
44 Correct 1110 ms 148452 KB Output is correct
45 Correct 990 ms 147304 KB Output is correct
46 Correct 1172 ms 148324 KB Output is correct
47 Correct 1219 ms 148580 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 0 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 20 ms 1272 KB Output is correct
55 Correct 19 ms 1280 KB Output is correct
56 Correct 21 ms 1272 KB Output is correct
57 Correct 18 ms 1280 KB Output is correct
58 Correct 20 ms 1280 KB Output is correct
59 Correct 20 ms 1408 KB Output is correct
60 Correct 17 ms 1280 KB Output is correct
61 Correct 93 ms 4600 KB Output is correct
62 Correct 192 ms 18140 KB Output is correct
63 Correct 960 ms 148028 KB Output is correct
64 Correct 1069 ms 148324 KB Output is correct
65 Correct 1476 ms 148324 KB Output is correct
66 Correct 1673 ms 148324 KB Output is correct
67 Correct 1699 ms 148584 KB Output is correct
68 Correct 1012 ms 148328 KB Output is correct
69 Correct 1438 ms 148472 KB Output is correct
70 Correct 1080 ms 148196 KB Output is correct
71 Correct 1224 ms 148400 KB Output is correct
72 Correct 1486 ms 148580 KB Output is correct
73 Correct 1656 ms 148328 KB Output is correct
74 Correct 957 ms 148328 KB Output is correct
75 Correct 1102 ms 148328 KB Output is correct