답안 #1046810

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046810 2024-08-07T01:54:49 Z nightfal 식물 비교 (IOI20_plants) C++17
100 / 100
1254 ms 268484 KB
// #include "plants.h"
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <utility>
#include <algorithm>


// using namespace std;
#define maxVal 1'000'000'001
static std:: vector<int> rank,lz,lzr;
static std:: vector<std::pair<int,int>> t,tr;
static std:: vector<std::vector<int>> g;
static std:: vector<std::vector<std::vector<int>>> power;

static int l,m;
void print(std::pair<int,int> p) {std::cout << "(" << p.first << "," << p.second << ") "; };
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;}
void print(std::vector<std::vector<std::vector<int>>> &v) {for (auto elem: v) print(elem); std::cout << std::endl;}

std::pair<int,int> update(std::vector<std::pair<int,int>> &t, std::vector<int> &lz, int node,int start,int end,int left,int right,int diff) {
    if (left <= start and end <= right) lz[node] += diff;
    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 ans1  = update(t,lz,2*node,start,mid,left,right,diff);
    auto ans2  = update(t,lz,2*node+1,mid+1,end,left,right,diff);
    auto ans = std::min(ans1,ans2);
    t[node] = std::min(t[2*node],t[2*node+1]);
    return ans;    
}
std::pair<int,int> findMin(std::vector<std::pair<int,int>> &t, std::vector<int> &lz, 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(t,lz,2*node,start,mid,left,right);
    auto min2 = findMin(t,lz,2*node+1,mid+1,end,left,right);
    auto ans = std::min(min1,min2);
    t[node] = std::min(t[2*node],t[2*node+1]);    
    return ans;
}
void segInit(std::vector<int> &r, std::vector<std::pair<int,int>> &t, int node, int start, int end) {
    if (start==end) {t[node]={r[start],start}; return;}
    int mid=(start+end)/2;
    segInit(r,t, 2*node,start,mid);
    segInit(r,t, 2*node+1,mid+1,end);
    t[node] = std::min(t[2*node],t[2*node+1]);
    return;
}
int findPreMinI(std::vector<std::pair<int,int>> &t, std::vector<int> &lz, int k,int minI1) {
    int ans = minI1;
    if (minI1>0) {
        auto [min2,minI2] = findMin(t,lz,1,0,m-1,std::max(0,minI1-k+1),minI1-1);
        if (min2==0) ans = minI2;
    }
    if (minI1-k+1<0) {
        auto [min2,minI2] = findMin(t,lz,1,0,m-1,minI1-k+1+m,m-1);
        if (min2==0) ans = minI2;
    }
    return ans;
}
int computeRank(std::vector<std::pair<int,int>> &t, std::vector<int> &lz, int minI1, int k, int ranking) {

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

    while (minI2 != minI1) {
        ranking = computeRank(t,lz,minI2,k,ranking); 
        minI2 = findPreMinI(t,lz,k,minI1);
    }
    rank[minI1] = ranking--;
    update(t,lz,1,0,m-1,minI1,minI1,m-1);
    if(minI1>0) update(t,lz,1,0,m-1,std::max(0,minI1-k+1),minI1-1,-1);
    if(minI1-k+1<0) update(t,lz,1,0,m-1,minI1-k+1+m,m-1,-1);

    return ranking;
}
bool inRangeNext(int z, int y, int next, int dir) {
    if(dir==0) return y<z && (z<next || next<=y) || z<y && (z<next && next<=y); // next==left
    if(dir==1)  return z<y && (next<z || y<=next) || y<z && (y<=next && next<z); // next==right
}
int reachable(int x, int y, int dir) {
    int next, i=0, z=x;
    while(true) {
        next = power[i][z][dir];
        if(next==-1) break;
        if(next==z || inRangeNext(z,y,next,dir)) break;
        i++;
    }
    for(int j=i-1; j>=0; j--) {
        next = power[j][z][dir];
        if(next==-1) continue;
        if(next==z || inRangeNext(z,y,next,dir)) continue;
        else z = next; 
    }
    next = power[0][z][dir];
    if (next!=-1 && inRangeNext(z,y,next,dir) && -rank[z]>-rank[y]) return 1;    
    return 0;
}
void calPower() {
    int x = m-1, cnt=1;
    while (x) {x>>=1; cnt++;}
    cnt++;
    power.resize(cnt,std::vector<std::vector<int>>(m,std::vector<int>(2,-1)));
    for(int j=0; j<m; j++)
        for(int x=0; x<2; x++)
            power[0][j][x] = g[j][x];
    
    for (int i=1; i<cnt; i++)
        for(int j=0; j<m; j++)
            for(int x=0; x<2; x++) {
                int  prev = power[i-1][j][x]; 
                int  &next = power[i][j][x];
                if (prev == -1 || prev == j) {next = prev; continue;}
                next = power[i-1][prev][x];
                if (next == -1 or next == j) continue;
                if(x==0 && inRangeNext(j,next,prev,0) || x==1 && inRangeNext(j,next,prev,1))
                // if(x==0 && inRangeLeft(j,next,prev) || x==1 && inRangeRight(j,next,prev))
                next = j;
            }
}
void calReachability(int k) {
    tr.resize(4*m); lzr.resize(4*m);
    std::vector<std::pair<int,int>> rankInv;
    for(int i=0; i<m; i++) {rank[i] = -rank[i]; rankInv.push_back({rank[i],i}); }
    sort(rankInv.begin(),rankInv.end());
    segInit(rank,tr,1,0,m-1);

    g.resize(m,std::vector<int>(2,-1));
    
    for(auto [val,i]: rankInv) {
        std::pair<int,int> temp1 ={maxVal,m+1}, temp2={maxVal,m+1};
        if (i>0) temp1 = findMin(tr,lzr,1,0,m-1,std::max(0,i-k+1),i-1);
        if (i-k+1<0) temp2 = findMin(tr,lzr,1,0,m-1,i-k+1+m,m-1);
        temp1 = std::min(temp1,temp2); 
        if (temp1.first <=0) g[i][0] = temp1.second;

        temp1 ={maxVal,m+1}, temp2={maxVal,m+1};
        if (i+1<m) temp1 = findMin(tr,lzr,1,0,m-1,i+1,std::min(m-1,i+k-1));
        if (i+k-1>m-1) temp2 = findMin(tr,lzr,1,0,m-1,0,i+k-1-m);
        temp1 = std::min(temp1,temp2);
        if (temp1.first <= 0) g[i][1] = temp1.second;
        update(tr,lzr,1,0,m-1,i,i,m);
    }
    calPower();
    return;
}
void init(int k, std::vector<int> r) {
    int n = r.size(); 
    m=n; l=k;
    t.resize(4*n); lz.resize(4*n); 
    segInit(r,t,1,0,n-1);
    rank.resize(n,n);
    int ranking = n-1;
    while (ranking >=0) {
        auto [min1,minI1] = findMin(t,lz,1,0,n-1,0,n-1);
        ranking = computeRank(t,lz,minI1,k,ranking);
    }
    calReachability(k);
	return;
}
int compare_plants(int x, int y) {
    int left = 0, right = 1, ans;
    ans = reachable(x,y,left) || reachable(x,y,right); if (ans) return ans;
    ans = reachable(y,x,left) || reachable(y,x,right); if (ans) return -ans;
    return 0;
}

