Submission #1125755

#TimeUsernameProblemLanguageResultExecution timeMemory
1125755dadas08간선 파괴 (GA5_destroy)C++20
0 / 100
340 ms2076 KiB
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> // #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); return 0; } using ll = int; using ull = unsigned long long; using ld = long double; using vi = std::vector<int>; using vvi = std::vector<vi>; using vl = std::vector<ll>; using vii = std::vector<pair<int, int> >; using vvl = std::vector<vl>; using vll = std::vector<pair<ll , ll> >; using vd = std::vector<double>; using vvd = std::vector<vd>; using vs = std::vector<std::string>; using vvs = std::vector<vs>; using vb = std::vector<bool>; using vvb = std::vector<vb>; using vc = std::vector<char>; using vvc = std::vector<vc>; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using piil = std::pair<pair<int, int>, ll>; using mii = std::map<int, int>; using mll = std::map<ll, ll>; using pql = std::priority_queue<ll>; using pqi = std::priority_queue<int>; using pqiil = std::priority_queue<pair<pair<int, int>, ll> >; using pqii = std::priority_queue<pair<int, int> >; #define pb push_back #define ps push #define eb emplace_back #define is insert #define er erase #define f first #define s second #define lb lower_bound #define ub upper_bound #define sf(i) sizeof(i) #define endl "\n" #define all(v) (v).begin(), (v).end() #define rep(i, L, R) for(ll i = L;i<=R;i++) #define pcis precision template<typename T> struct infinity { static constexpr T max=std::numeric_limits<T>::max(); static constexpr T min=std::numeric_limits<T>::min(); static constexpr T value=std::numeric_limits<T>::max()/2; static constexpr T mvalue=std::numeric_limits<T>::min()/2; }; template<typename T>constexpr T INF=infinity<T>::value; constexpr ll lINF=INF<ll>; constexpr int iINF = INF<int>; constexpr ld PI = 3.1415926535897932384626; struct UnionFindWithRollback { long long parent[721]; stack<tuple<int,int,int>> S; UnionFindWithRollback() { memset(parent, -1, sizeof(parent)); } int find(int x) { if(parent[x]<0) return x; return find(parent[x]); } bool uni(int x, int y) { x = find(x), y = find(y); if(x == y) return 0; if(parent[x] < parent[y]) swap(x,y); S.push({x, y, parent[x]}); parent[y] += parent[x]; parent[x] = y; return 1; } void rollback() { int x = get<0>(S.top()), y = get<1>(S.top()), sz = get<2>(S.top()); parent[y] -= sz; parent[x] = sz; S.pop(); } }; // 구간 합 구하기 기준 int sqrtN = 400; struct Query { int idx, s, e;// idx : 기존 쿼리 번호 / s~e까지의 쿼리 bool operator < (Query &x) { if(s/sqrtN != x.s/sqrtN) return s/sqrtN < x.s/sqrtN; return ((s/sqrtN)&1?-e : e) < ((x.s/sqrtN)&1?-x.e : x.e); } }; vector<Query> query; ll ans[51010]; void solve() { ll N, M; cin >>N >>M; pll EDGE[M+1]; for (ll i = 1; i<=M; i++) cin >> EDGE[i].f >> EDGE[i].s; UnionFindWithRollback ufLEFT; UnionFindWithRollback ufRIGHT; ll Q; cin >> Q; for(int i=0; i<Q; i++) { int s, e; cin >> s >> e; query.pb({i, s, e}); } sort(all(query));// 쿼리순 정렬 ll LEFTcomp = 0; bool A[M+1]; memset(A,0, sf(A)); int s = query[0].s, e = query[0].e; for(int i=1; i<s; i++) { auto [a,b] = EDGE[i]; A[i] = ufLEFT.uni(a, b); LEFTcomp += A[i]; } for(int i=e+1; i<=M; i++) { auto [a,b] = EDGE[i]; A[i] = ufRIGHT.uni(a, b); } bool TMP[N+1]; memset(TMP, 0, sf(TMP)); for (ll x = 1; x<=N; x++) { TMP[x] = ufLEFT.uni(ufRIGHT.find(x), x); LEFTcomp += TMP[x]; } ans[query[0].idx] = LEFTcomp; for (ll x = 1; x<=N; x++) { if (TMP[x]) ufLEFT.rollback(); LEFTcomp -= TMP[x]; } memset(TMP, 0, sf(TMP)); for(int i=1; i<Q; i++) { while(s > query[i].s) { // 왼쪽 늘림 s--; if (A[s]) ufLEFT.rollback(); LEFTcomp -= A[s]; A[s] = 0; } while(e < query[i].e) { e++; if (A[e]) ufRIGHT.rollback(); LEFTcomp -= A[e]; A[e] = 0; } while(s < query[i].s) { auto [a,b] = EDGE[s]; A[s] = ufLEFT.uni(a, b); LEFTcomp += A[s]; s++; } while(e > query[i].e) { auto [a,b] = EDGE[e]; A[e] = ufRIGHT.uni(a, b); e--; } for (ll x = 1; x<=N; x++) { TMP[x] = ufLEFT.uni(ufRIGHT.find(x), x); LEFTcomp += TMP[x]; } ans[query[i].idx] = LEFTcomp; for (ll x = 1; x<=N; x++) { if (TMP[x]) ufLEFT.rollback(); LEFTcomp -= TMP[x]; } memset(TMP, 0, sf(TMP)); } for(int i=0; i<Q; i++) cout << N - ans[i] << endl; return; } void _main() { solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...