Submission #960158

# Submission time Handle Problem Language Result Execution time Memory
960158 2024-04-09T18:42:30 Z hariaakas646 Bridges (APIO19_bridges) C++14
0 / 100
226 ms 9176 KB
#include <bits/stdc++.h>

using namespace std;

#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;


void usaco()
{
    freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
//    freopen("problem.out", "w", stdout);
}

vi disset, sze, cnt, out;
stack<pii> stk;
int n, m;

int findf(int x) {
    while(x != disset[x]) {
        x = disset[x];
    }
    return x;
}

void unionf(int u, int v) {
    u = findf(u);
    v = findf(v);
    if(u == v) return;
    if(sze[u] > sze[v]) swap(u, v);
    disset[u] = v;
    sze[v] = max(sze[v], sze[u]+1);
    stk.push(mp(u, v));
    cnt[v] += cnt[u];
}

vector<pair<int, pii>> edge;

void process(vector<pair<pii, pii>> quer) {
    vb change(m);
    stk = {};
    vector<pair<pii, int>> calc;
    vector<pair<pii, int>> upd;
    vvi uped(m);
    disset = vi(n+1);
    forr(i, 1, n+1) disset[i] = i;
    sze = cnt= vi(n+1, 1);
    for(auto p : quer) {

        if(p.f.s == 1) {
            // printf("%d\n", p.s.s);
            change[p.s.s] = true;
            upd.pb(mp(p.s, p.f.f));
            uped[p.s.s].pb(p.f.f);
        }
        else calc.pb(mp(p.s, p.f.f));
    }
    // upd.pb(mp(mp(0, 0), 1e6));
    sort(all(calc), greater<>());

    vector<pair<int, pii>> st;
    frange(i, m) {
        auto p = edge[i];
        if(!change[i])
            st.pb(p);
    }
    // printf("upd:\n");
    // for(auto p1 : upd) {
        
    //     printf("%d %d\n", edge[p1.f.s].s.f, edge[p1.f.s].s.s);
    // }
    // printf("std:\n");
    // for(auto p1 : st) {
        
    //     printf("%d %d\n", p1.s.f, p1.s.s);
    // }
    int it = st.size();
    for(auto p : calc) {
        // printf("%d:\n", p.s);
        stk = {};
        frange(i, upd.size()) {
            auto p1 = upd[i];
            int v = edge[p1.f.s].f;
            auto it = lower_bound(all(uped[p1.f.s]), p1.s);
            if(p1.s < p.s) v = p1.f.f;
            else if(it > uped[p1.f.s].begin()) continue;
            if(next(it) != uped[p1.f.s].end() && *next(it) < p.s) continue;
            if(v >= p.f.f) {
                unionf(edge[p1.f.s].s.f, edge[p1.f.s].s.s);
                // printf("upd %d %d\n", edge[p1.f.s].s.f, edge[p1.f.s].s.s);
            }
        }
        auto it1 = it;
        while(it != 0 && st[it-1].f >= p.f.f) {
            it--;
            auto p1 = st[it-1];
            unionf(p1.s.f, p1.s.s);
            // printf("std %d %d\n", p1.s.f, p1.s.s);
            
        }
        out[p.s] = cnt[findf(p.f.s)];
        while(stk.size()) {
            auto p1 = stk.top();
            stk.pop();
            disset[p1.f] = p1.f;
            cnt[p1.s] -= cnt[p1.f];
        }

        
        it = it1;
        while(it != 0 && st[it-1].f >= p.f.f) {
            it--;
            auto p1 = st[it-1];
            unionf(p1.s.f, p1.s.s);
        }
    }

    for(auto p : upd) {
        edge[p.f.s].f = p.f.f;
    }

}

int main() {
    // usaco();
    scd(n);
    scd(m);
    edge = vector<pair<int, pii>>(m);

    frange(i, m) {
        scd(edge[i].s.f);
        scd(edge[i].s.s);
        scd(edge[i].f);
    }

    int q;
    scd(q);

    int sq = sqrt(q);

    vector<pair<pii, pii>> quer;
    out = vi(q, -1);
    frange(i, q) {
        pair<pii, pii> p;
        p.f.f = i;
        scd(p.f.s);
        scd(p.s.s);
        scd(p.s.f);
        if(p.f.s == 1) p.s.s--;
        quer.pb(p);
        if(quer.size() >= sq) {
            process(quer);
            quer = {};
        }
    }
    process(quer);

    for(auto e : out) if(e != -1) {printf("%d\n", e);}


}

Compilation message

bridges.cpp: In function 'void process(std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >)':
bridges.cpp:7:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define forr(i, j, k) for (int i = j; i < k; i++)
      |                                         ^
bridges.cpp:8:22: note: in expansion of macro 'forr'
    8 | #define frange(i, j) forr(i, 0, j)
      |                      ^~~~
bridges.cpp:100:9: note: in expansion of macro 'frange'
  100 |         frange(i, upd.size()) {
      |         ^~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:170:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  170 |         if(quer.size() >= sq) {
      |            ~~~~~~~~~~~~^~~~~
bridges.cpp: In function 'void usaco()':
bridges.cpp:30:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:145:5: note: in expansion of macro 'scd'
  145 |     scd(n);
      |     ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:146:5: note: in expansion of macro 'scd'
  146 |     scd(m);
      |     ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:150:9: note: in expansion of macro 'scd'
  150 |         scd(edge[i].s.f);
      |         ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:151:9: note: in expansion of macro 'scd'
  151 |         scd(edge[i].s.s);
      |         ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:152:9: note: in expansion of macro 'scd'
  152 |         scd(edge[i].f);
      |         ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:156:5: note: in expansion of macro 'scd'
  156 |     scd(q);
      |     ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:165:9: note: in expansion of macro 'scd'
  165 |         scd(p.f.s);
      |         ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:166:9: note: in expansion of macro 'scd'
  166 |         scd(p.s.s);
      |         ^~~
bridges.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
bridges.cpp:167:9: note: in expansion of macro 'scd'
  167 |         scd(p.s.f);
      |         ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 225 ms 5220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 94 ms 6048 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 226 ms 9176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 225 ms 5220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -