提交记录 8321


用户 题目 状态 得分 用时 内存 语言 代码长度
bzy noip18f. 【NOIP2018】保卫王国 Accepted 100 279.242 ms 89004 KB C++11 3.52 KB
提交时间 评测时间
2019-02-12 16:42:03 2020-08-01 01:16:18
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

template<typename T,int height,int width>
struct matrix{
    T num[height][width];
    
    T* operator [](int Line)
      { return num[Line]; }
    
    matrix() {  
        for( int i = 0;i < height;i ++ )
            for( int j = 0;j < width;j ++ )
              { num[i][j] = T(); }
    }
    
    template<int widthN>
    matrix<T,height,widthN> operator *
        ( matrix<T,width,widthN> From ) {
            matrix<T,height,widthN> rt;
            for(int i = 0;i < height;i ++)
                for(int j = 0;j < widthN;j ++)
                    for(int k = 0;k < width;k ++)
                      { rt[i][j] = rt[i][j] + num[i][k] * From.num[k][j]; }
            return rt;
        }
        
    void Debug(){
        putchar('\n');
        for( int i = 0;i < height;i ++ )
          { putchar('[');
            for( int j = 0;j < width;j ++ )
                { cout << num[i][j].x << " "; }
            putchar(']');putchar('\n'); }
    }
};

struct VDP{
    LL x;
    VDP() : x( 1e18 ) {}
    VDP(LL _x) : x(_x) {}
    VDP operator +(const VDP& From) const
      { return VDP( min( x,From.x ) ); }
    VDP operator *(const VDP& From) const
      { return VDP( x + From.x ); }
    VDP& operator +=(const LL s) 
      { x += s;return *this; }
    bool operator <(const VDP& From)const
      { return x < From.x; }
};
typedef matrix< VDP,2,2 > dat;

int n,m;
vector<int>to[100005];
LL  data[100005];
LL  dp[100005][2];
int deep[100005];
int fa[100005][20];
dat dt[100005][20];

void dfs(int now,int last){
    deep[now] = deep[last] + 1;
    dp[now][1] = data[now];
    for( auto next : to[now] )if( next != last )
      { dfs( next,now ); fa[next][0] = now;
        dp[now][0] += dp[next][1]; 
        dp[now][1] += min( dp[next][0],dp[next][1] ); }
    for( auto next : to[now] )if( next != last )
      { dt[next][0][1][0] = dp[now][0] - dp[next][1]; 
        dt[next][0][0][1] = dt[next][0][1][1] = dp[now][1] 
          - min( dp[next][0],dp[next][1] ); }
}

void init(){
    for(int i = 1;i <= 19;i ++)
        for(int j = 1;j <= n;j ++)
          { fa[j][i] = fa[ fa[j][i - 1] ][i - 1];
            dt[j][i] = dt[j][i - 1] * dt[ fa[j][i - 1] ][i - 1]; }
}

int main(){
    string op;cin >> n >> m >> op;
    for(int i = 1;i <= n;i ++)cin >> data[i];
    for(int i = 1;i <  n;i ++)
      { int u,v;cin >> u >> v; 
        to[u].push_back(v);
        to[v].push_back(u); }
    dfs( 1,1 );init();
    while( m -- ){
        int a,b,c,d;cin >> a >> b >> c >> d;
        matrix< VDP,1,2 >A;A[0][0] = dp[a][0];A[0][1] = dp[a][1];
        matrix< VDP,1,2 >B;B[0][0] = dp[c][0];B[0][1] = dp[c][1];
        A[0][!b] = 1e18;B[0][!d] = 1e18;
        if( deep[a] < deep[c] )swap( a,c ),swap( A,B );
        if( deep[a] == deep[c] )
          { B = B * dt[c][0];c = fa[c][0]; }
        for( int i = 19;~i;i -- )if( deep[ fa[a][i] ] > deep[c] )
          { A = A * dt[a][i];a = fa[a][i]; }
        for( int i = 19;~i;i -- )if( fa[ fa[a][i] ][0] ^ fa[c][i] )
          { A = A * dt[a][i];a = fa[a][i];
            B = B * dt[c][i];c = fa[c][i]; }
        if( c != fa[a][0] )
          { A = A * dt[a][0];a = fa[a][0];
            B = B * dt[c][0];c = fa[c][0]; }
        B[0][0].x = B[0][0].x - dp[a][1] + A[0][1].x;
        B[0][1].x = B[0][1].x - min( dp[a][0],dp[a][1] ) + min( A[0][0],A[0][1] ).x;
        for( int i =19;~i;i -- )if( fa[c][i] )
          { B = B * dt[c][i];c = fa[c][i]; }
        LL ans = min( B[0][0],B[0][1] ).x;
        if( ans > 1e15 )cout << "-1\n";
        else cout << ans << "\n";
    }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #17.662 ms63 MB + 396 KBAcceptedScore: 4

Testcase #27.258 ms63 MB + 396 KBAcceptedScore: 4

Testcase #37.565 ms63 MB + 396 KBAcceptedScore: 4

Testcase #47.707 ms63 MB + 396 KBAcceptedScore: 4

Testcase #57.624 ms63 MB + 416 KBAcceptedScore: 4

Testcase #67.466 ms63 MB + 416 KBAcceptedScore: 4

Testcase #77.481 ms63 MB + 412 KBAcceptedScore: 4

Testcase #810.575 ms63 MB + 868 KBAcceptedScore: 4

Testcase #910.332 ms63 MB + 868 KBAcceptedScore: 4

Testcase #1010.142 ms63 MB + 768 KBAcceptedScore: 4

Testcase #1110.557 ms63 MB + 768 KBAcceptedScore: 4

Testcase #12210.541 ms86 MB + 940 KBAcceptedScore: 4

Testcase #13210.429 ms86 MB + 940 KBAcceptedScore: 4

Testcase #14226.867 ms86 MB + 744 KBAcceptedScore: 4

Testcase #15227.211 ms86 MB + 748 KBAcceptedScore: 4

Testcase #16227.395 ms86 MB + 748 KBAcceptedScore: 4

Testcase #17279.242 ms86 MB + 940 KBAcceptedScore: 4

Testcase #18201.094 ms77 MB + 892 KBAcceptedScore: 4

Testcase #19201.079 ms77 MB + 892 KBAcceptedScore: 4

Testcase #20202.321 ms81 MB + 952 KBAcceptedScore: 4

Testcase #21202.267 ms81 MB + 952 KBAcceptedScore: 4

Testcase #22215.945 ms81 MB + 756 KBAcceptedScore: 4

Testcase #23259.312 ms81 MB + 948 KBAcceptedScore: 4

Testcase #24259.878 ms81 MB + 952 KBAcceptedScore: 4

Testcase #25259.382 ms81 MB + 964 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:43:17 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