• 「SDOI2009」HH的项链


    「SDOI2009」HH的项链

    传送门
    数据加强了,莫队跑不过了。
    考虑用树状数组。
    先把询问按右端点递增排序。
    然后对于每一种贝壳,我们都用它最右一次出现的位置计算答案。
    具体细节看代码吧。
    参考代码:

    #include <algorithm>
    #include <cstdio>
    #define rg register
    #define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
    using namespace std;
    template < class T > inline void read(T& s) {
    	s = 0; int f = 0; char c = getchar();
    	while ('0' > c || c > '9') f |= c == '-', c = getchar();
    	while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    	s = f ? -s : s;
    }
    
    const int _ = 1e6 + 5;
    
    int n, q, a[_], res[_], tr[_], tag[_];
    
    struct node { int l, r, id; } t[_];
    inline bool cmp(const node& x, const node& y) { return x.r < y.r; }
    
    inline int lb(int x) { return x & -x; }
    
    inline void update(int x, int v)
    { for (rg int i = x; i <= n; i += lb(i)) tr[i] += v; }
    
    inline int query(int x)
    { int res = 0; for (rg int i = x; i >= 1; i -= lb(i)) res += tr[i]; return res; }
    
    int main() {
    #ifndef ONLINE_JUDGE
    	file("cpp");
    #endif
    	read(n);
    	for (rg int i = 1; i <= n; ++i) read(a[i]);
    	read(q);
    	for (rg int i = 1; i <= q; ++i) read(t[i].l), read(t[i].r), t[i].id = i;
    	sort(t + 1, t + q + 1, cmp);
    	int pos = 1;
    	for (rg int i = 1; i <= q; ++i) {
    		for (rg int j = pos; j <= t[i].r; ++j) {
    			if (tag[a[j]]) update(tag[a[j]], -1);
    			update(j, 1), tag[a[j]] = j;
    		}
    		pos = t[i].r + 1;
    		res[t[i].id] = query(t[i].r) - query(t[i].l - 1);
    	}
    	for (rg int i = 1; i <= q; ++i) printf("%d
    ", res[i]);
    	return 0;
    }
    
  • 相关阅读:
    颜色空间之CIE2000色差公式
    爱做梦的人-人的特性之一
    Lua文件操作和串行化
    Lua文件操作和串行化
    Lua调用C++带参数的方法
    Lua调用C++带参数的方法
    C++对Lua中table进行读取、修改和创建
    C++对Lua中table进行读取、修改和创建
    C++获取Lua全局变量和执行Lua多参数多返回值函数
    C++获取Lua全局变量和执行Lua多参数多返回值函数
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/12231638.html
Copyright © 2020-2023  润新知