XDOJ 1023: IP查询

题意:这个题的心路历程最为坎坷,从刚开始接触C的时候,觉得高大上,然后看了下线段树,觉得这题能水过去,结果TLEn次,REn+1次,最后不得以还是采用了二分的方法,但是引发了cout血案,因为这个一直TLE,直到最后改了printf终于,AC,23333333
#include<algorithm>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef struct node {
	int x;
	int y;
	int date;
	friend bool operator< (node a,node b) {
		return a.x<b.x;
	}
} node;
node a[100005];
int main() {
	int m;
	scanf("%d",&m);
	int n;
	for(int i=1; i<=m; i++) {
		scanf("%d",&n);
		for(int j=1; j<=n; j++) {
			scanf("%d %d %d",&a[j].x,&a[j].y,&a[j].date);
		}
		sort(a+1,a+n+1);
		int k;
		scanf("%d",&k);
		int p;
		for(int j=1; j<=k; j++) {
			scanf("%d",&p);
			int flag=0;
			int first=1,end=n;
			int sum;
			int mid=(first+end)/2;
			while(first<=end) {
				if(p>=a[mid].x) {
					if(p<=a[mid].y) {
						flag=1;
						break;
					}
					first=mid+1;
					mid=(first+end)/2;
				} else {
					if(p<=a[mid-1].y&&p>=a[mid-1].x) {
						flag=1;
						mid=mid-1;
						break;
					}
					end=mid-1;
					mid=(end+first)/2;
				}
			}
			if(flag==1) {
				printf("%d
",a[mid].date);
			} else {
				printf("-1
");
			}
		}
	}
	return 0;
}

原文地址:https://www.cnblogs.com/wang9897/p/7624406.html