Compilation message

plants.cpp: In function 'bool inRangeNext(int, int, int, int)':
plants.cpp:88:27: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   88 |     if(dir==0) return y<z && (z<next || next<=y) || z<y && (z<next && next<=y); // next==left
      |                       ~~~~^~~~~~~~~~~~~~~~~~~~~~
plants.cpp:89:28: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   89 |     if(dir==1)  return z<y && (next<z || y<=next) || y<z && (y<=next && next<z); // next==right
      |                        ~~~~^~~~~~~~~~~~~~~~~~~~~~
plants.cpp: In function 'void calPower()':
plants.cpp:126:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  126 |                 if(x==0 && inRangeNext(j,next,prev,0) || x==1 && inRangeNext(j,next,prev,1))
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plants.cpp: In function 'bool inRangeNext(int, int, int, int)':
plants.cpp:90:1: warning: control reaches end of non-void function [-Wreturn-type]
   90 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 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 34 ms 3164 KB Output is correct
7 Correct 126 ms 25816 KB Output is correct
8 Correct 493 ms 266432 KB Output is correct
9 Correct 564 ms 266460 KB Output is correct
10 Correct 681 ms 266432 KB Output is correct
11 Correct 783 ms 266588 KB Output is correct
12 Correct 845 ms 266436 KB Output is correct
13 Correct 888 ms 266548 KB Output is correct
14 Correct 1045 ms 266440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Correct 0 ms 348 KB Output is correct
6 Correct 3 ms 1372 KB Output is correct
7 Correct 52 ms 8492 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 3 ms 1372 KB Output is correct
10 Correct 54 ms 8292 KB Output is correct
11 Correct 78 ms 8280 KB Output is correct
12 Correct 122 ms 8528 KB Output is correct
13 Correct 55 ms 8424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Correct 0 ms 348 KB Output is correct
6 Correct 3 ms 1372 KB Output is correct
7 Correct 52 ms 8492 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 3 ms 1372 KB Output is correct
10 Correct 54 ms 8292 KB Output is correct
11 Correct 78 ms 8280 KB Output is correct
12 Correct 122 ms 8528 KB Output is correct
13 Correct 55 ms 8424 KB Output is correct
14 Correct 117 ms 25836 KB Output is correct
15 Correct 854 ms 266560 KB Output is correct
16 Correct 108 ms 25804 KB Output is correct
17 Correct 846 ms 266544 KB Output is correct
18 Correct 920 ms 266548 KB Output is correct
19 Correct 1160 ms 266432 KB Output is correct
20 Correct 757 ms 266436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 83 ms 4928 KB Output is correct
4 Correct 1113 ms 268140 KB Output is correct
5 Correct 1038 ms 266692 KB Output is correct
6 Correct 1037 ms 266556 KB Output is correct
7 Correct 960 ms 266548 KB Output is correct
8 Correct 846 ms 266544 KB Output is correct
9 Correct 939 ms 266692 KB Output is correct
10 Correct 1014 ms 266600 KB Output is correct
11 Correct 890 ms 266436 KB Output is correct
12 Correct 1111 ms 266436 KB Output is correct
13 Correct 995 ms 266432 KB Output is correct
# 결과 실행 시간 메모리 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 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 11 ms 1116 KB Output is correct
8 Correct 9 ms 1232 KB Output is correct
9 Correct 13 ms 1116 KB Output is correct
10 Correct 9 ms 1228 KB Output is correct
11 Correct 11 ms 1112 KB Output is correct
12 Correct 11 ms 1112 KB Output is correct
13 Correct 8 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 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 Correct 2 ms 1116 KB Output is correct
6 Correct 720 ms 266808 KB Output is correct
7 Correct 825 ms 266436 KB Output is correct
8 Correct 778 ms 266432 KB Output is correct
9 Correct 833 ms 266572 KB Output is correct
10 Correct 683 ms 266716 KB Output is correct
11 Correct 723 ms 266552 KB Output is correct
12 Correct 779 ms 268484 KB Output is correct
13 Correct 814 ms 266784 KB Output is correct
14 Correct 780 ms 266568 KB Output is correct
15 Correct 842 ms 266600 KB Output is correct
16 Correct 681 ms 266692 KB Output is correct
17 Correct 679 ms 266432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 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 34 ms 3164 KB Output is correct
7 Correct 126 ms 25816 KB Output is correct
8 Correct 493 ms 266432 KB Output is correct
9 Correct 564 ms 266460 KB Output is correct
10 Correct 681 ms 266432 KB Output is correct
11 Correct 783 ms 266588 KB Output is correct
12 Correct 845 ms 266436 KB Output is correct
13 Correct 888 ms 266548 KB Output is correct
14 Correct 1045 ms 266440 KB Output is correct
15 Correct 1 ms 348 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 3 ms 1372 KB Output is correct
21 Correct 52 ms 8492 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 3 ms 1372 KB Output is correct
24 Correct 54 ms 8292 KB Output is correct
25 Correct 78 ms 8280 KB Output is correct
26 Correct 122 ms 8528 KB Output is correct
27 Correct 55 ms 8424 KB Output is correct
28 Correct 117 ms 25836 KB Output is correct
29 Correct 854 ms 266560 KB Output is correct
30 Correct 108 ms 25804 KB Output is correct
31 Correct 846 ms 266544 KB Output is correct
32 Correct 920 ms 266548 KB Output is correct
33 Correct 1160 ms 266432 KB Output is correct
34 Correct 757 ms 266436 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 83 ms 4928 KB Output is correct
38 Correct 1113 ms 268140 KB Output is correct
39 Correct 1038 ms 266692 KB Output is correct
40 Correct 1037 ms 266556 KB Output is correct
41 Correct 960 ms 266548 KB Output is correct
42 Correct 846 ms 266544 KB Output is correct
43 Correct 939 ms 266692 KB Output is correct
44 Correct 1014 ms 266600 KB Output is correct
45 Correct 890 ms 266436 KB Output is correct
46 Correct 1111 ms 266436 KB Output is correct
47 Correct 995 ms 266432 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 11 ms 1116 KB Output is correct
55 Correct 9 ms 1232 KB Output is correct
56 Correct 13 ms 1116 KB Output is correct
57 Correct 9 ms 1228 KB Output is correct
58 Correct 11 ms 1112 KB Output is correct
59 Correct 11 ms 1112 KB Output is correct
60 Correct 8 ms 1116 KB Output is correct
61 Correct 60 ms 4904 KB Output is correct
62 Correct 112 ms 25808 KB Output is correct
63 Correct 605 ms 266604 KB Output is correct
64 Correct 771 ms 266436 KB Output is correct
65 Correct 982 ms 266432 KB Output is correct
66 Correct 882 ms 266432 KB Output is correct
67 Correct 817 ms 266436 KB Output is correct
68 Correct 810 ms 266692 KB Output is correct
69 Correct 919 ms 266692 KB Output is correct
70 Correct 1166 ms 268360 KB Output is correct
71 Correct 1254 ms 266692 KB Output is correct
72 Correct 1016 ms 266564 KB Output is correct
73 Correct 895 ms 266432 KB Output is correct
74 Correct 603 ms 266436 KB Output is correct
75 Correct 794 ms 266432 KB Output is correct