【SPOJ - DQUERY】D-query(权值树状数组 或 主席树 或 莫队)

题干:

Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

Input

  • Line 1: n (1 ≤ n ≤ 30000).
  • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
  • Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
  • In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).

Output

  • For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.

     

Example

Input
5
1 1 2 1 3
3
1 5
2 4
3 5Output
3
2
3 

解题报告:

 

AC代码:(树状数组,mp存位置,其实也可以用一个pre数组去存位置,(需要先都初始化成-1),用时分别是170ms和150ms)

#include<bits/stdc++.h>using namespace std;
map<int ,int> mp;
struct Node {int l,r;int id;
} node[200000 + 5];
int n,m,c[30000 + 5],ans[200000 + 5];
int a[30000 + 5];
int lowbit(int x) {return x&-x;
}
void update(int x,int val) {while(x<=n) {c[x] += val;x+=lowbit(x);}
}
int query(int x) {int res = 0;while(x>0) {res += c[x];x-=lowbit(x);}return res;
}
bool cmp(const Node & a,const Node & b) {return a.r < b.r;
}
int main()
{while(~scanf("%d",&n) ) {memset(c,0,sizeof c);mp.clear();for(int i = 1; i<=n; i++) {scanf("%d",&a[i]);}scanf("%d",&m);for(int i = 1; i<=m; i++) {scanf("%d%d",&node[i].l,&node[i].r);node[i].id = i;}int cur = 1;sort(node+1,node+m+1,cmp);for(int i = 1; i<=m; i++) {while(cur <= node[i].r) {if(mp[a[cur] ] == 0) update(cur,1);else {update(mp[a[cur] ] ,-1);update(cur,1);}mp[a[cur] ] = cur;cur++;}ans[node[i].id] = query(node[i].r) - query(node[i].l - 1);}for(int i = 1; i<=m; i++) {printf("%d\n",ans[i]);}}return 0 ;
}

AC代码2:

#include<bits/stdc++.h>using namespace std;
const int MAX = 30000 + 5;
struct TREE {int l,r;int val;
} tree[MAX * 40];
int n,tot;
int pre[1000000 + 5],root[MAX],a[MAX];
int build(int l,int r) {int ne = ++tot;tree[ne].val = 0;tree[ne].l=tree[ne].r = 0;if(l == r) return ne;int m = (l+r)/2;tree[ne].l = build(l,m);tree[ne].r = build(m+1,r);return ne;
}
int update(int pos,int c,int val,int l,int r) {int ne = ++tot;tree[ne] = tree[c];tree[ne].val += val;if(l == r) return ne;int m = (l+r)/2;if(m >= pos) tree[ne].l = update(pos,tree[c].l,val,l,m);else tree[ne].r = update(pos,tree[c].r,val,m+1,r);return ne; }
int query(int pos,int c,int l,int r) {if(l == r) return tree[c].val;int m = (l+r)/2;if(m>=pos) return tree[tree[c].r].val + query(pos,tree[c].l,l,m);else return query(pos,tree[c].r,m+1,r);
}
int main()
{cin>>n;memset(pre,-1,sizeof pre);for(int i = 1; i<=n; i++) scanf("%d",a+i);root[0] = build(1,n);//构造主席树 for(int i = 1; i<=n; i++) {if(pre[a[i]] == -1) {root[i] = update(i,root[i-1],1,1,n);}else {int tmp = update(pre[a[i]],root[i-1],-1,1,n);root[i] = update(i,tmp,1,1,n);	}pre[a[i]]=i;}int q,x,y;cin>>q;while(q--) {scanf("%d%d",&x,&y);printf("%d\n",query(x,root[y],1,n));}return 0 ;
}

莫队算法:(还未做、。、)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441525.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

freerdp 解压安装_Ubuntu下编译安装运作FreeRdp连接Windows

Ubuntu下编译安装运行FreeRdp连接Windows.linux下编译源码进行USB重定向到远程桌面的测试&#xff0c;经过参数的正确配置&#xff0c;可以正常的重定向。具体的操作步骤如下:⑴在虚拟机上安装git,安装命令如&#xff1a;sudo apt-get install git。⑵从GitHub下载FreeRdp源码&…

【HDU - 3328】Flipper (栈模拟)

题干&#xff1a; Problem Description Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts with n cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (C…

trunk口不通防火墙_交换机S5700与防火墙USG5500无法对接Eth-trunk LACP-static模式

问题&#xff1a;交换机S5700与防火墙USG5500无法对接Eth-trunk LACP-static模式&#xff0c;两端正常配置后&#xff0c;端口状态显示错误&#xff0c;Eth-trunk端口无法up 。问题描述&#xff1a;交换机侧 GE0/0/5和GE 0/0/6 组成Eth-trunk3 通过LACP-static与防火墙对接&…

【HDU - 1850】Being a Good Boy in Spring Festival (尼姆博弈,nim,异或前缀和)

题干&#xff1a; 一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄给爸爸买个小礼物 主动地 强烈地 要求洗一次碗 某一天早起 给爸妈用心地做回早餐 如果愿意 你还可以和爸妈说 咱们玩个小游戏吧 ACM课上学的呢…

comsol临时文件夹中有不支持的字符_Editor 常见问题

