답안 #211911

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211911 2020-03-21T17:14:28 Z nicolaalexandra Traffickers (RMI18_traffickers) C++14
60 / 100
3500 ms 12032 KB
/// yay heavy
/// bine ca scap de mle da acum iau tle
#include <bits/stdc++.h>
#define DIM 30002
//#pragma GCC optimize("Ofast")
/*
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")*/
using namespace std;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
vector <short> L[DIM],w,aux;
vector <short> chains[DIM];
//short chains[DIM];
int aib[20][21][DIM];

short whatChain[DIM],chainFatherNode[DIM],Size[DIM],posAib[DIM];
short level[DIM],fth[DIM];

int q,k,t1,t2;
short n,x,y,i,j,tip,m,nr_chains;
long long sum;

void update_aib (short a, short b, short p, short n, int val){
    for (;p<=n;p+=(p&-p))
        aib[a][b][p] += val;
}
long long query_aib (short a, short b, short p){
    if (!p)
        return 0;
    long long sol = 0;
    for (;p;p-=(p&-p))
        sol += aib[a][b][p];
    return sol;
}

long long get_sum (short a, short b, short x, short y){
    return query_aib (a,b,y) - query_aib (a,b,x-1);
}

void dfs (short nod, short tata){

    fth[nod] = tata;
    level[nod] = 1 + level[tata];
    Size[nod] = 1;
    short ok = 0, maxim = 0, poz = 0;
    for (auto vecin : L[nod]){
        if (vecin != tata){
            ok = 1;
            dfs (vecin,nod);

            Size[nod] += Size[vecin];

            if (Size[vecin] > maxim)
                maxim = Size[vecin], poz = vecin;

        }}

    if (!ok){
        nr_chains++;
        chains[nr_chains].push_back(0);
        chains[nr_chains].push_back(nod);

        whatChain[nod] = nr_chains;

    } else {

        chains[whatChain[poz]].push_back(nod);

        whatChain[nod] = whatChain[poz];

        for (auto vecin : L[nod]){
            if (vecin == tata || vecin == poz)
                continue;
            chainFatherNode[whatChain[vecin]] = nod;
        }}}


void drum (short x, short y){

    w.clear();
    aux.clear();

    while (x != y){
        if (level[x] > level[y]){
            w.push_back(x);
            x = fth[x];
        } else {
            aux.push_back(y);
            y = fth[y];
        }
    }
    w.push_back(x);

    for (j=aux.size()-1;j>=0;j--)
        w.push_back(aux[j]);

}
void adauga (short x, short y){

    drum (x,y);

    /// in w am lantul de la x la y

    short t = 0, lg = w.size();
    for (auto nod : w){

        update_aib(t,lg,posAib[nod],n,1);

        t++;
    }
}
void scoate (short x, short y){
    drum (x,y);

    short t = 0, lg = w.size();

    for (auto nod : w){

        update_aib(t,lg,posAib[nod],n,-1);

        t++;
    }
}

void query_heavy (short x, short y, short a, short b){
    short chainx = whatChain[x], chainy = whatChain[y];;
    if (chainx == chainy){
        short posx = posAib[x], posy = posAib[y];
        if (posx > posy)
            swap (posx,posy);
        sum += get_sum (a,b,posx,posy);
        return;
    }
    if (level[chainFatherNode[chainx]] <= level[chainFatherNode[chainy]])
        swap (x,y), swap (chainx,chainy);

    sum += get_sum (a,b,posAib[x],posAib[ chains[chainx][ chains[chainx].size()-1 ] ]);

    short nr = chainFatherNode[chainx];
    query_heavy (nr,y,a,b);
}

long long solve (short x, short y, int t1, int t2){

    long long sol1 = 0, sol2 = 0;
    for (short a=0;a<20;++a){
        for (short b=a+1;b<=20;++b){
            sum = 0;
            query_heavy (x,y,a,b);

            if (t2 >= a){

                int nr = (t2 - a) / b + 1;

                sol2 += 1LL * nr * sum;
            }
            if (t1 >= a){

                int nr = (t1 - a) / b + 1;

                sol1 += 1LL * nr * sum;
            }
        }
    }

    return sol2 - sol1;
}
int main (){

  //  ifstream cin ("traffickers.in");
  //  ofstream cout ("traffickers.out");

    cin>>n;
    for (i=1;i<n;++i){
        cin>>x>>y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    dfs (1,0);

    short idx = 0;

    for (i=1;i<=nr_chains;++i)
        for (j=1;j<chains[i].size();++j){
            short nod = chains[i][j];
            posAib[nod] = ++idx;
        }


    cin>>m; /// nr inital de traficanti
    for (i=1;i<=m;++i){
        cin>>x>>y;
        adauga (x,y);
    }

    cin>>q;
    for (;q--;){
        cin>>tip>>x>>y;
        if (tip == 1){
            adauga (x,y);
            continue;
        }
        if (tip == 2){
            scoate (x,y);
            continue;
        }
        cin>>t1>>t2;
        cout<<solve(x,y,t1-1,t2)<<"\n";
    }

    return 0;
}

Compilation message

traffickers.cpp: In function 'int main()':
traffickers.cpp:240:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j=1;j<chains[i].size();++j){
                  ~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2688 KB Output is correct
2 Correct 15 ms 3584 KB Output is correct
3 Correct 14 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 11000 KB Output is correct
2 Correct 176 ms 9336 KB Output is correct
3 Correct 112 ms 11512 KB Output is correct
4 Correct 144 ms 10880 KB Output is correct
5 Correct 161 ms 10496 KB Output is correct
6 Correct 203 ms 11008 KB Output is correct
7 Correct 158 ms 11128 KB Output is correct
8 Correct 136 ms 11776 KB Output is correct
9 Correct 137 ms 12032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3586 ms 4344 KB Time limit exceeded
2 Execution timed out 3575 ms 5112 KB Time limit exceeded
3 Execution timed out 3563 ms 4472 KB Time limit exceeded
4 Execution timed out 3586 ms 3704 KB Time limit exceeded
5 Execution timed out 3569 ms 3448 KB Time limit exceeded
6 Execution timed out 3585 ms 5112 KB Time limit exceeded
7 Execution timed out 3575 ms 5240 KB Time limit exceeded
8 Execution timed out 3579 ms 4728 KB Time limit exceeded