Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么TLE了我的指针主席树啊

Posted by 2014lvzelong at 2017-05-21 14:22:48 on Problem 2104 and last updated at 2017-05-21 14:23:11
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<set>
#define ss system("pause")
#define LL long long
#define pn puts("")
using namespace std;
const int MAXN = 100010;
int q[MAXN], line[MAXN];

int read()
{
	char ch = getchar(); int x =0, f = 1;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return x * f;
}

struct PST_Node{
	PST_Node *lson, *rson;
	int left, right;
	int cnt; 
	PST_Node():lson(NULL), rson(NULL), left(0), right(0), cnt(0) {}
	void Build(int l, int r);
	void Insert(PST_Node *P, int pos);
}*root[MAXN];

void PST_Node::Build(int l, int r)
{
	left = l;
	right = r;
	if(l == r) return; 
	int mid = (l + r) >> 1;
	lson = new PST_Node;
	rson = new PST_Node;
	lson->Build(l, mid);
	rson->Build(mid + 1, r);
}

void PST_Node::Insert(PST_Node *P, int pos)
{
	left = P->left;
	right = P->right;
	cnt = P->cnt + 1;
	if(left == right) return;
	int mid = (left + right) >> 1;
	if(pos <= mid) {
		rson = P->rson;
		lson = new PST_Node;
		lson->Insert(P->lson, pos);
	}
	else
	{
		lson = P->lson;
		rson = new PST_Node;
		rson->Insert(P->rson, pos);
	}
}

int Query(PST_Node *P, PST_Node *N, int k)
{
	if(N->left == N->right) return q[N->left];
	int cmp = N->lson->cnt - P->lson->cnt;
	if(cmp >= k) return Query(P->lson, N->lson, k);
	else return Query(P->rson, N->rson, k - cmp);
}

int main()
{
	int n = read(), m = read();
	for(int i = 1;i <= n; ++i)
	{
		line[i] = read();
		q[i] = line[i];
	}
	sort(q + 1, q + n + 1);
	root[0] = new PST_Node;
	root[0]->Build(1, n);
	for(int i = 1; i <= n; ++i)
	{
		root[i] = new PST_Node;
		int pos = lower_bound(q + 1, q + n + 1, line[i]) - q;
		root[i] -> Insert(root[i - 1], pos);
	}
	for(int i = 1;i <= m; ++i)
	{
		int l = read(), r = read(), k = read();
		printf("%d\n", Query(root[l - 1], root[r], k));
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator