HDU 3874 Necklace 区间查询的离线操作
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3874
对需要查询的区间按右端点排序,然后从左到右依次加入序列中的元素,同时更新,更新的方法是,把上一次出现a[i]值的点变为0,这一次a[i]值的点(即 i)变为a[i],这样保证了前i个元素中只存在一个等于a[i]值得元素,那为什么这样不会影响后面的查询呢?
因为是处理到i点,则把右边界等于a[i]的查询处理掉,剩下的待查询的区间右边界在i点之后,如果左边界在i之前,那么也会包含i点,也就包含了i点的值,由于i以前没有等于a[i]的点,所以只包含了一个这样的值,如果左边界在i之后,前面的操作对它就没影响了。也可以这样理解,当前处理到i点,如果后面的待查询区间的左边界要包含上一个值为a[i]的点,那么它必须也包含了i点,所以i之前等于a[i]的点完全可以舍弃-----------------原来先排序的处理方式还有个专业名字叫=======离线操作
话不多说,看代码:
/**********************************************
*** Problem:
*** Author: JKL
*** University: CSUST
*** Team: __Dream
*** Email: 1451108308@QQ.COM
*** My Blog: http://www.cnblogs.com/jklongint/
***********************************************/
//===================================================
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <numeric>
#include <ctime>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <set>
#include <bitset>
#include <deque>
using namespace std;
//---------------------------------------------------
#define mem(a,b) memset(a,b,sizeof(a))
#define GO cout<<"HelloWorld!"<<endl
#define Case(x) cout<<"Case "<<x<<":"
#define foru(i,n) for(int i=1; i <= n; i++)
#define ford(i,n) for(int i = n; i >= 1; i--)
#define fin freopen("input.txt","r",stdin);
#define fout freopen("output.txt","w",stdout)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 #define sqr(a) ((a)*(a))
#define abs(a) ((a>0)?(a):-(a))
#define pii pair<int,int> #define fmax(a,b) max(a,b)
#define fmin(a,b) min(a,b)
#define fmax3(a,b,c) (fmax(a,fmax(a,b)))
#define fmin3(a,b,c) (fmin(a,fmin(a,b))) #define sfi(x) scanf("%d",&x)
#define sfL(x) scanf("%I64d",&x)
#define sfc(x) scanf("%c",&x)
#define sfd(x) scanf("%lf",&x)
#define sfs(x) scanf("%s",x)
#define sfii(a,b) scanf("%d%d",&a,&b)
#define sfLL(a,b) scanf("%I64d%I64d",&a,&b)
#define sfcc(a,b) scanf("%c%c",&a,&b)
#define sfdd(a,b) scanf("%lf%lf",&a,&b)
#define sfss(a,b) scanf("%s%s",a,b) #define pfi(x) printf("%d",x)
#define pfL(x) printf("%I64d",x)
#define pfs(x) printf("%s",x)
#define pfd(x) printf("%lf",x)
#define pfc(x) print("%c",x)
#define newLine pfs("\n")
#define space pfs(" ") //--------------------------------------------------------
typedef __int64 LL;
typedef unsigned long long ULL;
//typedef __int64 __LL;
typedef unsigned __int64 __ULL; typedef vector<int> vi;
typedef vector<LL> vL;
typedef vector<string> vs;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<LL,LL> mLL;
typedef map<string,int> msi;
typedef map<char,int> mci;
//--------------------------------------------------------
const int dx[]={,-,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
const int N6=;
const int N5=;
const int N4=;
const int N3=;
const int N2=;
const int N=;
const int MOD=;
const LL LMAX=0x7fffffffffffffff;
const LL IMAX=0x3fffffff;
const double PI=3.14159265359;
//--------------------------------------------------------
template< class T > T gcd(T a, T b) { return (b != ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } //------------------------------------------------------------
struct TreeNode{
LL sum;
};
struct Node{
int l, r, id;
};
//=================================================================
TreeNode tree[N << ];
Node node[N];
int a[N], last[];
LL ans[N];
int cmp(Node i, Node j)
{
return i.r < j.r ;
}
void PushUP(int rt)
{
tree[rt].sum = tree[rt << ].sum + tree[rt << | ].sum;
}
void update(int p, int x, int l , int r, int rt)
{
if(l == r){
tree[rt].sum += x;
return;
}
int m = (l + r) >> ;
if(p <= m)update(p, x, lson);
else update(p, x, rson);
PushUP(rt);
}
LL query(int L, int R, int l, int r, int rt)
{
if(L <= l && R >= r){
return tree[rt].sum;
}
int m = (l + r) >> ;
LL res = ;
if(L <= m) res += query(L, R, lson);
if(R > m) res += query(L, R, rson);
return res;
}
void build(int l, int r, int rt)
{
if(l == r){
tree[rt].sum = ;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
PushUP(rt);
}
int main()
{
//fin;//fout;//freopen("input.txt","r",stdin);
int n, m, T;
cin >> T;
while(T--){
cin >> n ;
foru(i, n)sfi(a[i]);
cin >> m;
foru(i, m)sfii(node[i].l, node[i].r),node[i].id = i;
sort(node + , node + + m, cmp);
build(, n, );
int np = ;
mem(last, );
foru(i, n){
if(last[a[i]])update(last[a[i]], -a[i], , n, );
update(i, a[i], , n, );
while(node[np].r == i && np <= m){
ans[node[np].id] = query(node[np].l, node[np].r, , n, );
np++;
}
last[a[i]] = i;
}
foru(i, m)pfL(ans[i]),newLine;
}
return ;
}
HDU 3874 Necklace 区间查询的离线操作的更多相关文章
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 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 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- HDU - 3874 Necklace (线段树 + 离线处理)
欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 3874 Necklace(线段树)
这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...
- HDU 3874 Necklace
莫队算法. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #in ...
- HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
随机推荐
- 5. iphone 的:active样式
如果给按钮定义 :hover 样式,在 iPhone 上按钮点击一次是 hover 态,再点击一次 hover 态才会消失,这不是我们想要的,继而想通过定义 :active 样式来实现按钮按下时的效果 ...
- Java 多线程 -- 协作模型:生产消费者实现方式二:信号灯法
使用信号灯法实现生产消费者模式需要借助标志位. 下面以演员表演,观众观看电视为列,写一个demo 同一资源 电视: //同一资源 电视 class Tv { String voice; // 信号灯 ...
- Netty 中的心跳检测机制
心跳检测一般存在于建立长连接 或者 需要保活的场景. 心跳的使用场景 长连接的应用场景非常的广泛,比如监控系统,IM系统,即时报价系统,推送服务等等.像这些场景都是比较注重实时性,如果每次发送数据都要 ...
- sqlilab11-14
less11 抓包 ' " 实验发现'构成闭合,存在注入点 less-12 a,b都有注入点,b比较好判断闭合 less13 less14
- Git基本操作和使用
基本命令: git config git init git clone git remote git fetch git commit git rebase git push 本地基本操作: git ...
- 小程序里button边框有黑线解决办法(自定义button样式)
.go_to_user::after{ border:1px solid transparent; } button的class为go_to_user button{ padding:; box-si ...
- Linux安全实验缓冲区溢出
缓冲区溢出实验: 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器和返回地址的暂时关 ...
- Python(5)
把 aaabbcccd 这种形式的字符串压缩成 a3b2c3d1 这种形式. print(''.join({i+str(s.count(i)) for i in s})) dic={} for i i ...
- 2019-2020-1 20199329 第二周测试(环境:ubuntu64位)
2019-2020-1 20199329 第二周测试(环境:ubuntu64位) 实验一 0.每个.c一个文件,每个.h一个文件,文件名中最好有自己的学号 1.用Vi输入图中代码,并用gcc编译通过 ...
- jstat命令查看JVM 的GC状态
转载于 https://www.cnblogs.com/alter888/p/10407952.html jstat命令可以查看堆内存各部分的使用量,以及加载类的数量.命令的格式如下: jstat ...