QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620616 | #7736. Red Black Tree | ji_114514 | TL | 1815ms | 52136kb | C++20 | 2.2kb | 2024-10-07 19:52:11 | 2024-10-07 19:52:14 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
struct Magic {
vector<int>neg, pos;
int z, sum;
Magic() : z(0), sum(0) {};
Magic(vector<int>& v) : z(0), sum(0) {
int i = 0, j = (int)v.size() - 1;
while (i < v.size() && v[i] < 0)neg.push_back(v[i]), sum += v[i], i++;
while (j >= 0 && v[j] > 0)pos.push_back(v[j]), j--;
z = j - i + 1;
};
int size()
{
return neg.size() + pos.size() + z;
}
int data(int i)
{
if (i < neg.size())return neg[i];
else if (i < neg.size() + z)return 0;
else return pos[pos.size() - (i - z - neg.size()) - 1];
}
void insert(int x)
{
if (x < 0)neg.push_back(x), sum += x;
else pos.push_back(x);
}
};
int n, ans[N];
vector<int>e[N];
string s;
/*
总之对于突序列的维护就是维护首项和他们的差分数组
闵可夫斯基告诉我们差分数组直接归并为有序数组即可
那么首项呢
*/
pair<int, Magic> dfs(int u)
{
Magic magic;
int val = 0;//这就是首项
for (auto v : e[u])
{
auto res = dfs(v);
if (magic.size() == 0)magic = move(res.second), val += res.first;
else {
int len = min(magic.size(), res.second.size());
//暴力合并
val += res.first;
vector<int>v(len);
for (int i = 0; i < len; i++)v[i] = magic.data(i) + res.second.data(i);
magic = Magic(v);
}
}
//闵可夫斯基和
if (s[u - 1] == '0')// 1
{
magic.insert(1);
}
else {//-1
val++, magic.insert(-1);
}
ans[u] = val + magic.sum;
return { val, magic };
}
void solve()
{
cin >> n >> s;
for (int i = 1; i <= n; i++)e[i].clear();
for (int i = 2; i <= n; i++) {
int p; cin >> p;
e[p].push_back(i);
}
dfs(1);
for (int i = 1; i <= n; i++)cout << ans[i] << ' ';
cout << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5648kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
4 1 2 0 0 0 0 0 0 2 0 0 0
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1815ms
memory: 52136kb
input:
6107 12 000000001000 1 2 3 2 5 4 4 7 3 8 11 19 1100111101111011110 1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5 7 0111110 1 1 2 2 1 5 3 000 1 1 7 1000011 1 2 3 3 5 4 7 0001001 1 1 1 3 5 3 8 00111000 1 1 3 2 5 2 7 11 11111110111 1 1 1 4 5 4 5 2 5 1 15 110101101000010 1 2 3 2 1 5 2 5 6 5 8 7 9 14 10 0101000...
output:
1 1 1 1 0 0 0 0 0 0 0 0 6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 2 1 0 0 0 0 0 0 4 0 0 2 1 0 0 0 0 0 0 4 3 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 5 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0...
result:
ok 6107 lines
Test #3:
score: 0
Accepted
time: 218ms
memory: 9712kb
input:
10 100000 10001010000001100001000100001000010100010101100001001110110001000010000110001000000010000011000001000001010001110100000000000000000010011011100000000000001000000000100100100110000000100001010011000000110000000111010100100001100000100100101001000000010000000011100000000000000010001100011100...
output:
22440 21414 19422 13454 5328 2719 1421 1168 1478 661 4662 5037 418 183 2304 501 2008 1643 692 2211 570 1003 967 950 504 124 894 333 775 523 905 197 12 337 195 310 1325 1016 638 50 907 361 179 336 313 102 309 555 733 871 490 414 375 318 66 625 336 267 276 162 203 25 112 216 252 146 42 233 232 333 27 ...
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 121ms
memory: 4432kb
input:
10 100000 01010111111101011100011111111010011001111111110001100111111101011111110011101111110110111011010111011011010011111110101111111011111111011101011111011001110101111011011010110100011111001101001011111101111101111111111100101101000111111110111101111111111011111100111011101110110101111010101101...
output:
25019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 10 lines
Test #5:
score: -100
Time Limit Exceeded
input:
10 100000 00111110110011111111111010011111011111101010110111111110011110111111111111000110101110110111111101011111111111010101111111011001110110011101111001110111101101110110101000011111110100101110110100111110001111011100111101011010111111011011100011111011110111111110011110111111001111111010011100...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...