QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602234#7736. Red Black TreeMiniLongML 3ms11432kbC++173.8kb2024-09-30 21:29:452024-09-30 21:29:45

Judging History

你现在查看的是最新测评结果

  • [2024-09-30 21:29:45]
  • 评测
  • 测评结果:ML
  • 用时:3ms
  • 内存:11432kb
  • [2024-09-30 21:29:45]
  • 提交

answer

#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
    #ifdef ONLINE_JUDGE
    char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
    #define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
    #else
    #define get() getchar()
    #endif
    template<typename T> inline void read(T &t){
        T x = 0, f = 1;
        char c = getchar();
        while(!isdigit(c)){
            if(c == '-') f = -f;
            c = getchar();
        }
        while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
        t = x * f;
    }
    template<typename T, typename ... Args> inline void read(T &t, Args&... args){
        read(t);
        read(args...);
    }
    template<typename T> void write(T t){
        if(t < 0) putchar('-'), t = -t;
        if(t >= 10) write(t / 10);
        putchar(t % 10 + '0');
    }
    template<typename T, typename ... Args> void write(T t, Args... args){
        write(t), putchar(' '), write(args...);
    }
    template<typename T> void writeln(T t){
        write(t);
        puts("");
    }
    template<typename T> void writes(T t){
        write(t), putchar(' ');
    }
    #undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
    const ll mod = 998244353;
    ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
    void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
    void add(ll &x, ll y){x = (x + y) % mod;}
    void mul(ll &x, ll y){x = x * y % mod;}
    ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
    ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
    ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e5 + 5, inf = 0x3f3f3f3f;
int n, f[N];
char s[N];
vector<int> G[N];
struct node{
    vector<int> pos, neg;
    int num, sum;
    node(){num = sum = 0, pos.clear(), neg.clear();}
    node(vector<int> a){
        num = sum = 0, pos.clear(), neg.clear();
        for(auto &i : a){
            if(i < 0) neg.pb(i), sum += i;
            else if(i == 0) num++;
            else pos.pb(i);
        }
        reverse(pos.begin(), pos.end());
    }
    int get(int x){
        if(x < neg.size()) return neg[x];
        else if(x < neg.size() + num) return 0;
        return pos[pos.size() - (x - num - neg.size()) - 1];
    }
    void ins(int x){
        if(x == 1) pos.pb(x);
        else neg.pb(x), sum--;
    }
    int siz(){return pos.size() + neg.size() + num;}
}g[N];
int ans[N];
void dfs(int u){
    f[u] = s[u] - '0';
    for(auto &v : G[u]){
        dfs(v), f[u] += f[v];
        if(!g[u].siz()) g[u] = g[v];
        else{
            int len = min(g[u].siz(), g[v].siz());
            vector<int> t(len, 0);
            _rep(i, 0, len - 1) t[i] = g[u].get(i) + g[v].get(i);
            g[u] = node(t);
        }
    }
    g[u].ins(s[u] == '0' ? 1 : -1);
    ans[u] = f[u] + g[u].sum;
}
int main(){
    multitest(){
        read(n), scanf("%s", s + 1);
        _rep(i, 2, n){int u; read(u); G[u].pb(i);}
        dfs(1);
        _rep(i, 1, n) writes(ans[i]); puts("");
        _rep(i, 1, n){
            f[i] = ans[i] = 0, g[i] = node(), G[i].clear();
        }
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 11432kb

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: -100
Memory Limit Exceeded

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: