FJUT 3097(hdu 3333) 区间种类数 线段树+离线

来源:互联网 发布:京东和淘宝双11销售额 编辑:程序博客网 时间:2024/05/20 01:38

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




区间种类数

TimeLimit:2500MS  MemoryLimit:128MB
64-bit integer IO format:%lld
已解决 | 点击收藏
Problem Description

给一长度为n的序列,询问其任意的区间的数字的种类数

Input

每个测试文件仅有一组数据

第一行是两个整数n和q代表,序列的长度和查询次数

接下来一行有n个数字 ,a1,a2,a3,……an 代表序列的n个元素

再接下有q行,每行有两个整数l,r,代表所要查询区间的左右端点

n<=100000

q<=100000

1<=l<=r<=n

1<=ai<=n

Output

输出区间[l,r]内数字的种类数

SampleInput
10 66 5 5 6 2 3 1 3 4 61 21 32 53 71 104 9
SampleOutput
223565


很经典的问题,离线就是先按照右端点先排好序,对于出现过的a[1]来说,记录他上一次出现的位置。如果是第一次出现则记录这个位置并

在线段树上更新1,如果不是第一次,则把之前的位置更新为0,当前位置更新为1。线段树求区间和。


hdu3333同理。


///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.///             __.'              ~.   .~              `.__///           .'//                  \./                  \\`.///        .'//                     |                     \\`.///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.///     .'//.-"                 `-.  |  .-'                 "-.\\`.///   .'//______.============-..   \ | /   ..-============.______\\`./// .'______________________________\|/______________________________`.#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_backtypedef 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=1e3+10;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;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;//}struct node{    int lt, rt;    LL val;}tree[4*maxn];//向上更新void PushUp(int id){    tree[id].val = tree[id<<1].val + tree[id<<1|1].val;}//建立线段树void Build(int lt, int rt, int id){    tree[id].lt = lt;    tree[id].rt = rt;    tree[id].val = 0;//每段的初值,根据题目要求    if (lt == rt)    {        //tree[id].val = 1;        return;    }    int mid = (lt + rt) >> 1;    Build(lt, mid, id<<1);    Build(mid+1, rt, id<<1|1);    //PushUp(id);}//更改区间内某个点的值void Change(int lt, int rt, int id, int to){    if (lt <= tree[id].lt && rt >= tree[id].rt)    {        tree[id].val = to;        return;    }    int mid = (tree[id].lt + tree[id].rt) >> 1;    if (lt <= mid)        Change(lt, rt, id<<1, to);    if (rt > mid)        Change(lt, rt, id<<1|1, to);    PushUp(id);}//查询某段区间内的heLL Query(int lt, int rt, int id){    if (lt <= tree[id].lt && rt >= tree[id].rt)        return tree[id].val;    int mid = (tree[id].lt + tree[id].rt) >> 1;    LL ans = 0;    if (lt <= mid)        ans += Query(lt, rt, id<<1);    if (rt > mid)        ans += Query(lt, rt, id<<1|1);    return ans;}struct Node{int l,r;int id;}q[100005];int a[100005],n,m;LL sum[100005];bool cmp(Node a,Node b){return a.r<b.r;}void work(){Build(1,n,1);int t,now=1;map<int,int>s;FOR(1,n,i){t=s[a[i]];if(t==0){Change(i,i,1,1);//l r id value //hdu 3333 Change(i,i,1,a[i]); s[a[i]]=i;}else {Change(t,t,1,0);Change(i,i,1,1);//Change(i,i,1,a[i]);s[a[i]]=i;}for(;now<=m&&q[now].r==i;now++)sum[q[now].id]=Query(q[now].l,q[now].r,1);}}void solve(){s_2(n,m);FOR(1,n,i)s_1(a[i]);FOR(1,m,i){s_2(q[i].l,q[i].r);q[i].id=i;}sort(q+1,q+m+1,cmp);work();FOR(1,m,i)print(sum[i]);}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();    }}


阅读全文
0 0