zoukankan      html  css  js  c++  java
  • bzoj5178 [Jsoi2011]棒棒糖 主席树+线段树二分

    题目传送门

    https://lydsy.com/JudgeOnline/problem.php?id=5178

    https://lydsy.com/JudgeOnline/problem.php?id=2223

    https://lydsy.com/JudgeOnline/problem.php?id=3524

    三倍经验。

    题解

    这道题目很容易转化为求区间的众数。

    但是很显然这个和区间众数还是有一些区别的,因为这里要求出的众数的数量一定大于一半。

    也就是说,这个众数的出现次数比别的数的和还要多。

    所以可以考虑用主席树维护,每一次都往区间和最大的点走。

    最后比较一下是不是大于一半就可以了。


    时间复杂度 (O(nlog n))

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 500000 + 7;
    
    int n, m, Q, nod;
    int a[N], rt[N];
    
    struct Node { int lc, rc, val; } t[N * 20];
    inline void ins(int &o, int p, int L, int R, int x) {
    	t[o = ++nod] = t[p], ++t[o].val;
    	if (L == R) return;
    	int M = (L + R) >> 1;
    	if (x <= M) ins(t[o].lc, t[p].lc, L, M, x);
    	else ins(t[o].rc, t[p].rc, M + 1, R, x);
    }
    inline pii qval(int o, int p, int L, int R) {
    	if (L == R) return pii(t[o].val - t[p].val, L);
    	int M = (L + R) >> 1;
    	if (t[t[o].lc].val - t[t[p].lc].val > t[t[o].rc].val - t[t[p].rc].val) return qval(t[o].lc, t[p].lc, L, M);
    	else return qval(t[o].rc, t[p].rc, M + 1, R);
    }
    
    inline void work() {
    	for (int i = 1; i <= n; ++i) ins(rt[i], rt[i - 1], 1, m, a[i]);
    	while (Q--) {
    		int l, r;
    		read(l), read(r);
    		pii ans = qval(rt[r], rt[l - 1], 1, m);
    		if (ans.fi > (r - l + 1) / 2) printf("%d
    ", ans.se);
    		else puts("0");
    		printf("%d
    ", ans);
    	}
    }
    
    inline void init() {
    	read(n), read(Q);
    	for (int i = 1; i <= n; ++i) read(a[i]), smax(m, a[i]);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    kafka 常见面试题
    分布式-redis实现分布式锁
    java info信息中打印异常堆栈
    11.盛水最多的容器
    445.两数相加
    328. 奇偶链表
    7中join查询
    Linux基础学习05
    Linux基础学习04
    Linux基础学习03
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj5178.html
Copyright © 2011-2022 走看看