1. 程式人生 > >[HDU3966]Aragorn's Story

[HDU3966]Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N

N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C
1 C1
and C 2 C2 . Then, for C 1
C1
, C 2 C2 and all camps on the path from C 1 C1 to C 2 C2 , they will increase or decrease K K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.

Input
Multiple test cases, process to the end of input.

For each case, The first line contains three integers N , M , P N, M, P which means there will be N ( 1 N 50000 ) N(1 ≤ N ≤ 50000) camps, M ( M = N 1 ) M(M = N-1) edges and P ( 1 P 100000 ) P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1 1 .

The next line contains N N integers A 1 , A 2 , . . . A N ( 0 A i 1000 ) A_1, A_2, ...A_N(0 ≤ A_i ≤ 1000) , means at first in c a m p i camp-i has A i A_i enemies.

The next M M lines contains two integers u and v for each, denotes that there is an edge connects c a m p u camp-u and c a m p v camp-v .

The next P lines will start with a capital letter ‘I’, ‘D’ or ‘Q’ for each line.

‘I’, followed by three integers C 1 C1 , C 2 C2 and K ( 0 K 1000 ) K( 0≤K≤1000) , which means for camp C 1 C1 , C 2 C2 and all camps on the path from C 1 C1 to C 2 C2 , increase K K soldiers to these camps.

‘D’, followed by three integers C 1 C1 , C 2 C2 and K ( 0 K 1000 ) K( 0≤K≤1000) , which means for camp C 1 C1 , C 2 C2 and all camps on the path from C 1 C1 to C 2 C2 , decrease K K soldiers to these camps.

‘Q’, followed by one integer C C , which is a query and means Aragorn wants to know the number of enemies in camp C C at that time.

Output

For each query, you need to output the actually number of enemies in the specified camp.

Sample Input

3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1 
Q 3

Sample Output

7
4
8

Hint

1.The number of enemies may be negative.
2.Huge input, be careful. 

Source
2011 Multi-University Training Contest 13 - Host by HIT

題解:
裸的樹鏈剖分+區間修改

