Submission #217335

# Submission time Handle Problem Language Result Execution time Memory
217335 2020-03-29T12:42:14 Z 2fat2code Traffickers (RMI18_traffickers) C++14
0 / 100
3500 ms 197492 KB
#include <bits/stdc++.h>
#define debug cout << "CHECK" << '\n'
#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 pii pair<pair<int,int>,int>
#define mp make_pair
#define int long long
#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],tin[nmax],tout[nmax],lca[nmax][18],aib[21][21][2*nmax],first[nmax],last[nmax];
vector<int>nod[nmax],euler;

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

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(tin[lca[x][0]]>tin[y] || tout[lca[x][0]]<tout[y]){
        for(int j=1;j<=22;j++){
            if(lca[x][j]==0 || (tin[lca[x][j]]<tin[y] && tout[lca[x][j]]>tout[y])){
                x=lca[x][j-1];
                break;
            }
        }
    }
    return lca[x][0];
}

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

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);
    for(int i=tz.size()-1;i>=0;i--) path.push_back(tz[i]);
    for(int i=0;i<=19;i++){
        int nod = path[i%path.size()];
        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 t1,int t2){
    int cnt[20];
    int lc=findLCA(x,y);
    for(int i=0;i<=19;i++) cnt[i]=0;
    while(t1<t2 && t1%20!=0){
        cnt[t1%20]++;
        t1++;
    }
    while(t2>t1 && t2%20!=19){
        cnt[t2%20]++;
        t2--;
    }
    if(t1==t2){
        cnt[t1%20]++;
    }
    else{
        int curr=(t2-t1)/20;
        for(int i=0;i<=19;i++) cnt[i]+=curr;
    }
    int ans=0;
    for(int i=0;i<=19;i++){
        for(int j=1;j<=20;j++){
            int curr=cnt[i]*(sum(first[x],aib[i][j])+sum(first[y],aib[i][j])-sum(first[lc],aib[i][j])-sum(first[parinte[lc]],aib[i][j]));
            ans+=curr;
        }
    }
    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);
    }
    int timp=0;
    DFS(1,-1,timp);
    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;i<=17;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;
            cout << query3(x,y,t1,t2) << '\n';
        }
    }
}

Compilation message

traffickers.cpp:8:0: warning: ignoring #pragma GCC taraget [-Wunknown-pragmas]
 #pragma GCC taraget("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
traffickers.cpp: In function 'void update(long long int, long long int, long long int*)':
traffickers.cpp:60:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(pos<=euler.size()){
           ~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 3589 ms 2688 KB Time limit exceeded
2 Execution timed out 3596 ms 3072 KB Time limit exceeded
3 Execution timed out 3578 ms 6528 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 3587 ms 67960 KB Time limit exceeded
2 Execution timed out 3589 ms 61304 KB Time limit exceeded
3 Execution timed out 3581 ms 64884 KB Time limit exceeded
4 Execution timed out 3561 ms 67832 KB Time limit exceeded
5 Execution timed out 3590 ms 67832 KB Time limit exceeded
6 Execution timed out 3578 ms 61560 KB Time limit exceeded
7 Execution timed out 3584 ms 62584 KB Time limit exceeded
8 Execution timed out 3570 ms 67956 KB Time limit exceeded
9 Execution timed out 3546 ms 62580 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 3585 ms 197012 KB Time limit exceeded
2 Execution timed out 3575 ms 140788 KB Time limit exceeded
3 Execution timed out 3584 ms 187152 KB Time limit exceeded
4 Execution timed out 3573 ms 186996 KB Time limit exceeded
5 Execution timed out 3581 ms 91124 KB Time limit exceeded
6 Execution timed out 3600 ms 197492 KB Time limit exceeded
7 Execution timed out 3596 ms 196852 KB Time limit exceeded
8 Execution timed out 3594 ms 197228 KB Time limit exceeded