답안 #242340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
242340 2020-06-27T08:59:28 Z dwsc Magic Tree (CEOI19_magictree) C++14
48 / 100
299 ms 430056 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
int n,m,k;
vector<int> adj[100010];
int ripe[100010];
int weight[100010];
int memo[100010][1010];
int dp(int u,int j){
    if (j == 0) return 0;
    if (memo[u][j] != -1) return memo[u][j];
    int s = 0;
    for (int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        s += dp(v,j);
    }
    if (j == ripe[u]) s += weight[u];
    if (weight[u] != 0)return memo[u][j] = max(s,dp(u,j-1));
    else return memo[u][j] = s;
}
struct node{
    int s,e,m,v;
    node *l,*r;
    node (int _s,int _e){
        s = _s;
        e = _e;
        m = (s+e)/2;
        v = 0;
        if (s != e){
            l = new node(s,m);
            r = new node(m+1,e);
        }
    }
    void up(int x,int v1){
        if (s == e) {v = v1;return;}
        if (x <= m) l->up(x,v1);
        else r->up(x,v1);
        v = max(l->v,r->v);
    }
    int rmq(int x,int y){
        if (s == x && e == y) return v;
        if (y <= m) return l->rmq(x,y);
        if (x > m) return r->rmq(x,y);
        return max(l->rmq(x,m),r->rmq(m+1,y));
    }
}*root;
main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> k;
    int isline = 1;
    for (int i = 2; i <= n; i++){
        int p;
        cin >> p;
        if (i-1 != p) isline = 0;
        adj[p].push_back(i);
    }
    vector<int> temp;
    temp.push_back(0);
    for (int i = 0; i < m; i++){
        int v,d,w;
        cin >> v >> d >> w;
        ripe[v] = d;
        weight[v] = w;
        temp.push_back(d);
    }
    sort(temp.begin(),temp.end());
    temp.erase(unique(temp.begin(),temp.end()),temp.end());
    for (int i = 1; i <= n; i++) ripe[i] = lower_bound(temp.begin(),temp.end(),ripe[i])-temp.begin();
    //for (int i = 1; i <= n; i++) cout << ripe[i] << "\n";
    if (k <= 20){
    for (int i = 1; i<= n; i++){
        for (int j = 0; j < temp.size(); j++){
            memo[i][j] = -1;
        }
    }
    cout << dp(1,temp.size()-1);
    }
    else if (isline){
        int ans = 0;
        root = new node(0,temp.size()-1);
        for (int i = 1; i <= n; i++){
            if (!weight[i]) continue;
            int q = root->rmq(ripe[i],temp.size()-1);
            ans = max(ans,q+weight[i]);
            root->up(ripe[i],q+1);
        }
        cout << ans;
    }
    else{
        int ans = 0;
        for (int i = 1; i <= n; i++) ans += weight[i];
        cout << ans;
    }
}

Compilation message

magictree.cpp: In function 'long long int dp(long long int, long long int)':
magictree.cpp:14:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < adj[u].size(); i++){
                     ~~^~~~~~~~~~~~~~~
magictree.cpp: At global scope:
magictree.cpp:48:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
magictree.cpp: In function 'int main()':
magictree.cpp:74:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < temp.size(); j++){
                         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 6 ms 2816 KB Output is correct
7 Correct 6 ms 2816 KB Output is correct
8 Correct 6 ms 2816 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 6272 KB Output is correct
2 Correct 40 ms 6524 KB Output is correct
3 Correct 75 ms 6380 KB Output is correct
4 Correct 66 ms 6636 KB Output is correct
5 Correct 73 ms 6380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 7 ms 2816 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 84 ms 16236 KB Output is correct
5 Correct 80 ms 16108 KB Output is correct
6 Correct 130 ms 16108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 242 ms 410096 KB Output is correct
2 Correct 231 ms 408556 KB Output is correct
3 Correct 232 ms 412648 KB Output is correct
4 Correct 213 ms 408944 KB Output is correct
5 Correct 227 ms 416108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 6 ms 2816 KB Output is correct
7 Correct 6 ms 2816 KB Output is correct
8 Correct 6 ms 2816 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 299 ms 424176 KB Output is correct
11 Correct 271 ms 416492 KB Output is correct
12 Correct 258 ms 426724 KB Output is correct
13 Correct 279 ms 423160 KB Output is correct
14 Correct 266 ms 430056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 3456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 6 ms 2816 KB Output is correct
7 Correct 6 ms 2816 KB Output is correct
8 Correct 6 ms 2816 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 7 ms 2816 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 84 ms 16236 KB Output is correct
14 Correct 80 ms 16108 KB Output is correct
15 Correct 130 ms 16108 KB Output is correct
16 Correct 299 ms 424176 KB Output is correct
17 Correct 271 ms 416492 KB Output is correct
18 Correct 258 ms 426724 KB Output is correct
19 Correct 279 ms 423160 KB Output is correct
20 Correct 266 ms 430056 KB Output is correct
21 Incorrect 17 ms 3840 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 6 ms 2816 KB Output is correct
6 Correct 6 ms 2816 KB Output is correct
7 Correct 6 ms 2816 KB Output is correct
8 Correct 6 ms 2816 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 40 ms 6272 KB Output is correct
11 Correct 40 ms 6524 KB Output is correct
12 Correct 75 ms 6380 KB Output is correct
13 Correct 66 ms 6636 KB Output is correct
14 Correct 73 ms 6380 KB Output is correct
15 Correct 6 ms 2816 KB Output is correct
16 Correct 7 ms 2816 KB Output is correct
17 Correct 6 ms 2816 KB Output is correct
18 Correct 84 ms 16236 KB Output is correct
19 Correct 80 ms 16108 KB Output is correct
20 Correct 130 ms 16108 KB Output is correct
21 Correct 242 ms 410096 KB Output is correct
22 Correct 231 ms 408556 KB Output is correct
23 Correct 232 ms 412648 KB Output is correct
24 Correct 213 ms 408944 KB Output is correct
25 Correct 227 ms 416108 KB Output is correct
26 Correct 299 ms 424176 KB Output is correct
27 Correct 271 ms 416492 KB Output is correct
28 Correct 258 ms 426724 KB Output is correct
29 Correct 279 ms 423160 KB Output is correct
30 Correct 266 ms 430056 KB Output is correct
31 Incorrect 10 ms 3456 KB Output isn't correct
32 Halted 0 ms 0 KB -