zoukankan      html  css  js  c++  java
  • 「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;
    }
    
  • 相关阅读:
    gtk在线文档
    spice remote-viewer 连接会话时自动重定向usb设备(记录)
    04、数组与Arrays工具类
    03、选择、循环结构
    02、基本概念
    01、初识Java
    0、计算机相关知识了解
    云服务器Centos7部署Tomcat服务器
    JavaSE基础(三)--Java基础语法
    JavaSE基础(二)--Java环境配置
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/12231638.html
Copyright © 2011-2022 走看看