HDU-3874 Necklace 线段树+离线
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874
比较简单的题,题意也好懂。
先O(n)求每个数左边第一次出现的与他相同的数的位置l[i]。对询问按照y从小大排序,然后按照从左到右的顺序来跟新点,当前点为i,那么删掉l[i],加入点i,然后遇到询问求和。
//STATUS:C++_AC_2593MS_10024KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
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));}
//End struct Node{
int a,b,id;
bool operator < (const Node& a)const {
return b<a.b;
}
}q[M];
LL sum[N<<],ans[M];
int num[N],l[N],la[];
int T,n,m; void update(int l,int r,int rt,int w,int val)
{
if(l==r){
sum[rt]=val;
return;
}
int mid=(l+r)>>;
if(w<=mid)update(lson,w,val);
else update(rson,w,val);
sum[rt]=sum[rt<<]+sum[rt<<|];
} LL query(int l,int r,int rt,int L,int R)
{
if(L<=l && r<=R){
return sum[rt];
}
int mid=(l+r)>>;
LL ret=;
if(L<=mid)ret+=query(lson,L,R);
if(R>mid)ret+=query(rson,L,R);
return ret;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,k;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mem(la,);
for(i=;i<=n;i++){
scanf("%d",&num[i]);
l[i]=la[num[i]];
la[num[i]]=i;
}
scanf("%d",&m);
for(i=;i<m;i++){
scanf("%d%d",&q[i].a,&q[i].b);
q[i].id=i;
}
sort(q,q+m);
mem(sum,);k=;
for(i=;i<=n;i++){
if(l[i]){
update(,n,,l[i],);
}
update(,n,,i,num[i]);
for(;q[k].b==i && k<m;k++){
ans[q[k].id]=query(,n,,q[k].a,q[k].b);
}
}
for(i=;i<m;i++){
printf("%I64d\n",ans[i]);
}
}
return ;
}
HDU-3874 Necklace 线段树+离线的更多相关文章
- HDU - 3874 Necklace (线段树 + 离线处理)
欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/3 ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 4638-Group(线段树+离线处理)
题意: 给n个编号,m个查询每个查询l,r,求下标区间[l,r]中能分成标号连续的组数(一组内的标号是连续的) 分析: 我们认为初始,每个标号为一个组(线段树维护区间组数),从左向右扫序列,当前标号, ...
- HDU 4417 【线段树+离线处理】
http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意:找出给定区间内,有多少个数小于等于给定的数.用线段树维护的话会超时,要用到线段树的离线操作,对询问与 ...
- hdu 4288 Coder (线段树+离线)
题意: 刚开始有一个空集合.有三种操作: 1.往集合中加入一个集合中不存在的数 x 2.从集合中删除一个已经存在的数 x 3.计算集合的digest sum并输出. digest sum求 ...
- hdu 3874 Necklace(bit树+事先对查询区间右端点排序)
Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful v ...
- HDU 3874 离线段树
在所有数字的统计范围,,对于重复统计只有一次 离线段树算法 排序终点坐标.然后再扫,反复交锋.把之前插入树行被删除 #include "stdio.h" #include &quo ...
- HDU3874 线段树 + 离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 , 线段树(或树状数组) + 离线处理 下午做了第一道离线处理的题目(HDU4417),多少有点 ...
随机推荐
- 使用 httpkit 来替代 jetty
Compojure 是一个基于 ring 的上层web开发框架.在 lein new compojure my-app 生成的项目中,默认是启用 jetty 服务器的,最近用到了 http-kit 中 ...
- [lintcode the-smallest-difference]最小差(python)
题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...
- 无法创建链接服务器 "(null)" 的 OLE DB 访问接口 "Microsoft.Ace.OLEDB.12.0" 的实例。
--开启导入功能 exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc ...
- 2016MBA排名
2016全球商学院100强 2016上半年度最受欢迎的十大MBA排名 网上评选出上年度最受欢迎的十大MBA排名,有你想要报考的院校吗?快来一睹这些MBA院校的风采,选择好适合自己的院校项目. 第1名 ...
- LA 3602 DNA Consensus String
最近审题老是一错再错,Orz 题目中说求一个Hamming值总和最小的字符串,而不是从所给字符中找一个最小的 这样的话,我们逐列处理,所求字符串当前位置的字符应该是该列中出现次数最多其次ASCII值最 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- Codeforces Round #276 (Div. 2)
A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...
- [转][TFS] 禁止默认允许多人签出和强制解除签入签出锁
转自:http://blog.xieyc.com/tfs-disable-multiple-check-out-and-force-to-undo-locking/ | 小谢的小站 [TFS] 禁止默 ...
- Annotation(jdk5.0注解)复习(转自http://3w_cnblogs_com/pepcod/)
package annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retent ...
- (六)6.12 Neurons Networks from self-taught learning to deep network
self-taught learning 在特征提取方面完全是用的无监督的方法,对于有标记的数据,可以结合有监督学习来对上述方法得到的参数进行微调,从而得到一个更加准确的参数a. 在self-taug ...