hdu3333 Turing Tree 2016-09-18 20:53 42人阅读 评论(0) 收藏
Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4658 Accepted Submission(s): 1640
Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.
For each case, the input format will be like this:
* Line 1: N (1 ≤ N ≤ 30,000).
* Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000).
* Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
* Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).
2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
1
5
6
3
6
本题用线段树撸简单的,但我尝试了下传说中神奇的莫队算法,水过去了
用cnt数组记录每个数的个数,进行操作
莫队这东西真的很无脑,只有两个函数自己要想想,其他都模板
数据大,先离散化处理
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack> using namespace std;
#define maxn 30005
#define LL long long
LL a[maxn],b[maxn]; int m,n;
struct node
{
int l,r,id;
} qu[100005];
int pos[100005];
LL ans[100005];
int cnt[100005];
LL Ans; bool cmp(node a,node b)
{
if(pos[a.l]==pos[b.l])
return a.r<b.r; return pos[a.l]<pos[b.l]; } int get(LL x)
{
int pos;
int l=1;
int r=n;
while(l<=r)
{
int m=(l+r)/2;
if(b[m]<x)
l=m+1;
else if(b[m]==x) pos=m,r=m-1;
else r=m-1;
}
return pos;
} void add(int x)
{
if(cnt[a[x]]==0)
Ans+=b[a[x]];
cnt[a[x]]++;
} void del(int x)
{
if(cnt[a[x]]==1)
Ans-=b[a[x]];
cnt[a[x]]--;
} int main()
{
int o;
while(~scanf("%d",&o))
{
while(o--)
{
scanf("%d",&n);
memset(cnt,0,sizeof(cnt));
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int kt=sqrt(n);
for(int i=1; i<=n; i++)
{ a[i]=get(a[i]);
pos[i]=i/kt;
} scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
} sort(qu+1,qu+1+m,cmp); int l=1;
int r=0;
Ans=0;
for(int i=1; i<=m; i++)
{
while(l<qu[i].l)
{
del(l);
l++;
}
while(l>qu[i].l)
{
l--;
add(l);
} while(r<qu[i].r)
{
r++;
add(r);
} while(r>qu[i].r)
{
del(r);
r--;
}
ans[qu[i].id]=Ans; }
for(int i=1; i<=m; i++)
printf("%lld\n",ans[i]); }
}
return 0;
}
hdu3333 Turing Tree 2016-09-18 20:53 42人阅读 评论(0) 收藏的更多相关文章
- PAT 甲 1005. Spell It Right (20) 2016-09-09 22:53 42人阅读 评论(0) 收藏
1005. Spell It Right (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...
- House Robber 分类: leetcode 算法 2015-07-09 20:53 2人阅读 评论(0) 收藏
DP 对于第i个状态(房子),有两种选择:偷(rob).不偷(not rob) 递推公式为: f(i)=max⎧⎩⎨⎪⎪{f(i−1)+vali,f(i−2)+vali,robi−1==0robi−1 ...
- ZOJ2417 Lowest Bit 2017-04-18 20:53 38人阅读 评论(0) 收藏
Lowest Bit Time Limit: 2 Seconds Memory Limit: 65536 KB Given an positive integer A (1 <= A ...
- 如何在hadoop中控制map的个数 分类: A1_HADOOP 2015-03-13 20:53 86人阅读 评论(0) 收藏
hadooop提供了一个设置map个数的参数mapred.map.tasks,我们可以通过这个参数来控制map的个数.但是通过这种方式设置map的个数,并不是每次都有效的.原因是mapred.map. ...
- 使用ganglia监控hadoop及hbase集群 分类: B3_LINUX 2015-03-06 20:53 646人阅读 评论(0) 收藏
介绍性内容来自:http://www.uml.org.cn/sjjm/201305171.asp 一.Ganglia简介 Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于测 ...
- Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏
Self Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22101 Accepted: 12429 De ...
- Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏
Binary Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6355 Accepted: 2922 Descr ...
- NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏
地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...
随机推荐
- Delphi编程实现是否开启“平滑屏幕字体边缘“
在Windows高级设置中的视觉效果中可以设置是否开启“平滑屏幕字休边缘”,可以通过编程的方式来实现: if SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, ...
- html5中的SessionStorage 和localStorage
html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...
- scala-- 内建控制结构
内建控制结构 scala 内建的控制结构很少,只有 if while for try match 和函数调用 几种. 因为scala 从语法层面支持函数字面量.几乎所有的scala控制结构都会产生 ...
- java多线程实例
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.concurr ...
- Mysql 中json 相关函数的使用
1.JSON_LENGTH: select content from test1 ["1","2","3","4",&q ...
- update from
update table1 set table1.column1 =(select table2.column1 from table2 where 关联条件) where exists(selec ...
- Url,HTTPUrlConnection(一)
package com.cmy.urlcon; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...
- python2.7中可以使用到的一些模块地址
1.reportlab:由很多部分组成且允许用户使用多种方法创建输出,地址: 下载ReportLab https://pypi.python.org/simple/reportlab/ http:// ...
- 苹果 重置APPID密保问题及更新开发者协议
[链接]重置AppleID密保问题 https://www.jianshu.com/p/37e7f2852eda [链接]苹果开发者计划许可协议更新:持续更新 https://www.jianshu. ...
- 如何进入/home/user/.wine
命令行输入 :cd /home/user/.wine/drive_c/windows/fonts /home是linux的用户目录,/user是用户名/.wine是隐藏目录,凡是以.开头的都是隐藏目录 ...