http://120.78.128.11/Problem.jsp?pid=3097

对每个位置建一棵主席树,每颗树记录的区间信息为[1~n]位置上的不重复权值和(第k大主席树记录的是值域,和这里不同)。从左往右建树,这样rt[i]树表示[1~i]所有不同值之和,然后和上面一样把重复出现的值的位置劲量往右移,当出现过a[i]时先删除rt[i-1]中对应位置的a[i]再把它加入到当前树中的i位置,查询的时候只要查询rt[r]树中[l,r]区间之和即可

///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \./                  \\`.
///        .'//                     |                     \\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\`.
///   .'//______.============-..   \ | /   ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scan_d(x)
#define S_2(x,y) scan_d(x),scan_d(y)
#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=~0U>>1;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e5+10;
const int maxx=1e6+10;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=19260817;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;//void readString(string &s)
//{
//  static char str[maxn];
//  scanf("%s", str);
//  s = str;
//}
const int MAXM=maxn*40;
int lc[MAXM],rc[MAXM],tol;
LL sum[MAXM];
int a[maxn], rt[maxn];
map<int,int>vis;int n,q;
void init()
{tol=1;vis.clear();
}
void update(int &u, int x, int y, int p, LL v)
{sum[tol] = sum[u]+v,lc[tol] = lc[u],rc[tol] = rc[u];u = tol++;if(x == y) return;int m = (x+y)>>1;if(p <= m) update(lc[u],x,m,p,v);else update(rc[u],m+1,y,p,v);
}
LL query(int u, int x, int y, int ql, int qr)
{if(ql <= x && y <= qr) return sum[u];int m = (x+y)>>1;LL ans = 0;if(ql <= m) ans += query(lc[u],x,m,ql,qr);if(qr > m) ans += query(rc[u],m+1,y,ql,qr);return ans;
}void solve()
{s_2(n,q);init();FOR(1,n,i) s_1(a[i]);FOR(1,n,i){rt[i]=rt[i-1];if(vis.find(a[i])!=vis.end()){int tmp=rt[i-1];update(tmp,1,n,vis[a[i]],-1);rt[i]=tmp;update(rt[i],1,n,i,1);}else {rt[i] = rt[i-1];update(rt[i],1,n,i,1);}vis[a[i]]=i;}W(q--){int l,r;s_2(l,r);printf("%lld\n", query(rt[r],1,n,l,r));}
}int main()
{//freopen( "1.in" , "r" , stdin );//freopen( "1.out" , "w" , stdout );int t=1;//init();//s_1(t);for(int cas=1;cas<=t;cas++){//printf("Case #%d: ",cas);solve();}
}

FJUT 3097(hdu 3333) 区间种类数 主席树+在线相关推荐

  1. [Ynoi2016] 镜中的昆虫——浅谈区间种类数问题

    诈尸啦~ 这篇博客是很久之前就想写的,总结一些关于区间种类数的问题, 正好一早上都在偏远机房上课,又不想写项目,就随便更新一点: [Ynoi2016] 镜中的昆虫 题意: 给定一个长度为 n n n ...

  2. D-query SPOJ - DQUERY(求区间不同数的个数)(树状数组||线段树+离散)(主席树+在线)

    English Vietnamese Given a sequence of n numbers a1, a2, -, an and a number of d-queries. A d-query ...

  3. HDU - 4348 To the moon(主席树区间更新-标记永久化)

    题目链接:点击查看 题目大意:给出一个初始时长度为 n 的序列,有 m 次操作,每种操作分为下列四种类型: C l r d:新建一个继承了前一个版本的数组,并将区间 [ l , r ] 内的数字都加上 ...

  4. hdu 4348 To the moon (主席树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...

  5. [四校联考P3] 区间颜色众数 (主席树)

    主席树 Description 给定一个长度为 N 颜色序列A,有M个询问:每次询问一个区间里是否有一种颜色的数量超过了区间的一半,并指出是哪种颜色. Input 输入文件第一行有两个整数:N和C 输 ...

  6. HDU - 6621 K-th Closest Distance——主席树+二分

    [题目描述] HDU - 6621 K-th Closest Distance [题目分析] 因为看到第kkk大的要求,刚开始的时候一直都在想怎么运用第kkk大来解决问题,但是后来看其他人的博客才发现 ...

  7. HDU - 6278 Just $h$-index主席树+二分

    HDU - 6278 Just hhh-index [题目描述] [题目分析] 题目要求在区间[l,r][l,r][l,r]内大于h的数不少于h个,对于这种最大化问题,我们应该想到二分. 最小情况显然 ...

  8. HDU 2852 KiKi's K-Number 主席树

    题意: 要求维护一个数据结构,支持下面三种操作: \(0 \, e\):插入一个值为\(e\)的元素 \(1 \, e\):删除一个值为\(e\)的元素 \(2 \, a \, k\):查询比\(a\ ...

  9. Hdu-5919 Sequence II(主席树在线求区间不同数)

    Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,an There a ...

  10. HDU - 4417 Super Mario(主席树/线段树+离线)

    题目链接:点击查看 题目大意:给出由 n 个数的数列,再给出 m 次查询,每次查询需要输出 [ l , r ] 内小于等于 h 的数有多少个 题目分析:大晚上睡不着觉随便做做题,发现这个题目原来可以用 ...

最新文章

  1. java collections_Java集合基础的详细介绍(二)
  2. CCIE-MPLS基础篇-实验手册
  3. OpenCV中CV_IS_MAT_CONT(src->type dst->type) 的含义
  4. pandas(五) -- 文本处理
  5. MEF 插件式开发 - DotNetCore 初体验
  6. 非传统营销 text_传统营销已死
  7. C# 知识点笔记:IEnumerable的使用,利用反射动态调用方法
  8. delphi FastReport 安装方法
  9. 中国替代运动器材市场趋势报告、技术动态创新及市场预测
  10. c# textbox和listbox多行显示
  11. linux 子进程exit6,linux 惊群有关问题
  12. A New Romance Is Likely to End up like Your Previous Relationship 为什么每次恋爱总会走向相似的结局?
  13. 开源表单系统|Tduck填鸭表单docker部署详细教程
  14. php7和PHP5对比的新特性和性能优化
  15. 信源编码的三种方式与实现
  16. Neuralizing Regular Expressions for Slot Filling 神经网络转回自动机
  17. 计算机软件定时运行,Win7打开定时运行程序的方法
  18. GCC编译宏_GLIBCXX_USE_CXX11_ABI背景分析和实现原理
  19. [Android]在Android TV中实现组合按键的监听触发功能
  20. 使用YASM编程 - 01

热门文章

  1. 计算机用什么配置好电脑,买电脑主要看哪些配置 决定电脑好坏的关键
  2. 【论文笔记】ASNet:基于生成对抗网络(GAN)的无监督单模和多模配准网络(范敬凡老师)
  3. hdu-4565(矩阵快速幂+推导)
  4. uni-app 框架超详细新手入门
  5. 亲手将TP-LINK路由器改装成交换机使用
  6. 【wangeditor富文本编辑器v4版自定义功能】格式刷
  7. 任意波形发生器的主要功能
  8. 论文阅读——LSQ:Learned Step Size Quantization
  9. OSChina 周六乱弹 —— 泡面就要泡着吃……
  10. android格式化sd卡软件,如何在各种设备中把SD卡格式化?附误格式化数据恢复方法!...