Submission #20213

#TimeUsernameProblemLanguageResultExecution timeMemory
20213joonas두 섬간의 연결 (kriii1_2)C++98
1 / 1
44 ms8 KiB
#include <cstdio> typedef long long lld; int r[100001]; lld C[100001]; int find(int n){ if(n == r[n]) return n; return r[n] = find(r[n]); } int merge(int p, int q){ p = find(p); q = find(q); if(p != q){ r[q] = p; C[p] += C[q]; C[q] = 1; } return C[p]; } lld sum3(int n){ return n*(n-1LL)*(n+1)/6LL; } int main(){ int i, n, k; lld S[100001]={0}; lld pairs = 0, bridges = 0; scanf("%d", &n); for(i=1; i<=n; ++i){ r[i] = i, C[i] = 1; S[i] = S[i-1] + i; } for(i=1; i<n; ++i){ scanf("%d", &k); int k0 = find(k), k1 = find(k+1); pairs -= S[C[k0]] + S[C[k1]]; bridges -= sum3(C[k0]) + sum3(C[k1]); bridges += sum3(merge(k0, k1)); pairs += S[C[k0]] * S[C[k1]]; printf("%lld %lld\n", pairs, bridges); } return 0; }

Compilation message (stderr)

2.cpp: In function 'int main()':
2.cpp:32:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
2.cpp:38:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &k);
                        ^
#Verdict Execution timeMemoryGrader output
Fetching results...