| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我过了discuss里所有的数据可是还是WA贴代码
#include <iostream>
#include <cstdio>
using namespace std;
struct TreeNode
{
long long col;
bool update;
TreeNode *left, *right;
};
inline int color(int col)
{
return 1 << (col - 1);
}
inline void BuildTree(TreeNode *&root)
{
root = new TreeNode;
root->col = color(1);
root->update = true;
root->left = NULL;
root->right = NULL;
}
void Update(TreeNode *&root, int L, int R, int l, int r, int NewColor)
{
if ((L <= l) && (r <= R))
{
root->col = color(NewColor);
root->update = true;
return;
}
if ((L > r) || (R < l))
{
return ;
}
if (root->update)
{
if (!(root->left))
{
BuildTree(root->left);
}
root->left->col = root->col;
if (!(root->right))
{
BuildTree(root->right);
}
root->right->col = root->col;
root->update = false;
int mid = (l + r) >> 1;
if (L <= mid)
{
Update(root->left, L, R, l, mid, NewColor);
}
if (R > mid)
{
Update(root->right, L, R, mid + 1, r, NewColor);
}
root->col = root->left->col | root->right->col;
return;
}
else
{
int mid = (l + r) >> 1;
if (L <= mid)
{
Update(root->left, L, R, l, mid, NewColor);
}
if (R > mid)
{
Update(root->right, L, R, mid + 1, r, NewColor);
}
root->col = root->left->col | root->right->col;
return;
}
}
long long Search(TreeNode *&root, int L, int R, int l, int r)
{
if ((L <= l) && (r <= R))
{
return root->col;
}
if ((L > r) || (R < l))
{
return 0;
}
if (root->update)
{
if (!(root->left))
{
BuildTree(root->left);
}
root->left->col = root->col;
if (!(root->right))
{
BuildTree(root->right);
}
root->right->col = root->col;
root->update = false;
int mid = (l + r) >> 1;
long long ret = 0;
if (L <= mid)
{
ret = ret | Search(root->left, L, R, l, mid);
}
if (R > mid)
{
ret = ret | Search(root->right, L, R, mid + 1, r);
}
root->col = root->left->col | root->right->col;
return ret;
}
else
{
int mid = (l + r) >> 1;
long long ret = 0;
if (L <= mid)
{
ret = ret | Search(root->left, L, R, l, mid);
}
if (R > mid)
{
ret = ret | Search(root->right, L, R, mid + 1, r);
}
root->col = root->left->col | root->right->col;
return ret;
}
return 0;
}
TreeNode *root = NULL;
int main()
{
int L, T, O;
scanf("%d%d%d", &L, &T, &O);
BuildTree(root);
for (int i = 1; i <= O; i++)
{
fflush(stdin);
char operate;
int A, B, C;
scanf("%c%d%d", &operate, &A, &B);
if (A > B)
{
swap(A, B);
}
if (operate == 'C')
{
scanf("%d", &C);
Update(root, A, B, 1, L, C);
}
else
{
long long ret = Search(root, A, B, 1, L);
int ans = 0;
while (ret > 0)
{
if ((ret & 1) == 1)
{
ans++;
}
ret >>= 1;
}
cout << ans << endl;
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator