Submission #1283533

#TimeUsernameProblemLanguageResultExecution timeMemory
1283533lastgirlTourism (JOI23_tourism)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
// User: Kazuki_Will_Win_VOI_8703
using namespace std;
#define fi first
#define se second
#define pii pair<int, int>
#define ll long long
#define all(a) a.begin(), a.end()

const int mn = 2e5 + 5, lg = 20, inf = 1e5 + 5, mod = 1000000007;

int n, m, q, on[mn], up[mn][lg], heavy[mn], d[mn], st[mn], ft[mn], euler[mn], timer_dfs = 0, ans[mn], B;
vector <int> a[mn];

void pre_dfs(int u, int p){
    st[u] = ++ timer_dfs;
    d[u] = d[p] + 1;
    euler[timer_dfs] = u;
    for(auto v : a[u]){
        if(v == p) continue;
        pre_dfs(v, u);
        euler[++ timer_dfs] = u;
    }
    ft[u] = timer_dfs;
}

int higher(int u, int v){
    if(d[u] < d[v]) return u;
    return v;
}

void build(){
    for(int i = 1; i <= timer_dfs; i++) up[i][0] = euler[i];
    for(int j = 1; j <= 17; j++){
        for(int i = 1; i + (1 << j) <= timer_dfs; i++){
            up[i][j] = higher(up[i][j - 1], up[i + (1 << j - 1)][j - 1]);
        }
    }
}

int lca(int u, int v){
    if(st[u] > st[v]) swap(u, v);
    int l = st[u], r = st[v], lg2 = __lg(r - l + 1);
    return higher(up[l][lg2], up[r - (1 << lg2) + 1][lg2]);
}

int kc(int u, int v){
    int p = lca(u, v);
    return d[u] + d[v] - 2 * d[p];
}

set <int> ms;
int cur = 0, freq[mn];

void add(int i){
    int u = on[i];
    freq[u] ++;
    if(freq[u] == 1){
        if(!ms.size()){
            ms.insert(st[u]);
        }
        else{
            int pre, nxt;
            auto it_nxt = ms.lower_bound(st[u]);
            if(it_nxt == ms.end()) nxt = *ms.begin();
            else nxt = *it_nxt;
            if(it_nxt == ms.begin()) pre = *ms.rbegin();
            else pre = *(--it_nxt);
            cur -= kc(euler[pre], euler[nxt]);
            cur += kc(euler[pre], u);
            cur += kc(euler[nxt], u);
            ms.insert(st[u]);
        }
    }
}

void del(int i){
    int u = on[i];
    freq[u] --;
    if(freq[u] == 0){
        if(ms.size() == 1){
            ms.erase(st[u]);
        }
        else{
            ms.erase(ms.find(st[u]));
            int pre, nxt;
            auto it_nxt = ms.lower_bound(st[u]);
            if(it_nxt == ms.end()) nxt = *ms.begin();
            else nxt = *it_nxt;
            if(it_nxt == ms.begin()) pre = *ms.rbegin();
            else pre = *(--it_nxt);
            cur += kc(euler[pre], euler[nxt]);
            cur -= kc(euler[pre], u);
            cur -= kc(euler[nxt], u);
        }
    }
}

struct Megumi{
    int l, r, id;

    bool operator< (const Megumi& other) const {
        if(l / B == other.l / B) return r < other.r;
        return l < other.l;
    }
} e[mn];

void solve(){
    cin >> n >> m >> q;
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    B = max(1, sqrt(m));
    pre_dfs(1, 0);
    build();
    for(int i = 1; i <= m; i++) cin >> on[i];

    for(int i = 1; i <= q; i++){
        cin >> e[i].l >> e[i].r;
        e[i].id = i;
    }

    sort(e + 1, e + q + 1);
    int ptrl = e[1].l, ptrr = e[1].r;

    for(int i = e[1].l; i <= e[1].r; i++){
        add(i);
    }
    // cerr << cur << '\n';
    ans[e[1].id] = cur / 2;

    for(int i = 2; i <= q; i++){
        while(ptrl < e[i].l) del(ptrl ++);
        while(ptrl > e[i].l) add(-- ptrl);
        while(ptrr < e[i].r) add(++ ptrr);
        while(ptrr > e[i].r) del(ptrr --);
        ans[e[i].id] = cur / 2;
        // cerr << e[i].id << ' ' << ans[e[i].id] << '\n';
    }

    for(int i = 1; i <= q; i++) cout << ans[i] + 1 << '\n';
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    int t = 1;
    // cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

tourism.cpp: In function 'void solve()':
tourism.cpp:115:12: error: no matching function for call to 'max(int, __gnu_cxx::__enable_if<true, double>::__type)'
  115 |     B = max(1, sqrt(m));
      |         ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:51,
                 from tourism.cpp:1:
/usr/include/c++/13/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/13/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
tourism.cpp:115:12: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__enable_if<true, double>::__type' {aka 'double'})
  115 |     B = max(1, sqrt(m));
      |         ~~~^~~~~~~~~~~~
/usr/include/c++/13/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/13/bits/stl_algobase.h:303:5: note:   template argument deduction/substitution failed:
tourism.cpp:115:12: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__enable_if<true, double>::__type' {aka 'double'})
  115 |     B = max(1, sqrt(m));
      |         ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61:
/usr/include/c++/13/bits/stl_algo.h:5795:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5795 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/13/bits/stl_algo.h:5795:5: note:   template argument deduction/substitution failed:
tourism.cpp:115:12: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  115 |     B = max(1, sqrt(m));
      |         ~~~^~~~~~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:5805:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5805 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/13/bits/stl_algo.h:5805:5: note:   template argument deduction/substitution failed:
tourism.cpp:115:12: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  115 |     B = max(1, sqrt(m));
      |         ~~~^~~~~~~~~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
tourism.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~