• hdu 5654 xiaoxin and his watermelon candy 莫队


    题目链接

    求给出的区间中有多少个三元组满足i+1=j=k-1 && a[i]<=a[j]<=a[k] 如果两个三元组的a[i], a[j], a[k]都相等, 那么这两个三元组算一个。

    预处理一下所有三元组, 然后跑莫队就水过去了...

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <complex>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef complex <double> cmx;
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 2e5+5;
    struct node
    {
    	int l, r, id, block;
    	bool operator < (node a) const
    	{
    		if(block == a.block)
    			return r<a.r;
    		return block<a.block;
    	}
    }q[maxn];
    int a[maxn], b[maxn];
    int n, m, ans[maxn], num[maxn];
    map <pair<pll, int>, int> mp;
    void solve() {
        cin>>n;
        mem(num);
        mem(b);
    	int block = sqrt(n);
    	for(int i = 1; i <= n; i++) {
    		scanf("%d", &a[i]);
    	}
    	cin>>m;
    	for(int i = 0; i < m; i++) {
    		scanf("%d%d", &q[i].l, &q[i].r);
    		q[i].l++, q[i].r--;
    		q[i].block = q[i].l/block;
    		q[i].id = i;
    	}
    	sort(q, q+m);
    	int cnt = 0;
    	mp.clear();
    	for(int i = 2; i <= n-1; i++) {
    		if(a[i]>=a[i-1] && a[i]<=a[i+1]) {
    			if(!mp[mk(mk(a[i-1], a[i]), a[i+1])]) {
    				mp[mk(mk(a[i-1], a[i]), a[i+1])] = ++cnt;
    			}
    			b[i] = mp[mk(mk(a[i-1], a[i]), a[i+1])];
    		}
    	}
    	int L = 1, R = L-1, tot = 0;
    	for(int i = 0; i < m; i++) {
            if(q[i].l>q[i].r) {
                ans[q[i].id] = 0;
                continue;
            }
    		while(L<q[i].l) {
    			num[b[L]]--;
    			if(num[b[L]]==0&&b[L]!=0)
    				tot--;
    			L++;
    		}
    		while(L>q[i].l) {
    			L--;
    			num[b[L]]++;
    			if(num[b[L]]==1&&b[L]!=0)
    				tot++;
    		}
    		while(R>q[i].r) {
    			num[b[R]]--;
    			if(num[b[R]]==0&&b[R]!=0)
    				tot--;
    			R--;
    		}
    		while(R<q[i].r) {
    			R++;
    			num[b[R]]++;
    			if(num[b[R]]==1&&b[R]!=0)
    				tot++;
    		}
    		ans[q[i].id] = tot;
    	}
    	for(int i = 0; i < m; i++) {
            printf("%d
    ", ans[i]);
    	}
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--) {
        	solve();
        }
        return 0;
    }
    
    
  • 相关阅读:
    [IIS] IIS Framework "aspnet_regiis.exe" 注册
    [Bootstrap] install Bootstrap framework in window 7 by npm
    [Window] .MUS 0x80070422 Error
    【IIS】模块 DLL C:WindowsSystem32inetsrvauthcert.dll 未能加载。返回的数据为错误信息。
    【IIS】IIS 7.0/7.5 无法启动 w3svc 服务
    【IIS】IIS 7.0/7.5 绑定
    【SharePoint 2010】SharePoint 2010开发方面的课堂中整理有关问题
    『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于SQL)
    『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于AD)
    比较好用的Opera 翻译工具 ddict
  • 原文地址:https://www.cnblogs.com/yohaha/p/5328571.html
Copyright © 2020-2023  润新知