답안 #1020383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020383 2024-07-12T03:11:19 Z KhoaDuy Tree Rotations (POI11_rot) C++14
36 / 100
653 ms 65536 KB
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int MAXN=2*1e5;
vector<vector<int>> graph(4*MAXN+1);
vector<vector<int>> leaf(4*MAXN+1);
int seg[2*MAXN]={0};
int n;
int ptr=0;
long long ans=0;
void build(int l,int r){
    l+=n,r+=n;
    while(l>1){
        l>>=1,r>>=1;
        for(int v=l;v<=r;v++){
            seg[v]=seg[v<<1]+seg[(v<<1)|1];
        }
    }
}
void modify(int i,int x){
    i+=n;
    seg[i]+=x;
    build(i-n,i-n);
}
long long query(int l,int r){
    if(l>r){
        return 0;
    }
    l+=n,r+=n;
    int curr=0;
    for(;l<=r;l>>=1,r>>=1){
        if(l&1){
            curr+=seg[l];
            l++;
        }
        if(!(r&1)){
            curr+=seg[r];
            r--;
        }
    }
    return curr;
}
void DFS(int u,int p,bool keep){
    int big=-1;
    long long inver=0;
    long long tot=0;
    for(int i=0;i<graph[u].size();i++){
        int v=graph[u][i];
        if(v!=p&&(big==-1||leaf[big].size()<leaf[v].size())){
            big=v;
        }
    }
    if(big==-1){
        leaf[u].push_back(u);
        modify(u-1,1);
    }
    else{
        for(int i=0;i<graph[u].size();i++){
            int v=graph[u][i];
            if(v!=p&&v!=big){
                DFS(v,u,false);
            }
        }
        DFS(big,u,true);
        tot=leaf[big].size();
        swap(leaf[u],leaf[big]);
        for(int i=0;i<graph[u].size();i++){
            int v=graph[u][i];
            if(v!=p&&v!=big){
                tot*=((long long)(leaf[v].size()));
                for(int j=0;j<leaf[v].size();j++){
                    int temp=leaf[v][j];
                    inver+=query(temp,n-1);
                }
            }
        }
        for(int i=0;i<graph[u].size();i++){
            int v=graph[u][i];
            if(v!=p&&v!=big){
                for(int j=0;j<leaf[v].size();j++){
                    leaf[u].push_back(leaf[v][j]);
                    modify(leaf[v][j]-1,1);
                }
            }
        }
    }
    ans+=min(inver,tot-inver);
    if(!keep){
        for(int i=0;i<leaf[u].size();i++){
            modify(leaf[u][i]-1,-1);
        }
    }
}
vector<int> input;
int tim;
int Buildtree(){
    if(input[ptr]!=0){
        ptr++;
        return input[ptr-1];
    }
    ptr++;
    int u=tim;
    tim++;
    int v=Buildtree();
    graph[u].push_back(v);
    graph[v].push_back(u);
    v=Buildtree();
    graph[u].push_back(v);
    graph[v].push_back(u);
    return u;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    tim=n+1;
    cin.ignore();
    string line;
    while(getline(cin,line)){
        if(line.empty()){
            break;
        }
        stringstream ss;
        ss << line;
        int x=0;
        ss >> x;
        input.push_back(x);
    }
    int root=Buildtree();
    DFS(root,-1,true);
    cout << ans;
}

Compilation message

rot.cpp: In function 'void DFS(int, int, bool)':
rot.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for(int i=0;i<graph[u].size();i++){
      |                 ~^~~~~~~~~~~~~~~~
rot.cpp:58:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for(int i=0;i<graph[u].size();i++){
      |                     ~^~~~~~~~~~~~~~~~
rot.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for(int i=0;i<graph[u].size();i++){
      |                     ~^~~~~~~~~~~~~~~~
rot.cpp:71:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |                 for(int j=0;j<leaf[v].size();j++){
      |                             ~^~~~~~~~~~~~~~~
rot.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |         for(int i=0;i<graph[u].size();i++){
      |                     ~^~~~~~~~~~~~~~~~
rot.cpp:80:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |                 for(int j=0;j<leaf[v].size();j++){
      |                             ~^~~~~~~~~~~~~~~
rot.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int i=0;i<leaf[u].size();i++){
      |                     ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 37976 KB Output is correct
2 Correct 18 ms 37976 KB Output is correct
3 Correct 17 ms 37980 KB Output is correct
4 Correct 18 ms 37824 KB Output is correct
5 Correct 18 ms 37980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 37976 KB Output is correct
2 Correct 17 ms 37992 KB Output is correct
3 Correct 20 ms 38232 KB Output is correct
4 Correct 19 ms 37980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 37976 KB Output is correct
2 Correct 19 ms 37980 KB Output is correct
3 Correct 20 ms 37980 KB Output is correct
4 Correct 25 ms 38488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 51496 KB Output is correct
2 Correct 28 ms 39000 KB Output is correct
3 Correct 273 ms 53980 KB Output is correct
4 Correct 463 ms 56916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 642 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 213 ms 47508 KB Output is correct
2 Runtime error 653 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 428 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 652 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 392 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 511 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 550 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -