2014北京网络预选赛1008(线段树区间操作)HDU5039

Hilarity

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 211    Accepted Submission(s): 51


Problem Description
After June 1st, elementary students of Ted Land are still celebrating "The Sacred Day of Elementary Students”. They go to the streets and do some elementary students stuff. So we call them "elementary students". There are N cities in Ted Land. But for simplicity, the mayor Matt only built N - 1 roads so that all cities can reach each other. Some of the roads are occupied by the "elementary students". They will put an celebration hat on everyone who goes through the road without one. But if someone goes through the road with a celebration hat on his head, "elementary students" will steal the hat for no reason. Since Matt doesn’t have a celebration hat, he wants to know how many different paths in his land that he can end up with a hat. Two paths are considered to be different if and only if they have different start city or end city. As the counsellor of the mayor Matt, you have to answer this question for him. The celebration elementary students are not stable: sometimes a new crowd of elementary students go to an empty road; sometimes the elementary students on a road will go back home and remain the road empty. Matt will send you the monitor about the change of elementary students on road and ask you the question above. You will be fired if you answer wrong.
 

Input
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

For each test case, the first line contains N (1<=N<=30000) describing the number of cities.

Then N lines follow. The ith line of these contains the name of the ith city, it’s a string containing only letters and will not be longer than 10.

The following N - 1 lines indicate the N - 1 edges between cities. Each of these lines will contain two strings for the cities’ name and a integer for the initial status of the edge (0 for empty, 1 for crowded).

Then an integer M (1<=M<=60000) describes the number of queries. There are two kinds of query:

● "M i" means the status changing of the ith (starting from 1) road (0 to 1, 1 to 0);
● "Q" means that Matt asks you the number of different paths.
 

Output
For each test case, first output one line “Case #x:”, where x is the case number (starting from 1).

Then for each “Q” in input, output a line with the answer.
 

Sample Input
   
   
1 5 a b c d e a b 1 b c 0 c d 1 d e 1 7 Q M 1 Q M 3 Q M 4 Q
 

Sample Output
   
   
Case #1: 12 8 8 0

题意:给出一棵n个点的树,边权为0或1,可以修改任何一条边的值,查询整棵树上有多少对u,v之间路径的异或值为1

思路:首先根据异或的性质不难发现求xor(u,v)=xor(1,u)^xor(1,v)

            所以可以用线段树维护每个xor(1,u)

            1.修改操作,假如要修改u与其父亲的边,则以u为根的子树的xor全都改变了,即取反

               所以可以先将整棵树的用DFS序列化,也就是所谓的树形转线性,每个节点及其子树都是一段连续的线段

            2.查询,直接查询整个线段就好了

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
        #include 
        
          #include 
         
           using namespace std; const int MAXN = 30010; map 
          
            mp; int head[MAXN]; int edge[MAXN<<1]; int next[MAXN<<1]; int ww[MAXN<<1]; int vin[MAXN]; int vout[MAXN]; int a[MAXN]; int esz; int dfs_clock; void init() { memset(head,-1,sizeof(head)); esz=0;dfs_clock=0;a[0]=0; } void add(int u,int v,int w) { edge[esz]=v; ww[esz]=w; next[esz]=head[u]; head[u]=esz++; } void dfs(int u,int p,int w) { vin[u]=++dfs_clock; a[dfs_clock]=a[vin[p]]^w; for(int i=head[u];i!=-1;i=next[i]){ int v=edge[i]; if(v==p)continue; dfs(v,u,ww[i]); } vout[u]=dfs_clock; } typedef __int64 ll; #define lson l,m,o<<1 #define rson m+1,r,o<<1|1 int sum0[MAXN<<2]; int sum1[MAXN<<2]; int cnt0[MAXN<<2]; int cnt1[MAXN<<2]; int rev[MAXN<<2]; void pushup(int o) { int temp=cnt0[o<<1]*cnt1[o<<1|1]+cnt1[o<<1]*cnt0[o<<1|1]; sum0[o]=sum0[o<<1]+sum0[o<<1|1]+temp; sum1[o]=sum1[o<<1]+sum1[o<<1|1]+temp; cnt0[o]=cnt0[o<<1]+cnt0[o<<1|1]; cnt1[o]=cnt1[o<<1]+cnt1[o<<1|1]; } void sw(int o) { swap(sum1[o],sum0[o]); swap(cnt1[o],cnt0[o]); } void pushdown(int o) { if(rev[o]) { rev[o<<1]^=1; rev[o<<1|1]^=1; sw(o<<1); sw(o<<1|1); rev[o]=0; } } void build(int l,int r,int o) { rev[o]=0; if(l==r){ sum1[o]=sum0[o]=0; cnt0[o]=cnt1[o]=0; a[l] ? (cnt1[o]=1,sum1[o]=1) : (cnt0[o]=1,sum0[o]=1); return; } int m=l+r>>1; build(lson); build(rson); pushup(o); } void update(int l,int r,int o,int L,int R) { if(L<=l&&r<=R){ rev[o]^=1; sw(o); return; } pushdown(o); int m=l+r>>1; if(L<=m)update(lson,L,R); if(m 
           
             >s; mp[s]=i; } for(int i=1;i 
            
              >s1>>s2>>w; add(mp[s1],mp[s2],w); add(mp[s2],mp[s1],w); } dfs(1,0,0); build(2,n,1); int m; scanf("%d",&m); printf("Case #%d:\n",++tt); for(int i=0;i 
              
             
            
           
          
        
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值