답안 #223626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223626 2020-04-15T20:33:06 Z 2fat2code Traffickers (RMI18_traffickers) C++17
0 / 100
694 ms 524292 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC taraget("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz() size()
#define fr first
#define sc second
#define pi pair<int,int>
//#define int long long
#define pii pair<pair<int,int>,int>
#define mp make_pair
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
using namespace std;

const int mod=1e9+7;
const int modp=1999999973;
const int modulo=998244353;

const int nmax=30005;

int n,q,parinte[nmax],lca[nmax][23],aib[21][21][2*nmax],first[nmax],last[nmax];
int tin[nmax],tout[nmax];
vector<int>nod[nmax],euler;

void DFS(int s,int par,int &timp){
    euler.push_back(s);
    parinte[s]=par;
    tin[s]=(++timp);
    for(auto it:nod[s]){
        if(it!=par){
            DFS(it,s,timp);
            euler.push_back(s);
        }
    }
    tout[s]=(++timp);
}


int findLCA(int x, int y){
	if(x==y) return x;
    if(tin[x]<tin[y] && tout[x]>tout[y]) return x;
    if(tin[y]<tin[x] && tout[y]>tout[x]) return y;
    while(true){
        bool bl=false;
        for(int j=0;j<=21;j++){
            int x1=lca[x][j];
            int y1=lca[x][j+1];
            if(tin[x1]>tin[y] || tout[x1]<tout[y]){
                if(tin[y1]<tin[y] && tout[y1]>tout[y]){
                    x=x1;
                    bl=true;
                    break;
                }
            }
        }
        if(bl==false) break;
    }
    return lca[x][0];
}

void update(int pos,int val,int aib[]){
    while(pos<=euler.size()){
        aib[pos]+=val;
        pos+=(pos&-pos);
    }
}

int sum(int pos,int aib[]){
    int rs=0;
    while(pos){
        rs+=aib[pos];
        pos-=(pos&-pos);
    }
    return rs;
}


void query12(int x,int y,int type){
    int lc=findLCA(x,y);
    vector<int>path;
    while(x!=lc){
        path.push_back(x);
        x=parinte[x];
    }
    vector<int>tz;
    while(y!=lc){
        tz.push_back(y);
        y=parinte[y];
    }
    tz.push_back(y);
  if(tz.size())  for(int i=tz.size()-1;i>=0;i--) path.push_back(tz[i]);
    for(int i=0;i<path.size();i++){
        int nod = path[i];
        if(type==1){
            update(first[nod],1,aib[i][path.size()]);
            update(last[nod]+1,-1,aib[i][path.size()]);
        }
        else{
            update(first[nod],-1,aib[i][path.size()]);
            update(last[nod]+1,1,aib[i][path.size()]);
        }
    }
}


int query3(int x,int y,int timp){
    if(timp==-1) return 0LL;
    int lc=findLCA(x,y);
    int ans=0;
    for(int i=1;i<=20;i++){
        int cmp=(timp+1)/i;
        bool bo=false;
        for(int j=0;j<i;j++){
            if((timp+1)%i>j){
                bo=true;
                cmp++;
            }
            ans+=cmp*(sum(first[x],aib[j][i])+sum(first[y],aib[j][i])-sum(first[lc],aib[j][i])-sum(first[parinte[lc]],aib[j][i]));
            if(bo==true){
                bo=false;
                cmp--;
            }
        }
    }
    return ans;
}

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
    srand(chrono::steady_clock::now().time_since_epoch().count());
    cin >> n;
    for(int i=1;i<n;i++){
        int x,y;
        cin >> x >> y;
        nod[x].push_back(y);
        nod[y].push_back(x);
    }
    int timpu=0;
    DFS(1,-1,timpu);
    int pop=0;
    for(auto it:euler){
        pop++;
        if(first[it]==0){
            first[it]=pop;
            last[it]=pop;
        }
        else last[it]=pop;
    }
    parinte[1]=0;
    for(int i=1;i<=n;i++) lca[i][0]=parinte[i];
    for(int i=1;i<=21;i++){
        for(int j=1;j<=n;j++) lca[j][i]=lca[lca[j][i-1]][i-1];
    }
    cin >> q;
    for(int i=1;i<=q;i++){
        int x,y;
        cin >> x >> y;
        query12(x,y,1);
    }
    cin >> q;
    int t=0;
    tin[0]=0;
    tout[0]=1e18;
    for(int i=1;i<=q;i++){
        cin >> t;
        if(t==1 || t==2){
            int x,y;
            cin >> x >> y;
            query12(x,y,t);
        }
        else{
            int x,y,t1,t2;
            cin >> x >> y >> t1 >> t2;
            --t1;
            cout << query3(x,y,t2) - query3(x,y,t1) << '\n';
        }
    }
}

Compilation message

traffickers.cpp: In function 'void update(int, int, int*)':
traffickers.cpp:66:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(pos<=euler.size()){
           ~~~^~~~~~~~~~~~~~
traffickers.cpp: In function 'void query12(int, int, int)':
traffickers.cpp:96:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<path.size();i++){
                 ~^~~~~~~~~~~~
traffickers.cpp: In function 'int32_t main()':
traffickers.cpp:167:13: warning: overflow in implicit constant conversion [-Woverflow]
     tout[0]=1e18;
             ^~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 681 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 666 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 668 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 694 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 672 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 670 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 683 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 671 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 675 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 676 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Runtime error 668 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Runtime error 679 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 687 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 678 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 680 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 679 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 682 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 692 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 691 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Runtime error 682 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)