Submission #629721

# Submission time Handle Problem Language Result Execution time Memory
629721 2022-08-15T00:09:49 Z peti1234 Radio Towers (IOI22_towers) C++17
46 / 100
1102 ms 40136 KB
#include <bits/stdc++.h>
 
using namespace std;
const int c=100005, sok=2e9+5;
int n, t[c], maxi, fixd;
bool tc1=1, fontos[c];
set<pair<int, int> > tc5;
set<int> pos;
set<pair<int, int> > dif;
int kul(int a, int b) {
    return abs(t[a]-t[b]);
}
 
 
 
vector<vector<pair<int, int>>> rmq_mini, rmq_maxi;
vector<int> rmq_log;
void rmq_init(vector<int> sz) {
    int n=sz.size();
    rmq_log.resize(n+1);
    for (int i=2; i<=n; i++) {
        rmq_log[i]=rmq_log[i/2]+1;
    }
    int po=1, db=1;
    while (po<=n) {
        po*=2, db++;
    }
    rmq_mini.resize(db);
    rmq_maxi.resize(db);
    for (int i=0; i<db; i++) {
        rmq_mini[i].resize(n);
        rmq_maxi[i].resize(n);
    }
    for (int i=0; i<n; i++) {
        rmq_mini[0][i]={sz[i], i};
        rmq_maxi[0][i]={sz[i], i};
    }
    for (int j=1; j<db; j++) {
        for (int i=0; i+(1<<j)<=n; i++) {
            int s=i+(1<<(j-1));
            rmq_mini[j][i]=min(rmq_mini[j-1][i], rmq_mini[j-1][s]);
            rmq_maxi[j][i]=max(rmq_maxi[j-1][i], rmq_maxi[j-1][s]);
        }
    }
}
pair<int, int> rmq_ask(int l, int r, int id) {
    int s=rmq_log[r-l+1], s2=r+1-(1<<s);
    if (!id) {
        return min(rmq_mini[s][l], rmq_mini[s][s2]);
    } else {
        return max(rmq_maxi[s][l], rmq_maxi[s][s2]);
    }
}
 
 
vector<int> spec;
 
 
int try_tc46(int l, int r, int d) {
    if (fixd!=0 && d!=fixd) return 0;
    if (fixd==0) {
        fixd=d;
 
        int ut=sok, utid=0, id=1;
        for (int i=1; i<=n+1; i++) {
            if (id==1) {
                if (t[i]>ut) utid=i;
                ut=max(ut, t[i]);
                if (ut-t[i]>=d) {
                    spec.push_back(utid);
                    utid=i;
                    ut=t[i];
                    id=0;
                }
            } else {
                if (t[i]<ut) utid=i;
                ut=min(ut, t[i]);
                if (t[i]-ut>=d) {
                    spec.push_back(utid);
                    utid=i;
                    ut=t[i];
                    id=1;
                }
            }
        }
 
        spec.push_back(n+1);
 
        /*cout<< "spec: ";
        for (auto x:spec) cout << x << " ";
        cout << "\n";*/
 
    }
 
    int kezd=-1, veg=-1;
    int si=spec.size(), lo=0, hi=si, mid;
    while (hi-lo>1) {
        mid=(hi+lo)/2;
        if (spec[mid]<l) lo=mid;
        else hi=mid;
    }
    kezd=hi;
    if (spec[kezd]>r) return 1;
    lo=0, hi=si;
    while (hi-lo>1) {
        mid=(hi+lo)/2;
        if (spec[mid]>r) hi=mid;
        else lo=mid;
    }
    veg=lo;
    //cout << "sikerult " << kezd << " " << veg << "\n";
    assert(kezd<=veg);
    int ans=(veg+1)/2-(kezd/2);
    int kpos=spec[kezd], vpos=spec[veg];
    if (l<kpos && kezd%2==0 && rmq_ask(l, kpos-1, 0).first+d<=t[kpos]) ans++;
    if (vpos<r && veg%2==0 && rmq_ask(veg+1, r, 0).first+d<=t[vpos]) ans++;
 
    return ans;
}
void sub(int p) {
    auto it=pos.find(p);
    int el=0, kov=0;
    it++;
    kov=(*it);
    it--, it--;
    el=(*it);
    //cout << "valt " << el << " " << p << " " << kov << "\n";
    pos.erase(p);
    dif.erase({kul(p, kov), p});
    dif.erase({kul(el, p), el});
    dif.insert({kul(el, kov), el});
}
void calc_tc5() {
    fontos[0]=1, fontos[n+1]=1;
    int ut=sok, ans=0, id=1;
    for (int i=1; i<=n+1; i++) {
        if (id==1) {
            ut=max(ut, t[i]);
            if (ut>t[i]) {
                fontos[i-1]=1;
                ut=t[i];
                id=0;
                ans++;
            }
        } else {
            ut=min(ut, t[i]);
            if (t[i]>ut) {
                fontos[i-1]=1;
                ut=t[i];
                id=1;
            }
        }
    }
    int el=-1;
    for (int i=0; i<=n+1; i++) {
        if (!fontos[i]) continue;
        pos.insert(i);
        if (el!=-1) {
            dif.insert({kul(el, i), el});
            //cout << "fontos " << el << " " << i << "\n";
        }
        el=i;
    }
    tc5.insert({0, ans});
    while (ans>1) {
        auto it=dif.begin();
        int ert=(*it).first, p=(*it).second;
        auto it2=pos.upper_bound(p);
        int kov=*it2;
        //cout << "fontos " << p << " " << kov << "\n";
        sub(p), sub(kov);
        ans--;
        //cout << ert << " " << ans << "\n";
        it=tc5.upper_bound({ert, ans});
        if (it!=tc5.end()) tc5.erase(it);
        tc5.insert({ert, ans});
    }
}
void init(int N, vector<int> sz) {
    vector<int> sz2;
    sz2.push_back(sok);
    for (auto x:sz) sz2.push_back(x);
    sz2.push_back(sok);
    rmq_init(sz2);
    n=N;
    for (int i=0; i<n; i++) {
        t[i+1]=sz[i];
        if (t[i+1]>t[maxi]) maxi=i+1;
    }
    t[0]=sok, t[n+1]=sok;
    for (int i=1; i<=n; i++) {
        if (i<maxi && t[i]>t[i+1] || i>maxi && t[i]>t[i-1]) {
            tc1=0;
        }
    }
    calc_tc5();
}
 
int slow(int l, int r, int d) {
    int ut=t[l]+d, ans=0, id=1;
    for (int i=l; i<=r; i++) {
        if (id==1) {
            ut=max(ut, t[i]);
            if (ut-t[i]>=d) {
                ut=t[i];
                id=0;
                ans++;
            }
        } else {
            ut=min(ut, t[i]);
            if (t[i]-ut>=d) {
                ut=t[i];
                id=1;
            }
        }
    }
    return ans;
}
 
int max_towers(int l, int r, int d) {
    l++, r++;
    if (tc1) {
        if (l<=maxi && maxi<=r && max(t[l], t[r])+d<=t[maxi]) return 2;
        return 1;
    }
    if (l==1 && r==n) {
        auto it=tc5.upper_bound({d, 0});
        it--;
        return (*it).second;
    }
    int x=try_tc46(l, r, d);
    if (x) {
        return x;
    }
    return slow(l, r, d);
}
/*
int main()
{
    int N;
    vector<int> P;
    cin >> N;
    for (int i=0; i<N; i++) {
        int x;
        cin >> x;
        P.push_back(x);
    }
    init(N, P);
    int q;
    cin >> q;
    while (q--) {
        int l, r, d;
        cin >> l >> r >> d;
        cout << max_towers(l, r, d) << "\n";
    }
    return 0;
}
*/
/*
5
1 5 3 4 2
10
0 2 2
*/
/*
7
1 2 6 4 5 3 7
*/

Compilation message

towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:192:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  192 |         if (i<maxi && t[i]>t[i+1] || i>maxi && t[i]>t[i-1]) {
      |             ~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 430 ms 17496 KB Output is correct
2 Correct 775 ms 30656 KB Output is correct
3 Correct 937 ms 30644 KB Output is correct
4 Correct 761 ms 30656 KB Output is correct
5 Correct 702 ms 30552 KB Output is correct
6 Correct 814 ms 30656 KB Output is correct
7 Correct 812 ms 30748 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 848 KB Output is correct
3 Correct 2 ms 720 KB Output is correct
4 Correct 2 ms 848 KB Output is correct
5 Correct 3 ms 848 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
7 Correct 3 ms 848 KB Output is correct
8 Correct 1 ms 720 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 720 KB Output is correct
11 Correct 1 ms 720 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 720 KB Output is correct
14 Correct 1 ms 720 KB Output is correct
15 Correct 2 ms 848 KB Output is correct
16 Correct 2 ms 848 KB Output is correct
17 Correct 2 ms 848 KB Output is correct
18 Correct 1 ms 720 KB Output is correct
19 Correct 1 ms 720 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 4 ms 848 KB Output is correct
22 Correct 3 ms 848 KB Output is correct
23 Correct 1 ms 720 KB Output is correct
24 Correct 1 ms 720 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 2 ms 848 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
29 Correct 3 ms 848 KB Output is correct
30 Correct 2 ms 848 KB Output is correct
31 Correct 3 ms 848 KB Output is correct
32 Correct 1 ms 720 KB Output is correct
33 Correct 1 ms 720 KB Output is correct
34 Correct 1 ms 720 KB Output is correct
35 Correct 1 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 848 KB Output is correct
3 Correct 2 ms 720 KB Output is correct
4 Correct 2 ms 848 KB Output is correct
5 Correct 3 ms 848 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
7 Correct 3 ms 848 KB Output is correct
8 Correct 1 ms 720 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 720 KB Output is correct
11 Correct 1 ms 720 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 720 KB Output is correct
14 Correct 1 ms 720 KB Output is correct
15 Correct 2 ms 848 KB Output is correct
16 Correct 2 ms 848 KB Output is correct
17 Correct 2 ms 848 KB Output is correct
18 Correct 1 ms 720 KB Output is correct
19 Correct 1 ms 720 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 4 ms 848 KB Output is correct
22 Correct 3 ms 848 KB Output is correct
23 Correct 1 ms 720 KB Output is correct
24 Correct 1 ms 720 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 2 ms 848 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
29 Correct 3 ms 848 KB Output is correct
30 Correct 2 ms 848 KB Output is correct
31 Correct 3 ms 848 KB Output is correct
32 Correct 1 ms 720 KB Output is correct
33 Correct 1 ms 720 KB Output is correct
34 Correct 1 ms 720 KB Output is correct
35 Correct 1 ms 720 KB Output is correct
36 Correct 85 ms 23080 KB Output is correct
37 Correct 138 ms 36788 KB Output is correct
38 Correct 153 ms 36840 KB Output is correct
39 Incorrect 230 ms 39988 KB 1st lines differ - on the 1st token, expected: '678', found: '679'
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 743 ms 36620 KB Output is correct
2 Correct 930 ms 36808 KB Output is correct
3 Correct 832 ms 36888 KB Output is correct
4 Correct 887 ms 40108 KB Output is correct
5 Correct 932 ms 40116 KB Output is correct
6 Correct 1016 ms 40136 KB Output is correct
7 Correct 813 ms 40132 KB Output is correct
8 Correct 700 ms 30656 KB Output is correct
9 Correct 712 ms 30588 KB Output is correct
10 Correct 706 ms 30660 KB Output is correct
11 Correct 832 ms 30624 KB Output is correct
12 Correct 878 ms 30656 KB Output is correct
13 Correct 843 ms 30656 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 720 KB Output is correct
16 Correct 1 ms 720 KB Output is correct
17 Correct 136 ms 36884 KB Output is correct
18 Correct 217 ms 40136 KB Output is correct
19 Correct 218 ms 40112 KB Output is correct
20 Correct 27 ms 30636 KB Output is correct
21 Correct 34 ms 30664 KB Output is correct
22 Correct 145 ms 37048 KB Output is correct
23 Correct 215 ms 40120 KB Output is correct
24 Correct 215 ms 40112 KB Output is correct
25 Correct 27 ms 30612 KB Output is correct
26 Correct 36 ms 30656 KB Output is correct
27 Correct 2 ms 848 KB Output is correct
28 Correct 2 ms 848 KB Output is correct
29 Correct 3 ms 848 KB Output is correct
30 Correct 1 ms 720 KB Output is correct
31 Correct 1 ms 720 KB Output is correct
32 Correct 2 ms 720 KB Output is correct
33 Correct 3 ms 848 KB Output is correct
34 Correct 3 ms 848 KB Output is correct
35 Correct 1 ms 720 KB Output is correct
36 Correct 1 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 8236 KB Output is correct
2 Correct 957 ms 36792 KB Output is correct
3 Correct 914 ms 37012 KB Output is correct
4 Correct 1079 ms 39964 KB Output is correct
5 Correct 847 ms 39992 KB Output is correct
6 Correct 1014 ms 40020 KB Output is correct
7 Correct 1102 ms 39992 KB Output is correct
8 Correct 768 ms 30664 KB Output is correct
9 Correct 781 ms 30620 KB Output is correct
10 Correct 814 ms 30616 KB Output is correct
11 Correct 662 ms 30652 KB Output is correct
12 Correct 136 ms 36884 KB Output is correct
13 Correct 219 ms 39916 KB Output is correct
14 Correct 209 ms 40084 KB Output is correct
15 Correct 27 ms 30624 KB Output is correct
16 Correct 28 ms 30620 KB Output is correct
17 Correct 131 ms 35608 KB Output is correct
18 Correct 143 ms 36900 KB Output is correct
19 Correct 142 ms 36812 KB Output is correct
20 Correct 222 ms 40024 KB Output is correct
21 Correct 217 ms 40120 KB Output is correct
22 Correct 217 ms 39980 KB Output is correct
23 Correct 219 ms 40120 KB Output is correct
24 Correct 28 ms 30648 KB Output is correct
25 Correct 30 ms 30616 KB Output is correct
26 Correct 27 ms 30628 KB Output is correct
27 Correct 27 ms 30664 KB Output is correct
28 Correct 2 ms 720 KB Output is correct
29 Correct 2 ms 848 KB Output is correct
30 Correct 2 ms 848 KB Output is correct
31 Correct 1 ms 748 KB Output is correct
32 Correct 1 ms 720 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 2 ms 848 KB Output is correct
35 Correct 2 ms 720 KB Output is correct
36 Correct 3 ms 848 KB Output is correct
37 Correct 2 ms 848 KB Output is correct
38 Correct 3 ms 848 KB Output is correct
39 Correct 2 ms 848 KB Output is correct
40 Correct 1 ms 720 KB Output is correct
41 Correct 1 ms 720 KB Output is correct
42 Correct 1 ms 720 KB Output is correct
43 Correct 1 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 848 KB Output is correct
3 Correct 2 ms 720 KB Output is correct
4 Correct 2 ms 848 KB Output is correct
5 Correct 3 ms 848 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
7 Correct 3 ms 848 KB Output is correct
8 Correct 1 ms 720 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 720 KB Output is correct
11 Correct 1 ms 720 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 720 KB Output is correct
14 Correct 1 ms 720 KB Output is correct
15 Correct 2 ms 848 KB Output is correct
16 Correct 2 ms 848 KB Output is correct
17 Correct 2 ms 848 KB Output is correct
18 Correct 1 ms 720 KB Output is correct
19 Correct 1 ms 720 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
21 Correct 4 ms 848 KB Output is correct
22 Correct 3 ms 848 KB Output is correct
23 Correct 1 ms 720 KB Output is correct
24 Correct 1 ms 720 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 2 ms 848 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
29 Correct 3 ms 848 KB Output is correct
30 Correct 2 ms 848 KB Output is correct
31 Correct 3 ms 848 KB Output is correct
32 Correct 1 ms 720 KB Output is correct
33 Correct 1 ms 720 KB Output is correct
34 Correct 1 ms 720 KB Output is correct
35 Correct 1 ms 720 KB Output is correct
36 Correct 85 ms 23080 KB Output is correct
37 Correct 138 ms 36788 KB Output is correct
38 Correct 153 ms 36840 KB Output is correct
39 Incorrect 230 ms 39988 KB 1st lines differ - on the 1st token, expected: '678', found: '679'
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 430 ms 17496 KB Output is correct
2 Correct 775 ms 30656 KB Output is correct
3 Correct 937 ms 30644 KB Output is correct
4 Correct 761 ms 30656 KB Output is correct
5 Correct 702 ms 30552 KB Output is correct
6 Correct 814 ms 30656 KB Output is correct
7 Correct 812 ms 30748 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 720 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 2 ms 848 KB Output is correct
13 Correct 2 ms 720 KB Output is correct
14 Correct 2 ms 848 KB Output is correct
15 Correct 3 ms 848 KB Output is correct
16 Correct 3 ms 848 KB Output is correct
17 Correct 3 ms 848 KB Output is correct
18 Correct 1 ms 720 KB Output is correct
19 Correct 1 ms 720 KB Output is correct
20 Correct 1 ms 720 KB Output is correct
21 Correct 1 ms 720 KB Output is correct
22 Correct 0 ms 208 KB Output is correct
23 Correct 1 ms 720 KB Output is correct
24 Correct 1 ms 720 KB Output is correct
25 Correct 2 ms 848 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 2 ms 848 KB Output is correct
28 Correct 1 ms 720 KB Output is correct
29 Correct 1 ms 720 KB Output is correct
30 Correct 2 ms 848 KB Output is correct
31 Correct 4 ms 848 KB Output is correct
32 Correct 3 ms 848 KB Output is correct
33 Correct 1 ms 720 KB Output is correct
34 Correct 1 ms 720 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 2 ms 848 KB Output is correct
37 Correct 2 ms 848 KB Output is correct
38 Correct 3 ms 848 KB Output is correct
39 Correct 3 ms 848 KB Output is correct
40 Correct 2 ms 848 KB Output is correct
41 Correct 3 ms 848 KB Output is correct
42 Correct 1 ms 720 KB Output is correct
43 Correct 1 ms 720 KB Output is correct
44 Correct 1 ms 720 KB Output is correct
45 Correct 1 ms 720 KB Output is correct
46 Correct 85 ms 23080 KB Output is correct
47 Correct 138 ms 36788 KB Output is correct
48 Correct 153 ms 36840 KB Output is correct
49 Incorrect 230 ms 39988 KB 1st lines differ - on the 1st token, expected: '678', found: '679'
50 Halted 0 ms 0 KB -