Submission #1043262

# Submission time Handle Problem Language Result Execution time Memory
1043262 2024-08-04T06:54:50 Z nightfal Comparing Plants (IOI20_plants) C++14
30 / 100
129 ms 9820 KB
// #include "plants.h"
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <utility>
#include <tuple>



// using namespace std;
#define maxVal 1'000'000'001
static std:: vector<int> inc,dec,rank,lz,lzr,reach,reach2;
static std:: vector<std::pair<int,int>> t,tr;
static std:: vector<std::vector<int>> g, gt, path;
static int l,m,q2,subtask;
void print(std::vector<int> &v) {for(int elem: v) std::cout << elem << " "; std::cout << std::endl;}
void print(std::vector<std::vector<int>> &v) {for (auto elem: v) print(elem); std::cout << std::endl;}
void print(std::vector<std::pair<int,int>> &v) {for(auto [a,b]: v) std::cout << "(" << a << "," << b << ") ";std::cout << std::endl;}

std::pair<std::pair<int,int>,std::pair<int,int>>
update(int node,int start,int end,int left,int right,int diff) {
    if (left <= start and end <= right) {lz[node] += diff; lzr[node] += diff;}
    if (start!=end) {
        lz[2*node] += lz[node]; lz[2*node+1] += lz[node];
        lzr[2*node] += lzr[node]; lzr[2*node+1] += lzr[node];
    }
    t[node].first += lz[node]; lz[node]=0;
    tr[node].first += lzr[node]; lzr[node]=0;
    if (right < start or end < left) return {{maxVal,m+1},{maxVal,m+1}};
    if (left <= start and end <= right) return {t[node],tr[node]};

    int mid=(start+end)/2;
    auto [m1,mr1]  = update(2*node,start,mid,left,right,diff);
    auto [m2,mr2]  = update(2*node+1,mid+1,end,left,right,diff);
    auto ans1 = m1<m2? m1:m2;
    auto ans2 = mr1.first<mr2.first? mr1:mr2;
    t[node] = t[2*node]<t[2*node+1]? t[2*node]:t[2*node+1];    
    tr[node] = tr[2*node].first<tr[2*node+1].first? tr[2*node]:tr[2*node+1];    
    return {ans1,ans2};    
}
std::pair<int,int> findMinR(int node, int start, int end, int left, int right) {
    if (start!=end) {lzr[2*node] += lzr[node]; lzr[2*node+1] += lzr[node];}
    tr[node].first += lzr[node]; lzr[node]=0;
    if (right < start or end < left) return {maxVal,m+1};
    if (left <= start and end <= right) return tr[node];
    
    int mid=(start+end)/2;
    auto min1 = findMinR(2*node,start,mid,left,right);
    auto min2 = findMinR(2*node+1,mid+1,end,left,right);
    auto ans = min1.first<min2.first? min1:min2;
    tr[node] = tr[2*node].first<tr[2*node+1].first? tr[2*node]:tr[2*node+1];    
    return ans;
}
std::pair<int,int> findMin(int node, int start, int end, int left, int right) {
    if (start!=end) {lz[2*node] += lz[node]; lz[2*node+1] += lz[node];}
    t[node].first += lz[node]; lz[node]=0;
    if (right < start or end < left) return {maxVal,m+1};
    if (left <= start and end <= right) return t[node];
    
    int mid=(start+end)/2;
    auto min1 = findMin(2*node,start,mid,left,right);
    auto min2 = findMin(2*node+1,mid+1,end,left,right);
    auto ans = min1<min2? min1:min2;
    t[node] = t[2*node]<t[2*node+1]? t[2*node]:t[2*node+1];    
    return ans;
}
void segInitR(int node, int start, int end, std::vector<int> &r) {
    if (start==end) {tr[node]={r[start],start}; return;}
    int mid=(start+end)/2;
    segInitR(2*node,start,mid,r);
    segInitR(2*node+1,mid+1,end,r);
    tr[node] = tr[2*node].first<tr[2*node+1].first? tr[2*node]:tr[2*node+1];
    return;
}
void segInit(int node, int start, int end, std::vector<int> &r) {
    if (start==end) {t[node]={r[start],start}; return;}
    int mid=(start+end)/2;
    segInit(2*node,start,mid,r);
    segInit(2*node+1,mid+1,end,r);
    t[node] = t[2*node]<t[2*node+1]? t[2*node]:t[2*node+1];
    return;
}
int findPreMinI(int k,int minI1) {
    if (minI1>0) {
        auto[min2,minI2] = findMin(1,0,m-1,std::max(0,minI1-k+1),minI1-1);
        if (min2==0) minI1 = minI2;
    }
    if (minI1-k+1<0) {
        auto[min2,minI2] = findMin(1,0,m-1,minI1-k+1+m,m-1);
        if (min2==0) minI1 = minI2;
    }
    return minI1;
}
int computeRank(int minI1, int k, int ranking) {

    if (rank[minI1]!=m) return ranking;
    int minI2 = findPreMinI(k,minI1);

    while (minI2 != minI1) {
        ranking = computeRank(minI2,k,ranking); 
        minI2 = findPreMinI(k,minI1);
    }
    rank[minI1] = ranking--;
    if (subtask ==6) {
        std::pair<int,int> temp1, temp2;
        temp1 = temp2 = findMinR(1,0,m-1,std::max(0,minI1-k+1),minI1-1);
        if (minI1-k+1<0) temp2 = findMinR(1,0,m-1,minI1-k+1+m,m-1);
        if (temp1.first > temp2.first) temp1 = temp2;
        // else temp1 = std::min(temp1,temp2);
        if (rank[temp1.second]==m) {
            g[minI1][0] = temp1.second;
            gt[temp1.second].push_back(minI1);
        }

        temp1 = temp2 = findMinR(1,0,m-1,minI1+1,std::min(m-1,minI1+k-1));
        if (minI1+k-1>m-1) temp2 = findMinR(1,0,m-1,0,minI1+k-1-m);
        if (temp1.first >= temp2.first) temp1 = temp2;
        // temp1 = std::min(temp1,temp2);
        if (rank[temp1.second]==m) {
            g[minI1][1] = temp1.second;
            gt[temp1.second].push_back(minI1);
        }
        // std::cout << "minI1: " << minI1 << std::endl; print(g);
    }

    update(1,0,m-1,minI1,minI1,m-1);
    update(1,0,m-1,std::max(0,minI1-k+1),minI1-1,-1);
    if (minI1-k+1<0) update(1,0,m-1,minI1-k+1+m,m-1,-1);
    // if (ranking <= m) {
    // std::cout << "minI1: " << minI1 << std::endl;
    // std::cout << "g" << std:: endl; print(g); 
    // std::cout << "gt" << std:: endl; print(gt); 
    // std::cout << "t:  ";
    // for(int i=0; i<16; i++) std::cout << "(" << t[i].first << "," << t[i].second << ") "; std::cout << std::endl;
    // std::cout << "tr: "; 
    // for(int i=0; i<16; i++) std::cout << "(" << tr[i].first << "," << tr[i].second << ") "; std::cout << std::endl;
    // }
    return ranking;
}
void reachable(int node, std::vector<std::vector<int>> &g, std::vector<int> &reach) {
    if (node==-1 or reach[node]) return;
    reach[node] = 1;
    for(int elem: g[node])
        reachable(elem,g,reach);
    return;
}
void init6(int k, std::vector<int> &r) {
    int n = r.size(); 
    subtask = 6;
    t.resize(4*n); lz.resize(4*n); 
    tr.resize(4*n); lzr.resize(4*n); 
    g.resize(n,std::vector<int>(2,-1));
    gt.resize(n);
    reach.resize(n); reach2.resize(n);

    segInit(1,0,n-1,r);
    segInitR(1,0,n-1,r);
    // std::cout << "t: "; print(t); std::cout << std::endl;
    // std::cout << "tr: "; print(tr); std::cout << std::endl;
    rank.resize(n,n);
    int ranking = n-1;
    while (ranking >=0) {
        auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
        ranking = computeRank(minI1,k,ranking);
    }
    reachable(0,g,reach);
    reachable(0,gt,reach2);
    // std::cout << "g" << std:: endl; print(g); 
    // std::cout << "reach" << std:: endl; print(reach); 
    // std::cout << "gt" << std:: endl; print(gt); 
    // std::cout << "reach2" << std:: endl; print(reach2); 
    return;
}
void transClosure(int n, int k) {
    for (int i=0; i<n; i++)
        for(int j=i-k+1+n; j<=i+k-1+n; j++)
            if (rank[j%n]<rank[i%n]) g[i][j%n]=1;
    // print(g); std::cout << std::endl;
        for(int x=0; x<n; x++)
        for (int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                g[i][j] = g[i][j] or g[i][x] and g[x][j];
    // print(g); std::cout << std::endl;
    
    return;
}
void init5(int k, std::vector<int> &r) {
    int n = r.size(); 
    t.resize(4*n); lz.resize(4*n); g.resize(n,std::vector<int>(n));
    tr.resize(4*n); lzr.resize(4*n); 
    segInit(1,0,n-1,r);
    segInitR(1,0,n-1,r);
    rank.resize(n,n);
    int ranking = n-1;
    while (ranking >=0) {
        auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
        ranking = computeRank(minI1,k,ranking);
    }
    transClosure(n, k);
    return;    
}
void init4(int k, std::vector<int> &r) {
    int n = r.size(); 
    t.resize(4*n); lz.resize(4*n);g.resize(0);
    //  g.resize(0);
    segInit(1,0,n-1,r);
    rank.resize(n,n);
    int ranking = n-1;
    while (ranking >=0) {
        auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
        ranking = computeRank(minI1,k,ranking);
    }
    return;
}
void init3(int k, std::vector<int> &r) {
    int n = r.size(); 
    t.resize(4*n); lz.resize(4*n);
    segInit(1,0,n-1,r);
    rank.resize(n);
    for(int i=n-1; i>=0; i--) {
        auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
        int min2=1, minI2=minI1;
        int min3=1, minI3=minI1;
        if (minI1>0) std::tie(min2,minI2) = findMin(1,0,n-1,std::max(0,minI1-(n-k)),minI1-1);
        if (minI1-(n-k)<0) std::tie(min3,minI3) = findMin(1,0,n-1,minI1-(n-k)+n,n-1);
        if (min3==0) minI1=minI3;
        else if (min2==0) minI1=minI2;
        rank[minI1] = i;
        update(1,0,n-1,minI1,minI1,n-1);
        update(1,0,n-1,std::max(0,minI1-k+1),minI1-1,-1);
        if (minI1-k+1<0) update(1,0,n-1,minI1-k+1+n,n-1,-1);
    }
    return;
}
int findMax(int k, std::vector<int> &r) {
    int i=2*m-1;
    while (r[i%m]!=0) {i--;}
    int j = i-1;
    while (j > i-k) {
        if (r[j%m]==0) i = j; 
        j--;
    }
    for(int j=i; j>i-k; j--) r[j%m]--;
    return i%m;
}
void init2(int k, std::vector<int> &r) {
    int n = r.size();
    rank.resize(n);
    for(int i=n-1; i>=0; i--)
        rank[findMax(k,r)] = i;   
    return;
}
void init1(int k, std::vector<int> &r) {
    int n = r.size();
    inc.resize(n); dec.resize(n);
    for(int i=0; i<n; i++) {inc[i] = dec[i] = i;}
    int s,incVal,decVal;
    for(s=2*n-1; s>=0; s--) {
        if (r[s%n]==0) incVal = s;
        else decVal = s;
        inc[s%n] = incVal; dec[s%n] = decVal;
    }
    return;
}
void init(int k, std::vector<int> r) {
    int n = r.size(); 
    m=n; l=k;
    // std::cout << "here" << std::endl;
    // init6(k,r);
    // std::cout << "here2" << std::endl;

	if(k==2) init1(k, r);
    else if(n<=5000 and 2*k>n) init2(k, r);
    else if(2*k>n) init3(k,r);
    else if (n<=300 and q2 <= n*(n-1)/2) init5(k,r);
    else init4(k,r);
	return;
}
int compare_plants6(int x, int y) {
    if (reach[y]) return 1;
    else if (reach2[y]) return -1;
    else return 0;
}
int compare_plants5(int x, int y) {
    if (g[x][y]) return 1;
    else if (g[y][x]) return -1;
    else return 0;
}
int compare_plants2(int x, int y) {
    if (rank[x]>rank[y]) return 1;
    else return -1;
}
int compare_plants1(int x, int y) {
    if (y <= dec[x] or x+m <= inc[y]) return 1;
    else if (y <= inc[x] or x+m <= dec[y]) return -1;
	return 0;
}
int compare_plants(int x, int y) {
  	if(l==2) return compare_plants1(x,y);
    else if(m<=5000 and 2*l>m) return compare_plants2(x,y);
    else if(2*l>m) return compare_plants2(x,y);
    if (m<=300 and q2 <= m*(m-1)/2) return compare_plants5(x,y);
    // else return compare_plants6(x,y);
    else return compare_plants2(x,y);
	return 0;
}

Compilation message

plants.cpp: In function 'void print(std::vector<std::pair<int, int> >&)':
plants.cpp:19:58: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   19 | void print(std::vector<std::pair<int,int>> &v) {for(auto [a,b]: v) std::cout << "(" << a << "," << b << ") ";std::cout << std::endl;}
      |                                                          ^
plants.cpp: In function 'std::pair<std::pair<int, int>, std::pair<int, int> > update(int, int, int, int, int, int)':
plants.cpp:34:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |     auto [m1,mr1]  = update(2*node,start,mid,left,right,diff);
      |          ^
plants.cpp:35:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   35 |     auto [m2,mr2]  = update(2*node+1,mid+1,end,left,right,diff);
      |          ^
plants.cpp: In function 'int findPreMinI(int, int)':
plants.cpp:86:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |         auto[min2,minI2] = findMin(1,0,m-1,std::max(0,minI1-k+1),minI1-1);
      |             ^
plants.cpp:90:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |         auto[min2,minI2] = findMin(1,0,m-1,minI1-k+1+m,m-1);
      |             ^
plants.cpp: In function 'void init6(int, std::vector<int>&)':
plants.cpp:164:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  164 |         auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
      |              ^
plants.cpp: In function 'void transClosure(int, int)':
plants.cpp:183:46: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  183 |                 g[i][j] = g[i][j] or g[i][x] and g[x][j];
plants.cpp: In function 'void init5(int, std::vector<int>&)':
plants.cpp:197:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  197 |         auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
      |              ^
plants.cpp: In function 'void init4(int, std::vector<int>&)':
plants.cpp:211:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  211 |         auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
      |              ^
plants.cpp: In function 'void init3(int, std::vector<int>&)':
plants.cpp:222:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  222 |         auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
      |              ^
plants.cpp: In function 'void init1(int, std::vector<int>&)':
plants.cpp:262:37: warning: 'decVal' may be used uninitialized in this function [-Wmaybe-uninitialized]
  262 |         inc[s%n] = incVal; dec[s%n] = decVal;
plants.cpp:262:18: warning: 'incVal' may be used uninitialized in this function [-Wmaybe-uninitialized]
  262 |         inc[s%n] = incVal; dec[s%n] = decVal;
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 25 ms 4036 KB Output is correct
7 Correct 34 ms 5544 KB Output is correct
8 Correct 43 ms 8652 KB Output is correct
9 Correct 50 ms 8648 KB Output is correct
10 Correct 40 ms 8792 KB Output is correct
11 Correct 44 ms 8788 KB Output is correct
12 Correct 40 ms 8680 KB Output is correct
13 Correct 40 ms 9040 KB Output is correct
14 Correct 53 ms 8788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 544 KB Output is correct
7 Correct 114 ms 5204 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 5 ms 348 KB Output is correct
10 Correct 120 ms 5216 KB Output is correct
11 Correct 89 ms 4976 KB Output is correct
12 Correct 90 ms 5200 KB Output is correct
13 Correct 129 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 544 KB Output is correct
7 Correct 114 ms 5204 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 5 ms 348 KB Output is correct
10 Correct 120 ms 5216 KB Output is correct
11 Correct 89 ms 4976 KB Output is correct
12 Correct 90 ms 5200 KB Output is correct
13 Correct 129 ms 5200 KB Output is correct
14 Runtime error 24 ms 9820 KB Execution killed with signal 11
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 21 ms 7136 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 24 ms 1688 KB Output is correct
8 Correct 32 ms 1684 KB Output is correct
9 Correct 31 ms 1624 KB Output is correct
10 Correct 32 ms 1624 KB Output is correct
11 Correct 25 ms 1628 KB Output is correct
12 Correct 31 ms 1628 KB Output is correct
13 Correct 7 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Runtime error 1 ms 856 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 25 ms 4036 KB Output is correct
7 Correct 34 ms 5544 KB Output is correct
8 Correct 43 ms 8652 KB Output is correct
9 Correct 50 ms 8648 KB Output is correct
10 Correct 40 ms 8792 KB Output is correct
11 Correct 44 ms 8788 KB Output is correct
12 Correct 40 ms 8680 KB Output is correct
13 Correct 40 ms 9040 KB Output is correct
14 Correct 53 ms 8788 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 5 ms 544 KB Output is correct
21 Correct 114 ms 5204 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 5 ms 348 KB Output is correct
24 Correct 120 ms 5216 KB Output is correct
25 Correct 89 ms 4976 KB Output is correct
26 Correct 90 ms 5200 KB Output is correct
27 Correct 129 ms 5200 KB Output is correct
28 Runtime error 24 ms 9820 KB Execution killed with signal 11
29 Halted 0 ms 0 KB -