URAL 1613 For Fans of Statistics

题意:就是给了你n个数字,他们的编号为1到n,然后接下来有q个询问,每次询问有 l,r,x, 就是问你 是否 在编号区间[l,r]内有数字X出现过,有就是1,无就是0,最后一起输出来


输入其实数字是有重复的,所以先用map离散化,然后再用map跟vector 的邻接表连接,讲编号放入邻接表里面,并升序排序,然后询问的时候直接二分查找编号,是否存在就可以了,手写的二分 一直WA,莫名其妙,改用Lower_bound就过了


int n;

map<string ,int>mark,mp;

vector<int > G[70000 + 55];

int tot;

void init() {
	mp.clear();
	mark.clear();
	for(int i=0;i<70000 + 55;i++)G[i].clear();
	tot = 1;
}

bool input() {
	while(cin>>n) {
		string tmp;
		for(int i=0;i<n;i++) {
			cin>>tmp;
			if(!mark[tmp]) {
				mp[tmp] = tot;
				G[tot].push_back(i + 1);
				tot++;
				mark[tmp]++;
			}
			else G[mp[tmp]].push_back(i + 1);
		}
		return false;
	}
	return true;
}

void cal() {
	for(int i=1;i<=tot;i++)sort(G[i].begin(),G[i].end());
	int q;
	cin>>q;
	int le,ri;
	string now;
	string ans = "";
	while(q--) {
		cin>>le>>ri>>now;
		int pos = mp[now];
		if(pos == 0) {ans += "0";continue;}
		int len = G[pos].size();
		int Head = lower_bound(G[pos].begin(),G[pos].end(),le) - G[pos].begin();
		if(Head < len && G[pos][Head] <= ri)ans += "1";
		else ans += "0";
	}
	cout<<ans<<endl;
}

void output() {

}

int main() {
	while(true) {
		init();
		if(input())return 0;

		cal();
		output();
	}
	return 0;
}

/*
5
1234567 666666 3141593 666666 4343434
5
1 5 3141593
1 5 578202
2 4 666666
4 4 666666
1 1 1234567

*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值