zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    字符串替换
    字符串查找
    字符串比较
    字节与字符串相互转换
    1365. How Many Numbers Are Smaller Than the Current Number
    1486. XOR Operation in an Array
    1431. Kids With the Greatest Number of Candies
    1470. Shuffle the Array
    1480. Running Sum of 1d Array
    【STM32H7教程】第56章 STM32H7的DMA2D应用之刷色块,位图和Alpha混合
  • 原文地址:https://www.cnblogs.com/rolight/p/4280485.html
Copyright © 2011-2022 走看看