hdu 5221 Occupation

Occupation

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 269    Accepted Submission(s): 93


Problem Description
Miceren finds a huge country named HY. HY has N cities numbered from 1 to N connected by N  1 bidirectional roads. There exists a path between any two cities.

It can be imagined as a tree with n vertices rooted at vertex 1.

Miceren wants to occupy some cities here. Each city has a value vi . (Notice that the value of a city may be negative. Nevertheless, Miceren wants to occupied this city.)

As some usual stories, someone named Cloud wants to "steal" some cities from Miceren.

At the beginning, Miceren and Cloud don't occupy any city.

In the following Q days, one of three events may happen

1. Miceren will walk from the a-th city to the b-th city and all cities visited in this trip will belong to Miceren. ( 1  a,b  N )

2. Cloud will steal the x-th city. If Miceren occupied the x-th city before, Miceren will lost the control of this city. ( 1  x  N )

3. Miceren will occupy the subtree rooted at x.( 1  x  N )

As Miceren's friend, you must tell Miceren the total value of all cities which belong to Miceren after each day.
 

Input
The first line contains a single integer T , indicating the number of test cases.

Each test case begin with one integer N , indicating the number of cities in HY.

The next line contains N integer Vi , indicating the value of each city.

The next N  1 lines contain the details of the roads. Each line contains two integers u, v meaning that there is a road between cities u and v .

The next line contains one integer Q .

The next Q lines contain the details of event. If the format is "1 a b", it means the first event happened where Miceren walks from a-th city to b-th city. If the format is “2 x”, it means the second event happened where Cloud "steal"s the x-th city. Otherwise the format is “3 x” and the third event happened where Micron occupied the subtree rooted at x.

T is about 100.

1  N,Q  100000.

1000  Vi  1000.

The ratio of test cases with N > 100 is less than 5%.
 

Output
For each test queries, print the answer.
 

Sample Input
  
  
1 10 1 2 3 4 5 6 7 8 9 10 1 2 1 3 2 4 2 5 5 9 5 10 3 6 3 7 3 8 6 1 10 4 1 9 7 2 5 3 4 2 4 1 6 10
 

Sample Output
  
  
21 41 36 36 32 43
Hint
If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
 

Source
 题目分析:
树链剖分模板题,分别是修改子树和修改路径
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define MAX 100007

using namespace std;

struct Edge
{
    int v,next;
}e[MAX<<1];

int head[MAX];
int sum[MAX];
int a[MAX];
int ans,t,n,u,v,q;
int cc;

void add ( int u , int v )
{
    e[cc].v = v;
    e[cc].next = head[u];
    head[u] = cc++;
}

int fa[MAX],siz[MAX],dep[MAX],top[MAX],tid[MAX],Rank[MAX],tim,son[MAX];

void init ( )
{
    memset ( head , -1 , sizeof ( head ) );
    tim = cc = 0;
    memset ( son , -1 , sizeof ( son ) );
}

void dfs1 ( int u = 1 , int p = 0, int d = 0 )
{
    siz[u] = 1 , dep[u] = d;
    fa[u] = p;
    for ( int i = head[u] ; ~i ; i = e[i].next )
    {
        int v = e[i].v;
        if (  v == p ) continue;
        dfs1 ( v , u , d+1 );
        siz[u] += siz[v];
        if ( son[u] == -1 || siz[v] > siz[son[u]] )
           son[u] = v; 
    }    
}

void dfs2 ( int u = 1 , int tp = 1 )
{
    top[u] = tp , tid[u] = ++tim;
    Rank[tid[u]] = u;
    if ( son[u] == -1 ) return;
    dfs2 ( son[u] , tp );
    for ( int i = head[u] ; ~i ; i = e[i].next )
    {
        int v = e[i].v;
        if ( v == fa[u] || v == son[u] ) continue;
        dfs2 ( v , v );
    }
}

int back[MAX],maxn;

/*void dfs3 ( int u = 1 , int p = -1 )
{
    maxn = max ( maxn , tid[u] );
    for ( int i = head[u] ; ~i ; i = e[i].next )
    {
        int v = e[i].v;
        if ( v == p ) continue;
        dfs3 ( v , u );
    }
    back[u] = maxn;
}*/

