• HDU 5172 GTY's gay friends 线段树 or Hash


    统计一个区间中没有重复得数,用线段树搞法大概就是记录一下每个数上一次出现的位置,然后肯这个区间的pre的最大值是否大于左端点的值了。

    //预处理前缀和+线段树解法
    #include <cstdio>
    #include <cstring>
    #include <climits>
    #include <cmath>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    
    #define lson rt << 1, l, mid
    #define rson rt << 1 | 1, mid + 1, r
    
    const int maxn = 1e6 + 10;
    int n, q, arr[maxn], pre[maxn], maxv[maxn << 2], pos[maxn];
    LL sum[maxn];
    
    void build(int rt, int l, int r) {
    	int mid = l + r >> 1;
    	if(l == r) maxv[rt] = pre[l];
    	else {
    		build(lson); build(rson);
    		maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]);
    	}
    }
    
    int ask(int rt, int l, int r, int ql, int qr) {
    	if(ql <= l && qr >= r) return maxv[rt];
    	int ret = -1, mid = l + r >> 1;
    	if(ql <= mid) ret = max(ret, ask(lson, ql, qr));
    	if(qr > mid) ret = max(ret, ask(rson, ql, qr));
    	return ret;
    }
    
    void preprocess() {
    	memset(pos, -1, sizeof(pos));
    	for(int i = 1; i <= n; i++) {
    		sum[i] = sum[i - 1] + arr[i];
    		pre[i] = pos[arr[i]];
    		pos[arr[i]] = i;
    	}
    	build(1, 1, n);
    }
    
    void gao(int l, int r) {
    	LL len = r - l + 1, tsum = (1 + len) * len / 2;
    	int tp = ask(1, 1, n, l, r);
    	if(tsum == sum[r] - sum[l - 1] && tp < l) puts("YES");
    	else puts("NO");
    }
    
    int main() {
    	while(scanf("%d%d", &n, &q) != EOF) {
    		for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
    		preprocess();
    		int l, r;
    		for(int i = 1; i <= q; i++) {
    			scanf("%d%d", &l, &r);
    			gao(l, r);
    		}
    	}
    	return 0;
    }
    

     这里主要用到的hash姿势就是把每个数字映射到另外一个随机数上面,然后异或来搞就好了,不过要注意^运算的优先级.保险起见我判了两次。

    #include <cstdio>
    #include <cstring>
    #include <climits>
    #include <cmath>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <stack>
    #include <ctime>
    
    
    typedef unsigned long long LL;
    const int maxn = 1e6 + 10;
    int n, q, arr[maxn];
    LL sum[maxn], hash[maxn], nmap[maxn], thash[maxn], thash1[maxn], nmap1[maxn], hash1[maxn];
    
    LL longRand() {
        return
            ((LL)rand() << 49) |
            ((LL)rand() << 34) |
            ((LL)rand() << 19) |
            ((LL)rand() << 4)  |
            (rand() & 15);
    }
    
    void init() {
        for(int i = 1; i <= 1000000; i++) {
            nmap[i] = longRand();
            nmap1[i] = longRand();
            thash[i] = thash[i - 1] ^ nmap[i];
            thash1[i] = thash1[i - 1] ^ nmap1[i];
        }
    }
    
    int main() {
        srand(time(NULL));
        init();
        while(scanf("%d%d", &n, &q) != EOF)  {
            for(int i = 1; i <= n; i++) {
                scanf("%d", &arr[i]);
                sum[i] = sum[i - 1] + arr[i];
                hash[i] = hash[i - 1] ^ nmap[arr[i]];
                hash1[i] = hash1[i - 1] ^ nmap1[arr[i]];
            }
            int l, r;
            for(int i = 1; i <= q; i++) {
                scanf("%d%d", &l, &r);
                LL len = r - l + 1;
                if(sum[r] - sum[l - 1] == len * (len + 1) / 2
                        && (hash[r] ^ hash[l - 1]) == thash[len]
                        && (hash1[r] ^ hash1[l - 1]) == thash1[len]
                        ) {
                    puts("YES");
                }
                else puts("NO");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    chrome调试
    css与jquery、图标字体、常用数据
    subline 快捷键与功能解释
    mysql-5.7.25安装及常用语法
    python 异步IO-aiohttp与简单的异步HTTP客户端/服务器
    python异步IO-asyncio、async和await
    python 异步IO-协程
    python 异步IO
    python-生成器即send()用法
    python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句
  • 原文地址:https://www.cnblogs.com/rolight/p/4280485.html
Copyright © 2020-2023  润新知