#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int n,m,p;
struct edge{
    int to,nt;
}e[1000004];
struct tree{
    int l,r,tag,w;
}tr[200004];
int ne,cnt,h[50004],a[50004];
int belong[50004],sz[50004],pos[50004],fa[50004],dep[50004];
void add(int u,int v){
     e[++ne].to=v;e[ne].nt=h[u];
     h[u]=ne;
}
void push(int k){
     if(tr[k].tag==0)return ;
     int x=tr[k].tag;tr[k].tag=0;
     tr[k<<1].tag+=x;
     tr[k<<1|1].tag+=x;
     tr[k<<1].w+=x;
     tr[k<<1|1].w+=x;
}
void build(int k,int l,int r){
     tr[k].l=l;tr[k].r=r;
     tr[k].w=0;tr[k].tag=0;
     if(l==r)return;
     int mid=(l+r)>>1;
     build(k<<1,l,mid);
     build(k<<1|1,mid+1,r);
}
void modify(int k,int a,int b,int v){
     int l=tr[k].l,r=tr[k].r;
     if(l==a&&r==b){
        tr[k].tag+=v;
        tr[k].w+=v;
        return ;
     }
     push(k);
     int mid=(l+r)>>1;
     if(b<=mid)modify(k<<1,a,b,v);
     else if(a>mid)modify(k<<1|1,a,b,v);
     else{
        modify(k<<1,a,mid,v);
        modify(k<<1|1,mid+1,b,v);
     }
}
int query(int k,int x){
    int l=tr[k].l,r=tr[k].r;
    if(l==x&&r==x)return tr[k].w;
    push(k);
    int mid=(l+r)>>1;
    if(x<=mid)return query(k<<1,x);
    else return query(k<<1|1,x);
}
void dfs1(int x){
     sz[x]=1;
     for(int i=h[x];i;i=e[i].nt){
         if(e[i].to==fa[x])continue;
         fa[e[i].to]=x;
         dep[e[i].to]=dep[x]+1;
         dfs1(e[i].to);
         sz[x]+=sz[e[i].to];
     }
     return ;
}
void dfs2(int x,int chain){
     pos[x]=++cnt;
     belong[x]=chain;
     int ntc=0;
     for(int i=h[x];i;i=e[i].nt){
         if(e[i].to==fa[x])continue;
         if(sz[ntc]<sz[e[i].to])ntc=e[i].to;
     }
     if(ntc==0)return ;
     dfs2(ntc,chain);
     for(int i=h[x];i;i=e[i].nt){
         if(e[i].to==fa[x])continue;
         if(e[i].to!=ntc)dfs2(e[i].to,e[i].to);
     }
}
void solve(int x,int y,int v){
     while(belong[x]!=belong[y]){
        if(dep[belong[x]]<dep[belong[y]])swap(x,y);
        modify(1,pos[belong[x]],pos[x],v);
        x=fa[belong[x]];
     }
     if(pos[x]>pos[y])swap(x,y);
     modify(1,pos[x],pos[y],v);
     return ;
}
int w33ha(){
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    cnt=0;ne=0;sz[0]=0;
    memset(h,0,sizeof(h));
    for(int i=1;i<=m;i++){
        int u,v;scanf("%d%d",&u,&v);
        add(u,v);add(v,u);
    }
    build(1,1,n);
    dep[1]=1
            
           

相關推薦

[HDU3966]Aragorn&#39;s Story

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description Our protagonist is the handsome human pr

HDU-3966 Aragorn&#39;s Story(樹鏈剖分+線段樹)

real letter 們的 等等 then 需要 family sea inpu Aragorn‘s Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot

hdu 3966 Aragorn&#39;s Story

ret bsp panel change it is find rom multi ini Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S

HDU Aragorn&#39;s Story -樹鏈剖分

next font scanf ont change desc one proc php HDU Aragorn‘s Story Problem Descript

Aragorn&#39;s Story 樹鏈剖分+線段樹 && 樹鏈剖分+樹狀數組

date shu 更新 none ++ span display struct mem Aragorn‘s Story 來源:http://120.78.128.11/Problem.jsp?pid=2710 來源:http://acm.hdu.edu.cn/showpro

HDU-3966 Aragorn&#39;s Story

樹鏈剖分+樹狀陣列 模板題 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespac

Aragorn&#39;s Story 【HDU - 3966】【樹鏈剖分】

題目連結 樹鏈剖分的學習筆記(更新中)   這道題所給的Hint好有迷惑性,它跟我們說注意士兵的數量可能為負數,我的第一反應是,士兵的數量是不是不能為負數,那麼我們是不是要做出些什麼調整,然而,語文不好的我看了Discuss才知道說的是:士兵的數量可以為負數。這樣也好,題目就變

hdu 3966 Aragorn&#39;s Story : 樹鏈剖分 O(nlogn)建樹 O((logn)²)修改與查詢

esp style wow tree urn max return down data 1 /** 2 problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 3 裸板 4 **/ 5

HDU - 3966 Aragorn&#39;s Story

top nbsp || span sca .org ini lan space 題目傳送門:HDU - 3966 Aragorn‘s Story 題目大意: 存在一個樹,樹上每個節點為一個陣營,陣營中存在敵人,現在要進行以下操作 I C1 C2 K :將陣營C

poj 3207 Ikki&#39;s Story IV - Panda&#39;s Trick 2-sat

name continue ios sat code bug != sin debug 題意: 問給你n個點和m對點 如果每對點必須一個在圓裏一個在圓外是否可行 思路: 我們對每個點建造虛點 進行四次連邊 然後跑一遍2-sat就可以了 1 #in

POJ3207 Ikki&#39;s Story IV - Panda&#39;s Trick 【2-sat】

game mos decide rcu Circul 區間 not following have 題目 liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweep

poj 3207 Ikki&#39;s Story IV - Panda&#39;s Trick【2-SAT+tarjan】

注意到相交的點對一定要一裡一外,這樣就變成了2-SAT模型 然後我建邊的時候石樂志,實際上不需要考慮這個點對的邊是正著連還是反著連,因為不管怎麼連,能相交的總會相交,所以直接判相交即可 然後tarjan縮點,再判是否合法即可 #include<iostream> #include<cstd

3966 Aragorn's Story(樹鏈剖分模板題)

Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who w

Aragorn's Story HDU

樹鏈剖分模板題目。 樹鏈剖分,就是先選定好根節點,由根節點往下,沿著子樹節點最多的節點往下,將其拆成鏈,再由鏈組成線段樹的資料結構 #pragma comment(linker,"/STACK:100000000,100000000") #include <iostr

UVA - 434 Matty&#39;s Blocks

mes [0 () block += tty scan 一個 ems 題意:給你正視和側視圖,求最多多少個,最少多少個 思路:貪心的思想。求最少的時候:由於能夠想象著移動,盡量讓兩個視圖的重疊。所以我們統計每一個視圖不同高度的個數。然後計算。至於的話。就是每次拿正視圖的

POJ 2488:A Knight&#39;s Journey

graph for erp 技術分享 rpe one star void get A Knight‘s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29241

Hoeffding&#39;s inequality

oba cad val ref earch ons fyi them nat Let $\{Y_i: i\in J\}$ be zero mean independent complex-valued random variables satisfying $|Y_i|\l

Xcode 真機調試報錯:This application&#39;s application-identifier entitleme

報錯 調試 win cati app itl ati 刪除 allow This application‘s application-identifier entitlement does not match that of the installed applicatio

[UVALive7261]A - Xiongnu&#39;s Land (二分)

while continue 大於 並且 輸出結果 net lan include != 題目鏈接:https://vjudge.net/problem/UVALive-7261 題意略 三個步驟: 1.二分滿足左邊綠洲面積大於等於右邊綠洲面積,並且使左邊面積盡可能大的分割

解題報告 之 HDU5288 OO&#39; s Sequence

bold repr frame roman efi int tom relative 記錄 解題報告 之 HDU5288 OO‘ s Sequence Description OO has got a array A of size n ,defined