#include <bits/stdc++.h>
using namespace std;
const int nmax = 200000;
const int block = 1000;
int n, r, q;
int region[nmax + 5], cntRegion[nmax + 5];
int bigRegion[nmax + 5], cntBigRegion, bigIdx[nmax + 5];
int smallRegion[nmax + 5], cntSmallRegion, smallIdx[nmax + 5];
int cntAbove[nmax + 5], sol[205][25005], sumePartiale[nmax + 5];
int precalc[205][25005];
int cntTimp;
pair<int, int> timp[nmax + 5];
vector<int> nodesFromRegion[nmax + 5];
vector<int> g[nmax + 5];
void dfs(int fiu, int tata)
{
timp[fiu].first = ++cntTimp;
/// cntAbove[i] -> numarul de noduri de regiunea i(care este o regiune de tip BIG), care sunt stramosi ai nodului curent
if(bigIdx[region[fiu]] != 0)
{
cntAbove[bigIdx[region[fiu]]] ++;
}
for(int i = 1; i <= cntBigRegion; i ++)
{
sol[bigIdx[bigRegion[i]]][region[fiu]] += cntAbove[bigIdx[region[fiu]]];
}
for(auto it : g[fiu])
{
if(it == tata)
{
continue;
}
dfs(it, fiu);
}
if(bigIdx[region[fiu]] != 0)
{
cntAbove[bigIdx[region[fiu]]] ++;
}
timp[fiu].second = cntTimp;
}
int main()
{
cin >> n >> r >> q;
cin >> region[1];
cntRegion[region[1]] ++;
for(int i = 2; i <= n; i ++)
{
int p;
cin >> p >> region[i];
g[p].push_back(i);
g[i].push_back(p);
cntRegion[region[i]] ++;
nodesFromRegion[region[i]].push_back(i);
}
for(int i = 1; i <= r; i ++)
{
if(cntRegion[i] >= block)
{
bigRegion[++cntBigRegion] = i;
bigIdx[i] = cntBigRegion;
}
else
{
smallRegion[++cntSmallRegion] = i;
smallIdx[i] = cntSmallRegion;
}
}
/// in dfs calculez pentru tipurile 1 si 2, cand regiunea de sus este de tip BIG
dfs(1, 0);
cout << cntTimp;
return 0;
/// imi propun sa calculez pentru tipul 3, cand regiunea de jos este de tip BIG, iar regiunea de sus este de tip SMALL
for(int i = 1; i <= r; i ++)
{
if(bigIdx[i] == 0)
{
continue;
}
for(auto it : nodesFromRegion[i])
{
sumePartiale[timp[it].first] ++;
}
for(int j = 1; j <= n; j ++)
{
sumePartiale[j] += sumePartiale[j - 1];
}
for(int j = 1; j <= cntSmallRegion; j ++)
{
for(auto it : nodesFromRegion[smallRegion[i]])
{
precalc[bigIdx[i]][j] += 1LL * (sumePartiale[timp[it].second] - sumePartiale[timp[it].first] - 1);
}
}
for(int j = 1; j <= n; j ++)
{
sumePartiale[j] = 0;
}
}
while(q--)
{
int a, b;
cin >> a >> b;
if(bigIdx[a] != 0)
{
/// cand regiunea de sus e de tip BIG
cout << sol[bigIdx[a]][b] << '\n';
}
else
{
if(bigIdx[b] != 0)
{
/// cand regiunea de jos e BIG, dar cea de sus e SMALL
cout << precalc[bigIdx[b]][smallIdx[a]] << '\n';
}
else
{
cout << 0 << '\n';
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9680 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
5 ms |
9680 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
5 ms |
9680 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
5 ms |
9680 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
5 ms |
9728 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
5 ms |
9808 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
6 ms |
9768 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
6 ms |
9808 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
8 ms |
10244 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
12 ms |
10236 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
15 ms |
10432 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
18 ms |
10928 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
18 ms |
10920 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
22 ms |
11176 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
28 ms |
14124 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
14288 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
61 ms |
13820 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
65 ms |
16200 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
26 ms |
11392 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
32 ms |
13144 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
52 ms |
12680 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
57 ms |
13624 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
76 ms |
19016 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
126 ms |
18656 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
157 ms |
24264 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
187 ms |
20580 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
171 ms |
19644 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
166 ms |
20540 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
197 ms |
21488 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
152 ms |
24664 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
163 ms |
32120 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
176 ms |
31424 KB |
Unexpected end of file - int32 expected |