제출 #134435

#제출 시각아이디문제언어결과실행 시간메모리
134435dualityConstruction of Highway (JOI18_construction)C++11
100 / 100
733 ms22968 KiB
#define DEBUG 0 #include <bits/stdc++.h> using namespace std; #if DEBUG // basic debugging macros int __i__,__j__; #define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl #define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl #define printVar(n) cout<<#n<<": "<<n<<endl #define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl #define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;} #define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;} // advanced debugging class // debug 1,2,'A',"test"; class _Debug { public: template<typename T> _Debug& operator,(T val) { cout << val << endl; return *this; } }; #define debug _Debug(), #else #define printLine(l) #define printLine2(l,c) #define printVar(n) #define printArr(a,l) #define print2dArr(a,r,c) #define print2dArr2(a,r,c,l) #define debug #endif // define #define MAX_VAL 999999999 #define MAX_VAL_2 999999999999999999LL #define EPS 1e-6 #define mp make_pair #define pb push_back // typedef typedef unsigned int UI; typedef long long int LLI; typedef unsigned long long int ULLI; typedef unsigned short int US; typedef pair<int,int> pii; typedef pair<LLI,LLI> plli; typedef vector<int> vi; typedef vector<LLI> vlli; typedef vector<pii> vpii; typedef vector<plli> vplli; // ---------- END OF TEMPLATE ---------- int C[100000]; vi adjList[100000]; int parent[100000][17],order[100000]; int depth[100000]; int disc[100000],fin[100000],inv[100000],num = 0; int doDFS(int u,int d) { int i; depth[u] = d,disc[u] = num++,inv[num-1] = u; for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i],d+1); fin[u] = num; return 0; } int good[100000],pref[100000]; vpii chains; vi sorted; int bit[100001],bit2[100001]; int update(int u,int N) { int i; for (i = disc[u]+1; i <= N; i += i & (-i)) bit[i] += good[u] ? -1:1; for (i = fin[u]+1; i <= N; i += i & (-i)) bit[i] += good[u] ? 1:-1; good[u] ^= 1; return 0; } int query(int u) { int i,sum = 0; for (i = disc[u]+1; i > 0; i -= i & (-i)) sum += bit[i]; return sum; } int main() { int i; int N,A,B; scanf("%d",&N); for (i = 0; i < N; i++) scanf("%d",&C[i]); parent[0][0] = -1; for (i = 0; i < N-1; i++) { scanf("%d %d",&A,&B); A--,B--; adjList[A].pb(B),parent[B][0] = A; order[i] = B; } doDFS(0,0); int j,k; for (i = 1; (1 << i) < N; i++) { for (j = 0; j < N; j++) { if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1]; else parent[j][i] = -1; } } int logn = i; for (i = 0; i < N; i++) pref[i] = -1; for (i = 0; i < N-1; i++) { int u = order[i]; while (u != 0) { int v = pref[parent[u][0]]; if (v != -1) update(v,N); pref[parent[u][0]] = u,update(u,N); int w = parent[u][0]; for (j = logn-1; j >= 0; j--) { if ((parent[w][j] != -1) && (query(w) == (query(parent[w][j])+(1 << j)))) w = parent[w][j]; } if (v != -1) C[v] = C[w]; chains.pb(mp(C[w],depth[u]-depth[w])),sorted.pb(C[w]); u = w; } LLI ans = 0; sort(sorted.begin(),sorted.end()); sorted.resize(unique(sorted.begin(),sorted.end())-sorted.begin()); for (j = 0; j < chains.size(); j++) { int p = lower_bound(sorted.begin(),sorted.end(),chains[j].first)-sorted.begin(); for (k = p; k > 0; k -= k & (-k)) ans += (LLI) chains[j].second*bit2[k]; for (k = p+1; k <= sorted.size(); k += k & (-k)) bit2[k] += chains[j].second; } printf("%lld\n",ans); C[0] = C[order[i]]; for (j = 0; j <= sorted.size(); j++) bit2[j] = 0; chains.clear(),sorted.clear(); } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

construction.cpp: In function 'int doDFS(int, int)':
construction.cpp:66:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i],d+1);
                 ~~^~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:126:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < chains.size(); j++) {
                     ~~^~~~~~~~~~~~~~~
construction.cpp:129:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (k = p+1; k <= sorted.size(); k += k & (-k)) bit2[k] += chains[j].second;
                           ~~^~~~~~~~~~~~~~~~
construction.cpp:133:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j <= sorted.size(); j++) bit2[j] = 0;
                     ~~^~~~~~~~~~~~~~~~
construction.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&N);
     ~~~~~^~~~~~~~~
construction.cpp:90:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&C[i]);
                             ~~~~~^~~~~~~~~~~~
construction.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&A,&B);
         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...