Codeforces 296C
2015-05-08 23:43
302 查看
写了个简单的AVL,只插入不删除。
当然Tutorial里给出的解法更简单
// Codeforces 296C
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
class AVL{
private:
struct AVLNode{
int begin;
int end;
int height;
AVLNode* left;
AVLNode* right;
AVLNode(int b, int e, int h = 0, AVLNode* l = NULL, AVLNode* r = NULL) :
begin(b), end(e), height(h), left(l), right(r) {}
AVLNode() : left(NULL), right(NULL) {}
};
AVLNode* root;
int* lencnt;
long long max;
int Height(AVLNode* root){
if (root == NULL) return -1;
else return root->height;
}
AVLNode* LL(AVLNode* root){
AVLNode* tmp = root->left;
root->left = tmp->right;
tmp->right = root;
root->height = ((Height(root->left) > Height(root->right)) ?
Height(root->left) : Height(root->right)) + 1;
tmp->height = ((Height(tmp->left) > Height(tmp->right)) ?
Height(tmp->left) : Height(tmp->right)) + 1;
return tmp;
}
AVLNode* LR(AVLNode* root){
root->left = RR(root->left);
root = LL(root);
return root;
}
AVLNode* RL(AVLNode* root){
root->right = LL(root->right);
root = RR(root);
return root;
}
AVLNode* RR(AVLNode* root){
AVLNode* tmp = root->right;
root->right = tmp->left;
tmp->left = root;
root->height = ((Height(root->left) > Height(root->right)) ?
Height(root->left) : Height(root->right)) + 1;
tmp->height = ((Height(tmp->left) > Height(tmp->right)) ?
Height(tmp->left) : Height(tmp->right)) + 1;
return tmp;
}
AVLNode* insert(AVLNode* root, int b, int e){
if (root == NULL) return new AVLNode(b, e);
if (e <= root->begin) root->left = insert(root->left, b, e);
else root->right = insert(root->right, b, e);
if (Height(root->left) - Height(root->right) >= 2){
if (Height(root->left->left) > Height(root->left->right))
root = LL(root);
else root = LR(root);
}
else if (Height(root->right) - Height(root->left) >= 2){
if (Height(root->right->right) > Height(root->right->left))
root = RR(root);
else root = RL(root);
}
root->height = ((Height(root->left) > Height(root->right)) ?
Height(root->left) : Height(root->right)) + 1;
return root;
}
void remove(AVLNode* root){
if (root == NULL) return;
remove(root->left);
remove(root->right);
delete root;
}
public:
AVL(int len){
root = new AVLNode(0, len);
lencnt = new int[len + 1];
memset(lencnt, 0, (len + 1) * sizeof(int));
lencnt[len] = 1;
max = len;
}
~AVL(){
delete[] lencnt;
remove(root);
}
void split(int pos){
// Find the segment containing this position
AVLNode* tmp = root;
while (true){
if (pos > tmp->end) tmp = tmp->right;
else if (pos < tmp->begin) tmp = tmp->left;
else break;
}
// Update the segment counts
lencnt[tmp->end - tmp->begin]--;
lencnt[pos - tmp->begin]++;
lencnt[tmp->end - pos]++;
if (lencnt[max] == 0) while (lencnt[max] == 0) max--;
int e = tmp->end;
tmp->end = pos;
root = insert(root, pos, e);
}
long long getMax() const{
return max;
}
};
int main(){
int w, h, n, i, k;
cin >> w >> h >> n;
AVL row(w);
AVL col(h);
char dir;
int pos;
for (k = 1; k <= n; k++){
cin >> dir;
if (dir == 'H'){
cin >> pos;
col.split(pos);
}
else {
cin >> pos;
row.split(pos);
}
cout << row.getMax() * col.getMax() << endl;
}
return 0;
}
当然Tutorial里给出的解法更简单
// Codeforces 296C
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
class AVL{
private:
struct AVLNode{
int begin;
int end;
int height;
AVLNode* left;
AVLNode* right;
AVLNode(int b, int e, int h = 0, AVLNode* l = NULL, AVLNode* r = NULL) :
begin(b), end(e), height(h), left(l), right(r) {}
AVLNode() : left(NULL), right(NULL) {}
};
AVLNode* root;
int* lencnt;
long long max;
int Height(AVLNode* root){
if (root == NULL) return -1;
else return root->height;
}
AVLNode* LL(AVLNode* root){
AVLNode* tmp = root->left;
root->left = tmp->right;
tmp->right = root;
root->height = ((Height(root->left) > Height(root->right)) ?
Height(root->left) : Height(root->right)) + 1;
tmp->height = ((Height(tmp->left) > Height(tmp->right)) ?
Height(tmp->left) : Height(tmp->right)) + 1;
return tmp;
}
AVLNode* LR(AVLNode* root){
root->left = RR(root->left);
root = LL(root);
return root;
}
AVLNode* RL(AVLNode* root){
root->right = LL(root->right);
root = RR(root);
return root;
}
AVLNode* RR(AVLNode* root){
AVLNode* tmp = root->right;
root->right = tmp->left;
tmp->left = root;
root->height = ((Height(root->left) > Height(root->right)) ?
Height(root->left) : Height(root->right)) + 1;
tmp->height = ((Height(tmp->left) > Height(tmp->right)) ?
Height(tmp->left) : Height(tmp->right)) + 1;
return tmp;
}
AVLNode* insert(AVLNode* root, int b, int e){
if (root == NULL) return new AVLNode(b, e);
if (e <= root->begin) root->left = insert(root->left, b, e);
else root->right = insert(root->right, b, e);
if (Height(root->left) - Height(root->right) >= 2){
if (Height(root->left->left) > Height(root->left->right))
root = LL(root);
else root = LR(root);
}
else if (Height(root->right) - Height(root->left) >= 2){
if (Height(root->right->right) > Height(root->right->left))
root = RR(root);
else root = RL(root);
}
root->height = ((Height(root->left) > Height(root->right)) ?
Height(root->left) : Height(root->right)) + 1;
return root;
}
void remove(AVLNode* root){
if (root == NULL) return;
remove(root->left);
remove(root->right);
delete root;
}
public:
AVL(int len){
root = new AVLNode(0, len);
lencnt = new int[len + 1];
memset(lencnt, 0, (len + 1) * sizeof(int));
lencnt[len] = 1;
max = len;
}
~AVL(){
delete[] lencnt;
remove(root);
}
void split(int pos){
// Find the segment containing this position
AVLNode* tmp = root;
while (true){
if (pos > tmp->end) tmp = tmp->right;
else if (pos < tmp->begin) tmp = tmp->left;
else break;
}
// Update the segment counts
lencnt[tmp->end - tmp->begin]--;
lencnt[pos - tmp->begin]++;
lencnt[tmp->end - pos]++;
if (lencnt[max] == 0) while (lencnt[max] == 0) max--;
int e = tmp->end;
tmp->end = pos;
root = insert(root, pos, e);
}
long long getMax() const{
return max;
}
};
int main(){
int w, h, n, i, k;
cin >> w >> h >> n;
AVL row(w);
AVL col(h);
char dir;
int pos;
for (k = 1; k <= n; k++){
cin >> dir;
if (dir == 'H'){
cin >> pos;
col.split(pos);
}
else {
cin >> pos;
row.split(pos);
}
cout << row.getMax() * col.getMax() << endl;
}
return 0;
}
相关文章推荐
- Codeforces 296C
- codeforces 296C
- Codeforces 3D Least Cost Bracket Sequence --- 贪心
- codeforces 573B B. Bear and Blocks(线段树+dp)
- Codeforces 676C Vasya and String (两点法)
- Codeforces 733C (模拟,贪心)
- codeforces——810A——Straight «A»
- Codeforces 871C 872E Points, Lines and Ready-made Titles
- Codeforces Div2 243AB
- codeforces 392 B. Tower of Hanoi(汉诺塔 DP)
- codeforces 414B (DP)
- 【codeforces】378BParade(简单思维+英语阅读理解-.-)
- codeforces 810C Do you want a date? 思维
- Codeforces 659F Polycarp and Hay【思维+并查集+Bfs】
- Codeforces 279C
- Codeforces 548E Mike and Foam(容斥)
- 【CodeForces】638A - Home Numbers(水)
- Codeforces 733e
- CodeForces - 581B - Luxurious Houses 逆序处理&水
- [Codeforces] #441 div.2