您的位置:首页 > 理论基础 > 数据结构算法

HDU3031及左偏树模板

2015-01-28 16:25 274 查看
//
// main.cpp
// leftist-tree
//
// Created by 蘇與軒 on 15/1/28.
// Copyright (c) 2015年 蘇與軒. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#define rep(i,a,b) for (int i=a;i<(b+1);i++)
#define Rep(i,a,b) for (int i=a;i>=b;i--)
#define mid ((l+r)>>1)
#define lson (k<<1)
#define rson (k<<1|1)
using namespace std;
#define M 1000010
struct P{
int l,r,dis,v;
}tree[M];
int f[110];
int Merge(int x,int y) {
if (!x) return y;
if (!y) return x;
if (tree[x].v<tree[y].v) swap(x,y);//big treap
tree[x].r=Merge(tree[x].r,y);
if (tree[tree[x].l].dis<tree[tree[x].r].dis) swap(tree[x].l,tree[x].r);
if (tree[x].r) tree[x].dis=tree[tree[x].r].dis+1;
else tree[x].dis=0;
return x;
}
int Pop(int x) {
int l=tree[x].l;
int r=tree[x].r;
tree[x].l=tree[x].r=tree[x].dis=0;
return Merge(l,r);
}
int T,n,m,p[110],pp,x,root[2],sum[110],tot,sheep,wolf;
char s[5];
int main(int argc, const char * argv[]) {
scanf("%d",&T);
sheep=0;wolf=0;
while (T--) {
scanf("%d%d",&n,&m);
memset(f,0,sizeof f);
rep(i,1,m) scanf("%d",&p[i]);
int tot=1;
rep(i,1,m) rep(j,1,p[i]){
scanf("%d",&pp);
tree[tot].l=tree[tot].r=tree[tot].dis=0;
tree[tot].v=pp;
f[i]=Merge(f[i],tot);
tot++;
}
memset(sum,0,sizeof sum);
root[0]=root[1]=0;
rep(i,0,n-1) {
scanf("%s",s);
switch (s[0]) {
case 'T':
scanf("%d",&x);
root[i&1]=Merge(f[x],root[i&1]);
sum[i&1]+=p[x];
f[x]=0;
p[x]=0;
break;
case 'C':
if (tree[root[0]].v>tree[root[1]].v){
root[0]=Merge(root[0],root[1]);
sum[0]+=sum[1];
root[1]=0;
sum[1]=0;
}else{
root[1]=Merge(root[0],root[1]);
sum[1]+=sum[0];
root[0]=0;
sum[0]=0;
}
break;
case 'L':
root[i&1]=Pop(root[i&1]);
sum[i&1]--;
break;
case 'A':
scanf("%d",&x);
tree[root[i&1]].v+=x;
break;
case 'E':
scanf("%d",&x);
int cur=Pop(root[i&1]);
tree[cur].v=x;
root[i&1]=Merge(cur,root[i&1]);
break;
}
}
printf("%d:%d\n",sum[0],sum[1]);
}
if (sum[0]>sum[1]) {
wolf++;
}else sheep++;
if (sheep>wolf) printf("I will be back!!\n");
else printf("Hahaha...I win!!\n");
return 0;
}


关于左偏树的教程很多大牛的博客和网上都有,就不累述了,直接上代码!
题意:

Problem Description

That's a question. Now Happy (Xi Yangyang) has been caught by Wolffy (Hui Tailang). As Wolffy is busy preparing the big meal, a good idea comes to Happy. He proposes a game that only Wolffy had won, he can eat Happy. Wolffy always believes he is the cleverest
one, so they reach a consensus. And they both agree with Wolnie (Hong Tailang) when the referee. A theater will be beat to die by Wolnie's pan.



The game is defined as follow.

There are multiple test cases.

In each case there are R (R < 10) rounds of the game, R is an odd number to guarantee that there must be a winner in the end.

In each round: There is a pile of n (10 <= n <= 200) Special-cards and m (1 <= m <= 100) piles of Point-card on the table. The Point-card piles are ordered from 1 to m. Wolffy and Happy take turns to get one card from the top of Special-cards pile. Wolffy always
takes first in the game. When all the Special-cards have been taken, the round is over and the one with more cards in the hands gains one point. If there is a tie, Wolffy gains one point.(Wolffty and Happy both have 0 point before the game).

There are 5 kinds of Special-cards besides the Point-card in the game.

0) Point-card: a card with a point X (1 <= X <= 2000000).

1) Challenge-card: no matter who takes this card, they both take one card with the maximum point from their own hands. After a comparison, if Happy's card has a larger point, He takes all the Wolffy's in-hands cards, vice versa; If there is a tie no more operation.

2) Loss-card: the one who takes this card, He must throw a card with the maximum point.

3) Add-card: a card with P point, the one who gets this card will make the card with maximum point P point larger, i.e. if a Point-card with X point is the maximum, its point will change to X + P. An Add-card can only work on one Point-card.

4) Exchange-card: a card with Q point. The one who gets this card must change one maximum-point card's point to Q.

5) Take-card: a card with a integer K, indicates one can get the all the cards of Kth Point-card pile. In one round no two Take-card have the same K.

You can assume that when one gets the Loss-card, Add-card, Exchange-card, He has at least one card in the hands, when one gets a Challenge-card, they both have at least one card in the hands.

 

Input

Input

For each test case, the first line of input is an integer R, indicates the number of rounds:

Line 2: two integers n indicates the number of Special-cards, m indicates the number of Point-card piles.

Line 3: a line of m integers. The ith number Pi (1 <= Pi <= 10000)indicates the number cards of ith Point-card pile.

For the next m lines, ith line contains Pi numbers indicate every Point-card's point of ith Point-card pile.

The next n lines, in each line, there are five kinds of input, indicate Special-cards by the order of "from top to bottom".

1) T K: indicates one gets a Take-card, and He can get Kth Point-card pile(1 <= K <= m).

2) C: indicates one gets a Challenge card.

3) L: indicates one gets a Loss card.

4) A P: indicates one gets an Add card with P point (1 <= P <= 30).

5) E Q: indicates one gets an Exchange card with Q point (1 <= Q <= 2000000).

 

Output

For each round you should print A:B in a line. A indicate the number of left cards of Wolffy, B indicates the number of left cards of Happy. At the end of game, if Wolffy gains more points, print "Hahaha...I win!!", else print "I will be back!!".

 

Sample Input

3
5 3
3 3 3
10 11 2
7 4 12
4 2 9
T 1
T 2
A 7
T 3
C
6 3
2 2 2
1 4
5 2
4 2
T 2
T 1
L
A 2
T 3
C
5 3
2 2 2
1 3
4 2
5 2
T 2
T 1
E 3
A 1
L

 

Sample Output

9:0
0:5
1:2
I will be back!!

 

Source

2009 Multi-University Training Contest 13 - Host
by HIT 

 

Recommend

gaojie   |   We have carefully selected several similar problems for you:  3032 3033 3034 3035 3038 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 数据结构