답안 #411718

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411718 2021-05-25T18:51:00 Z AugustinasJucas Regions (IOI09_regions) C++14
1 / 100
8000 ms 64792 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC target("avx2")
using namespace std;
struct medis{
    struct node{
        int l, r, val = 0;
    };
    vector<node> tree;
    int n;
    int dbInd = 0;
    void build(int v){
        if(v >= n){
            tree[v].l = tree[v].r = dbInd++;
        }else{
            build(v*2); build(v*2+1);
            tree[v].l = tree[v*2].l;
            tree[v].r = tree[v*2+1].r; 
        }
    }
    medis(int dd){
        n = dd;
        tree.resize(n * 2 + 1);
        build(1);
    }
    void change(int v, int l, int r, int x){
        if(l <= tree[v].l && tree[v].r <= r){
            tree[v].val = x;
        }else if(r < tree[v].l || tree[v].r < l){
        
        }else{
            change(v*2, l, r, x);
            change(v*2+1, l, r, x);
            tree[v].val = tree[v*2].val + tree[v*2+1].val;
        }
    }
    int get(int v, int l, int r){
        if(l <= tree[v].l && tree[v].r <= r){
            return tree[v].val;
        }else if(r < tree[v].l || tree[v].r < l){
            return 0;
        }else{
            return get(v*2, l, r) + get(v*2+1, l, r);
        }
    }
};
const int dydis = 2e5 + 10;
int n, r, q;
vector<int> dideli; // regionai
vector<int> regs[dydis];
int tevas[dydis];
vector<int> gr[dydis];
unordered_map<long long, long long > rem;
int sm[dydis];
int val[dydis];
int kam[dydis];
int sq;
int dbInd = 0;
int kelint[dydis];
int enter[dydis], leave[dydis];
long long dideliuMem1[450][20001], dideliuMem2[450][20001];
medis tree(dydis);
void dfs1(int v, int came){
	enter[v] = dbInd++;
	for(auto x : gr[v]){
		if(x == came) continue;
		dfs1(x, v);
	}
	leave[v] = dbInd;
}
bool isAnc(int a, int b){
	return enter[a] <= enter[b] && leave[b] <= leave[a];
}
int kek = 0;
int prm = 0;
void dfs(int v){
//		cout << "v = " << v << endl;
	sm[v] = val[v];
	for(auto x : gr[v]){
		if(x == tevas[v]) continue;
		kek += val[v];
        dfs(x);
        kek -= val[v];
		sm[v] += sm[x];
	}
 
	if(!val[v]){
//		cout << "rem[" << prm+1 << "][" << v+1 << "(" << kam[v]+1 << ")] += " << kek << "\n";
//		cout << "rem[" << v+1 << "(" << kam[v]+1 << ")][" << prm+1 << "] += " << sm[v] << "\n"
		dideliuMem1[prm][kam[v]] += kek;
		dideliuMem2[prm][kam[v]] += sm[v];
	}
}
int dbIndd = 0;
void calc(int ind){
	kelint[ind] = dbIndd++;
//	cout << "nuo " << ind+1 << endl;
	for(int i = 0; i < n; i++) sm[i] = val[i] = 0;
	for(auto x : regs[ind]){
		val[x] = 1;
	}
    kek = 0;
    prm = dbIndd-1;
	dfs(0);
}
long long f(int a, int b){
	if((int)regs[a].size() >= sq){
		int sk = kelint[a];
		return dideliuMem1[sk][b];
	}
	if((int)regs[b].size() >= sq){
		int sk = kelint[b];
		return dideliuMem2[sk][a];
	}
	if(rem.count(a*dydis+b)) {
		return rem[a*dydis+b] ;
	}
	long long ret = 0;
	for(auto x : regs[b]){
     //   tree.change(1, enter[x], enter[x], 1);
    }
    for(auto x : regs[a]){
        ret += tree.get(1, enter[x], leave[x]-1);
    }
    for(auto x : regs[b]){
	   // tree.change(1, enter[x], enter[x], 0);
    }
	return rem[a*dydis+b] = ret;
}
int main(){
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin >> n >> r >> q;
	for(int i = 0; i < n; i++){
		int par = -1, reg;
		if(i == 0) cin >> reg;
		else cin >> par >> reg;
		par--; reg--;
		regs[reg].push_back(i);
		if(par != -2){
			gr[i].push_back(par);
			gr[par].push_back(i);
		}
		tevas[i] = par;
		kam[i] = reg;
	}
	dfs1(0, -1);
	sq = sqrt(n);
	for(int i = 0; i < r; i++){
		if((int)regs[i].size() >= sq){
			calc(i);
			dideli.push_back(i);
		}
	}
	while(q--){
		int a, b; cin >> a >> b; a--; b--;
		cout << f(a, b) << endl;
	}
	return 0;
}

Compilation message

regions.cpp: In function 'long long int f(int, int)':
regions.cpp:119:11: warning: unused variable 'x' [-Wunused-variable]
  119 |  for(auto x : regs[b]){
      |           ^
regions.cpp:125:14: warning: unused variable 'x' [-Wunused-variable]
  125 |     for(auto x : regs[b]){
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14408 KB Output is correct
2 Incorrect 11 ms 14408 KB Output isn't correct
3 Incorrect 14 ms 14444 KB Output isn't correct
4 Incorrect 14 ms 14448 KB Output isn't correct
5 Incorrect 23 ms 14468 KB Output isn't correct
6 Incorrect 35 ms 14628 KB Output isn't correct
7 Incorrect 42 ms 14536 KB Output isn't correct
8 Incorrect 65 ms 14684 KB Output isn't correct
9 Incorrect 91 ms 15044 KB Output isn't correct
10 Incorrect 155 ms 15372 KB Output isn't correct
11 Incorrect 288 ms 15552 KB Output isn't correct
12 Incorrect 267 ms 16268 KB Output isn't correct
13 Incorrect 373 ms 16324 KB Output isn't correct
14 Incorrect 629 ms 16936 KB Output isn't correct
15 Incorrect 692 ms 20152 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2195 ms 20948 KB Output isn't correct
2 Incorrect 2570 ms 20840 KB Output isn't correct
3 Incorrect 5162 ms 25116 KB Output isn't correct
4 Incorrect 766 ms 17280 KB Output isn't correct
5 Incorrect 789 ms 19088 KB Output isn't correct
6 Incorrect 859 ms 26400 KB Output isn't correct
7 Incorrect 1306 ms 28324 KB Output isn't correct
8 Incorrect 1694 ms 42596 KB Output isn't correct
9 Execution timed out 8068 ms 28960 KB Time limit exceeded
10 Incorrect 5572 ms 64792 KB Output isn't correct
11 Execution timed out 8031 ms 30808 KB Time limit exceeded
12 Incorrect 3801 ms 28404 KB Output isn't correct
13 Incorrect 5644 ms 30644 KB Output isn't correct
14 Incorrect 5545 ms 37060 KB Output isn't correct
15 Execution timed out 8052 ms 36584 KB Time limit exceeded
16 Execution timed out 8071 ms 42780 KB Time limit exceeded
17 Execution timed out 8058 ms 49272 KB Time limit exceeded