struct Tree 
{
    int l,r,sum,lazy;
}tree[MAX<<2];

void push_up ( int u )
{
    tree[u].sum = tree[u<<1].sum + tree[u<<1|1].sum;
   // cout << "angry: " << u << " " << tree[u].sum 
   //      << " " << tree[u<<1].sum << " " <<
   //      tree[u<<1|1].sum << endl;
}

void push_down ( int u )
{
    if ( tree[u].lazy )
    {
        tree[u].lazy = 0;
        tree[u<<1].lazy = tree[u<<1|1].lazy = 1;
        int l = tree[u<<1].l , r = tree[u<<1].r;
        tree[u<<1].sum = sum[r] - sum[l-1];
        l = tree[u<<1|1].l , r = tree[u<<1|1].r;
        tree[u<<1|1].sum = sum[r] - sum[l-1];
    }
}

void build ( int u , int l , int r )
{
    tree[u].l = l;
    tree[u].r = r;
    tree[u].sum = 0;
    tree[u].lazy = 0;
    if ( l == r ) return;
    int mid = l + r >>1;
    build ( u<<1 , l , mid );
    build ( u<<1|1 , mid+1 , r );
}

void update ( int u , int x )
{
    int l = tree[u].l , r = tree[u].r;
    int mid = l + r >>1;
    if ( l == r )
    {
        tree[u].sum = 0; 
        return;
    }
    push_down ( u );
    if ( x > mid ) update ( u<<1|1 , x );
    else update ( u<<1 , x );
    push_up ( u ); 
}

void correct ( int u , int left , int right )
{
    int l = tree[u].l , r = tree[u].r;
    int mid = l + r >> 1;
    if ( left <= l && r <= right )
    {
        tree[u].sum = sum[r] - sum[l-1];
        tree[u].lazy = 1;
        return;
    }
    push_down ( u );
    if ( left <= mid && right >= l ) correct ( u<<1 , left , right );
    if ( left <= r && right > mid ) correct ( u<<1|1 , left , right );
    push_up ( u ); 
}

void change1 ( int x , int y )
{
    while ( top[x] != top[y] )
    {
        if ( dep[top[x]] < dep[top[y]] ) swap ( x , y );
        correct ( 1 , tid[top[x]] , tid[x] );
        x = fa[top[x]];
    }
    if ( dep[x] > dep[y] ) swap ( x ,y );
    correct ( 1 , tid[x] , tid[y] );
}

void change2 ( int x )
{
    correct ( 1 , tid[x] , tid[x]+siz[x]-1 );
}


void calcSum ( )
{
    sum[0] = 0;
    for ( int i = 1 ; i <= n ; i++ )
        sum[i] = sum[i-1] + a[Rank[i]];
}

int main ( )
{
    //freopen ( "a.in" , "r" , stdin );
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%d" , &n );
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%d" , &a[i] );
        init();
        for ( int i = 1 ; i < n ; i++ )
        {
            scanf ( "%d%d" , &u , &v );
            add ( u , v );
            add ( v , u );
        }
        dfs1 ();
        dfs2 ();
        maxn = 1;
        //dfs3 ();
        calcSum ( );
        /*cout << "tid : " << endl;
        for ( int i = 1 ; i <= n ; i++ )
            printf ( "%d " , tid[i] );
        cout << endl;
        cout << "sum : " << endl;
        for ( int i = 1 ; i <= n ; i++ )
            printf ( "%d " , sum[i] );
        cout << endl;*/
        build ( 1 , 1 , n );
        scanf ( "%d" , &q );
        int f,x,y;
        while ( q-- )
        {
            scanf ( "%d" , &f );
            if ( f == 1 )
            {
                scanf ( "%d%d" , &x , &y );
                change1 ( x , y );
            }
            if ( f == 2 )
            {
                scanf ( "%d" , &x );
                update ( 1 , tid[x] ); 
            }
            if ( f == 3 )
            {
                scanf ( "%d" , &x );
                change2 ( x );
            }
            printf ( "%d\n" , tree[1].sum );
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值