Submission #1206885

#TimeUsernameProblemLanguageResultExecution timeMemory
1206885veplsnxElection Campaign (JOI15_election_campaign)C++20
0 / 100
33 ms7348 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define u128 unsigned __int128
#define i128 __int128
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define eb emplace_back
#define mt make_tuple
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define ff first
#define ss second

const ll inf = 9e18;
const int iinf = 2e9;
const int N = 1e5;
const ll MOD = 1e9 + 7;

class lazy_seg_tree
{
private:
    vector<ll> t, l;
    int n;
    void build(int v, int tl, int tr, vector<int>& a){
        if (tl == tr){
            t[v] = a[tl];
            return;
        }
        int tm = tl + tr >> 1;
        build(2 * v + 1, tl, tm, a);
        build(2 * v + 2, tm + 1, tr, a);
        t[v] = t[2 * v + 1] + t[2 * v + 2];
    }
    void push(int v, int tl, int tm, int tr){
        if (l[v] != -1){
            t[2 * v + 1] = 1ll * (tm - tl + 1) * l[v];
            t[2 * v + 2] = 1ll * (tr - tm) * l[v];
            l[2 * v + 1] = l[v];
            l[2 * v + 2] = l[v];
            l[v] = -1;
        }
    }
    void update(int v, int tl, int tr, int ql, int qr, int val){
        if (qr < tl || ql > tr){
            return;
        }
        if (ql <= tl && tr <= qr){
            t[v] = 1ll * (tr - tl + 1) * val;
            l[v] = val;
            return;
        }
        int tm = tl + tr >> 1;
        push(v, tl, tm, tr);
        update(2 * v + 1, tl, tm, ql, qr, val);
        update(2 * v + 2, tm + 1, tr, ql, qr, val);
        t[v] = t[2 * v + 1] + t[2 * v + 2];
    }
    ll get(int v, int tl, int tr, int ql, int qr){
        if (tl > qr || tr < ql){
            return 0ll;
        }
        if (ql <= tl && tr <= qr)
            return t[v];
        int tm = tl + tr >> 1;
        push(v, tl, tm, tr);
        return get(2 * v + 1, tl, tm, ql, qr) + get(2 * v + 2, tm + 1, tr, ql, qr);
    }
public:
    lazy_seg_tree(vector<int>& a, int size){
        n = size;
        t.resize(4 * n);
        l.assign(4 * n, -1);
        build(0, 0, n - 1, a);
    }
    void update(int l, int r, int val){
        update(0, 0, n - 1, l, r, val);
    }
    ll get(int l, int r){
        return get(0, 0, n - 1, l, r);
    }
};

vector<vector<int>> g;

void solution(){
    int n;
    cin >> n;
    g.resize(n + 1);
    bool case2 = true;
    for (int i = 0; i < n - 1; ++i){
        int x, y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
        case2 &= (abs(x - y) == 1);
    }
    int m;
    cin >> m;
    vector<pair<int, int>> v(m);
    vector<int> c(m);
    for (int i = 0; i < m; ++i){
        cin >> v[i].ff >> v[i].ss >> c[i];
        if (v[i].ff > v[i].ss)
            swap(v[i].ff, v[i].ss);
        case2 &= (c[i] == 1);
    }
    if (case2){
        sort(all(v), [](auto a, auto b){
            return (a.ss - a.ff + 1) < (b.ss - b.ff + 1);
        });/*
        cout << "v = ";
        for (int i = 0; i < m; ++i)
            cout << "{" << v[i].ff << ", " << v[i].ss << "} ";
        cout << "\nl, r : \n";*/
        vector<int> a(n + 1, 0);
        lazy_seg_tree seg(a, n + 1);
        int ans = 0;
        for (int i = 0; i < m; ++i){
            int l = v[i].ff, r = v[i].ss;
            ll sum = seg.get(l, r);
            if (sum == 0){
                seg.update(l, r, 1);
                ++ans;
            }
        }
        cout << ans;
    } else{

    }
}

signed main(/* Kurmankul Nurislam */){
    //freopen("fcolor.in", "r", stdin);
    //freopen("fcolor.out", "w", stdout);
    cin.tie(nullptr) -> sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while (t--){
        solution();
        //cout << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...