Codeforces Round #343 (Div. 2) B. Far Relative’s Problem (线段树+区间更新+单点查询)

B. Far Relative’s Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.

Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.

Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends.

Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day biinclusive.

Output

Print the maximum number of people that may come to Famil Door's party.

Examples
input
4
M 151 307
F 343 352
F 117 145
M 24 128
output
2
input
6
M 128 130
F 128 131
F 131 140
F 131 141
M 131 200
M 140 200
output
4
Note

In the first sample, friends 3 and 4 can come on any day in range [117, 128].

In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.


题意:给你一个人的性别和他能来的时间区间,问最后能来多少人,前提是女生和男生一样多


思路:我是用线段树写的,sum1和sum2记录在那一天能来的女生和男生的数量,两个lazy维护,最后查找每天能去的最大值


ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
struct s
{
	int left;
	int right;
	int sum1,sum2; 
	int lazy1,lazy2;
}tree[5555];
struct ss
{
	char a[2];
	int b,c;
}p[5555];
void pushdown(int i)
{
	if(tree[i].lazy1)
	{
		tree[i*2].sum1+=tree[i].lazy1*(tree[i*2].right-tree[i*2].left+1);
		tree[i*2+1].sum1+=tree[i].lazy1*(tree[i*2+1].right-tree[i*2+1].left+1);
		tree[i*2].lazy1+=tree[i].lazy1;
		tree[i*2+1].lazy1+=tree[i].lazy1;
		tree[i].lazy1=0;
	}
	if(tree[i].lazy2)
	{
		tree[i*2].sum2+=tree[i].lazy2*(tree[i*2].right-tree[i*2].left+1);
		tree[i*2+1].sum2+=tree[i].lazy2*(tree[i*2+1].right-tree[i*2+1].left+1);
		tree[i*2].lazy2+=tree[i].lazy2;
		tree[i*2+1].lazy2+=tree[i].lazy2;
		tree[i].lazy2=0;
	}
}
void build(int l,int r,int i)
{
	int mid;
	tree[i].left=l;
	tree[i].right=r;
	tree[i].lazy1=0;
	tree[i].lazy2=0;
	if(l==r)
	{
		tree[i].sum1=0;
		tree[i].sum2=0;
		return;
	} 
	mid=(l+r)/2;
	build(l,mid,i*2);
	build(mid+1,r,i*2+1);
	tree[i].sum1=tree[i*2].sum1+tree[i*2+1].sum1;
	tree[i].sum2=tree[i*2].sum2+tree[i*2+1].sum2;
}
void update(int l,int r,int c,int i,int q)
{
	int mid;
	if(tree[i].left==l&&tree[i].right==r)
	{
		if(q==0)
		tree[i].lazy1+=c,tree[i].sum1+=c*(r-l+1);
		else
		tree[i].lazy2+=c,tree[i].sum2+=c*(r-l+1);	
		return;
	}
	pushdown(i);
	mid=(tree[i].left+tree[i].right)/2;
	if(r<=mid)
	update(l,r,c,i*2,q);
	else if(l>mid)
	update(l,r,c,i*2+1,q);
	else
	{
		update(l,mid,c,i*2,q);
		update(mid+1,r,c,i*2+1,q);
	}
	tree[i].sum1=tree[i*2].sum1+tree[i*2+1].sum1;
	tree[i].sum2=tree[i*2].sum2+tree[i*2+1].sum2;
}
s query(int a,int i)
{
	if(tree[i].left==a&&tree[i].right==a)
	return tree[i];
	pushdown(i);
	int mid=(tree[i].left+tree[i].right)/2;
	if(a<=mid)
	return query(a,i*2);
	else if(a>mid)
	return query(a,i*2+1);
}
int main()
{
	build(1,366,1);
	int n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s%d%d",p[i].a,&p[i].b,&p[i].c);
		if(p[i].a[0]=='M')
		update(p[i].b,p[i].c,1,1,0);
		else
		update(p[i].b,p[i].c,1,1,1);
	}
	int ans=-1;
	//s kk=query(117,1);
	//printf("debug %d %d\n",kk.sum1,kk.sum2);
	for(i=1;i<=366;i++)
	{
		s k=query(i,1);
		ans=max(ans,min(k.sum1,k.sum2)*2);
	}
	printf("%d\n",ans);
	return 0;
}


你可能感兴趣的:(Codeforces Round #343 (Div. 2) B. Far Relative’s Problem (线段树+区间更新+单点查询))