二分法、线段树

http://acm.nbut.edu.cn/Problem/view.xhtml?id=1233
二分法
//#pragma comment(linker, "/STACK:61400000,61400000")
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define pi 3.1415926535897932385
#define LL64 __int64
#define LL long long
#define oo 2147483647
#define N 100100
#define M 210
#define INF 1e9

int a[N];

void bin(int l, int r, int v)
{
	int mid = (l + r)>>1;
	if (v == a[mid]) printf("True\n");
	else if (l == r) printf("False\n");
	else if (v < a[mid]) bin(l, mid, v);
	else bin(mid + 1, r, v);
}

int main()
{
	int n;
	
	//freopen("data.in", "r", stdin);
	//freopen("data.out", "w", stdout);
	//int _case = 1;
	//scanf("%d", &t);
	//while (t--)
	//for (int _case = 1; _case <= t; _case++)
	while (~scanf("%d", &n))
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
		}
		int m;
		scanf("%d", &m);
		while (m--)
		{
			int x;
			scanf("%d", &x);
			bin(0, n - 1, x);
		}
	}
	
	return 0;
}
线段树

//#pragma comment(linker, "/STACK:61400000,61400000")
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define pi 3.1415926535897932385
#define LL64 __int64
#define LL long long
#define oo 2147483647
#define N 100100
#define M 210
#define INF 1e9

struct tree
{
int l, r, mid, lv, rv;
}a[N * 3];

void init(int ll, int rr, int num)
{
a[num].l = ll;
a[num].r = rr;
a[num].mid = (ll + rr)>>1;
if (ll == rr)
{
scanf("%d", &a[num].lv);
a[num].rv = a[num].lv;
}
else
{
init(ll, a[num].mid, num * 2);
init(a[num].mid + 1, rr, num * 2 + 1);
a[num].lv = a[num * 2].lv;
a[num].rv = a[num * 2 + 1].rv;
}
}

void find(int num, int x)
{
if (a[num].l == a[num].r)
{
if (a[num].lv == x) printf("True\n");
else printf("False\n");
}
else if (x <= a[num * 2].rv) find(num * 2, x);
else find(num * 2 + 1, x);
}

int main()
{
int n;

//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
//int _case = 1;
//scanf("%d", &t);
//while (t--)
//for (int _case = 1; _case <= t; _case++)
while (~scanf("%d", &n))
{
init(1, n, 1);
int m;
scanf("%d", &m);
while (m--)
{
int x;
scanf("%d", &x);
find(1, x);
}
}

return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值