Editor的数据存在哪里&#xff1f;Editor所处理的数据&#xff0c;就是你自己本地电脑的文件夹、图片、文本文档。除了配置信息、一些必要的临时缓存之外&#xff0c;Editor没有特殊格式的数据。所以&#xff0c;数据都在电脑里&#xff1b;比如说需要备份的话&#xff0c;只要…

【51nod - 1098】 最小方差(基础数学,公式化简,前缀和,积的前缀和)

题干&#xff1a; 若x1,x2,x3……xn的平均数为k。 则方差s^2 1/n * [(x1-k)^2(x2-k)^2…….(xn-k)^2] 。 方差即偏离平方的均值&#xff0c;称为标准差或均方差&#xff0c;方差描述波动程度。 给出M个数&#xff0c;从中找出N个数&#xff0c;使这N个数方差最小。 Input …

【牛客 - 181B】送分题(另类求解a+b,二分)

题干&#xff1a; 对于一套题来说&#xff0c;没有一道送分题&#xff0c;就很不符合常理&#xff0c;但是我又懒得写送分题&#xff0c;所以你可以直接复制以下代码&#xff0c;即可ac本题. #include<cstdio>#include<iostream> using namespace std; int a,…

牧马人机械鼠标g3_性价比好的有线鼠标都有哪些?2020年12款热选游戏鼠标推荐...

决定一款鼠标好坏因素众多&#xff0c;我们常常发现一款鼠标&#xff0c;有些用户觉得非常好&#xff0c;也有些用户觉得不好&#xff0c;是因为大多数用户对于鼠标的尺寸大小、轻重、实际的手感等方面都会有所不同&#xff0c;这都很正常&#xff0c;所以适合自己才是最好的。…

emc re 整改 超标_老刘工程师睡前故事5-EMC 辐射发射超标怎么办?

老刘工程师睡前故事-EMC 辐射发射超标怎么办&#xff1f;今天的工程师睡前故事讲讲汽车EMC测试中辐射发射超标了怎么办?首先来聊聊辐射发射的基本知识辐射发射英文简称RE&#xff0c;检测的是产品对外的辐射干扰。国际标准参考CISPR25&#xff0c;国标参考GBT18655。这个标准分…

全国计算机等级考试职称,全国职称计算机考试与全国计算机等级考试有什么不同?...

落羽杉属共有三个树种&#xff1a;落羽杉(Taxodium.distichum)、池杉(T.ascendens)和墨西哥落羽杉(T.mucronatum)。苏杉一号(T. distichum ‘Sushang’)是从落羽杉不同地理种源、家系中选育出的优良种源。中山杉302是从落羽杉和墨西哥落羽杉杂种中选育出的优良无性系(另外&…

【PAT - 甲级 - 1018】Public Bike Management (带权最短路,多条最短路中加条件,DFS)

题干&#xff1a; 链接&#xff1a;https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f 来源&#xff1a;牛客网 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. On…

【牛客 - 181C】序列(前缀和,二分,可用set维护)(有坑)

题干&#xff1a; 小a有n个数&#xff0c;他想把他们划分为连续的权值相等的k段&#xff0c;但他不知道这是否可行。 每个数都必须被划分 这个问题对他来说太难了&#xff0c;于是他把这个问题丢给了你。 输入描述: 第一行为两个整数n,q&#xff0c;分别表示序列长度和询问…

【CodeForces - 122B 】Lucky Substring (字符串,水题)

题干&#xff1a; Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya was …

*【CodeForces - 122D】Lucky Transformation(字符串问题,思维剪枝,优化,有坑,需注意的问题if的层次总结)

题干&#xff1a; Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number…

c/c++,字符,字符串,各种方式读入与对空格,回车的处理

#include<iostream> #include<string> using namespace std; int main() {char a[50],b[50],charr;//经测试&#xff0c;cin读入字符串&#xff0c;会识别空格和回车为截止&#xff0c;并且不会吞掉&#xff0c;//只是每次读的时候会从第一个不为空格/回车的字符开…

【CodeForces - 357D】Xenia and Hamming (字符串问题,数论,思维)

题干&#xff1a; Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance. The Hamming distance between two strings s  s1s2... sn and t  t1t2... tn of equal length n is value . Record [si ≠ ti] is the Iverson n…

【牛客 - 181D】小叶的巡查(树的直径,数学)

题干&#xff1a; 8102年&#xff0c;牛客系列竞赛空前繁荣。为了更好地管理竞赛&#xff0c;小叶决定巡查于各大城市之间&#xff0c;体察民情。所以&#xff0c;从一个城市马不停蹄地到另一个城市成了小叶最常做的事情。小叶有一个钱袋&#xff0c;用于存放往来城市间的路费…

【蓝桥杯 - 练习】k倍区间(思维,数组)

题干&#xff1a; http://lx.lanqiao.cn/problem.page?gpidT444 问题描述 给定一个长度为N的数列&#xff0c;A1, A2, ... AN&#xff0c;如果其中一段连续的子序列Ai, Ai1, ... Aj(i < j)之和是K的倍数&#xff0c;我们就称这个区间[i, j]是K倍区间。   你能求出数列中…

【 HDU - 1525 】Euclid's Game(较难找规律,玄学博弈,分析必败点必胜点)

题干&#xff1a; Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be…

【CodeForces - 569A】Music (数学公式化简,模拟追及问题)

题干&#xff1a; Little Lesha loves listening to music via his smartphone. But the smartphone doesnt have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of E…