답안 #217452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
217452 2020-03-29T18:07:57 Z 2fat2code Traffickers (RMI18_traffickers) C++17
100 / 100
2457 ms 112372 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],height[nmax];
vector<int>nod[nmax],euler;

void DFS(int s,int par,int lvl){
    euler.push_back(s);
    parinte[s]=par;
    height[s]=lvl;
    for(auto it:nod[s]){
        if(it!=par){
            DFS(it,s,lvl+1);
            euler.push_back(s);
        }
    }
}


int findLCA(int x, int y){
	if(height[x]<height[y]) swap(x, y);
	int log1, log2;
	for(log1 = 1; (1 << log1) < height[x]; ++log1);
	for(log2 = 1; (1 << log2) < height[y]; ++log2);
	for(int k = log1; k >= 0; --k)
		if(height[x] - (1 << k) >= height[y])
			x = lca[x][k];
	if(x == y) return x;
	for(int k = log2; k >= 0; --k)
		if(lca[x][k] && lca[x][k] != lca[y][k])
			x = lca[x][k],
			y = lca[y][k];
	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());
  //  ifstream cin("input.in");
    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);
    }
    DFS(1,-1,0);
    int pop=0;
    for(auto it:euler){
        pop++;
        if(first[it]==0){
            first[it]=pop;
            last[it]=pop;
        }
        else last[it]=pop;
    }
    for(int i=1;i<=n;i++) lca[i][0]=parinte[i];
    for(int i=1;(1<<i)<=n;i++){
        for(int j=1;j<=n;j++) lca[j][i]=lca[lca[j][i-1]][i-1];
    }
    parinte[1]=0;
    cin >> q;
    for(int i=1;i<=q;i++){
        int x,y;
        cin >> x >> y;
        query12(x,y,1);
    }
    cin >> q;
    int t=0;
    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(long long int, long long int, long long int*)':
traffickers.cpp:58:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(pos<=euler.size()){
           ~~~^~~~~~~~~~~~~~
traffickers.cpp: In function 'void query12(long long int, long long int, long long int)':
traffickers.cpp:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<path.size();i++){
                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2176 KB Output is correct
2 Correct 11 ms 5760 KB Output is correct
3 Correct 11 ms 5736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 38232 KB Output is correct
2 Correct 202 ms 34572 KB Output is correct
3 Correct 179 ms 36724 KB Output is correct
4 Correct 204 ms 38356 KB Output is correct
5 Correct 212 ms 38112 KB Output is correct
6 Correct 198 ms 38264 KB Output is correct
7 Correct 197 ms 37620 KB Output is correct
8 Correct 185 ms 38304 KB Output is correct
9 Correct 187 ms 38396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2106 ms 111308 KB Output is correct
2 Correct 2017 ms 111844 KB Output is correct
3 Correct 1965 ms 111584 KB Output is correct
4 Correct 2265 ms 110840 KB Output is correct
5 Correct 2457 ms 110632 KB Output is correct
6 Correct 2029 ms 112020 KB Output is correct
7 Correct 2318 ms 112372 KB Output is correct
8 Correct 2296 ms 111520 KB Output is correct