#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = + ;
ll a[ + ]; struct tree
{
ll l , r;
ll val;
}t[maxn << ]; void push_up(ll rt){
//if(t[rt << 1].val != t[rt << 1|1].val)
t[rt].val = t[rt << ].val + t[rt << |].val;
//else
// t[rt].val += t[rt << 1].val ;
} void build(ll rt,ll l ,ll r){
t[rt].l = l;
t[rt].r = r;
t[rt].val = ;
if(l == r){
//t[rt].val = a[l];
return;
}
ll mid = (l + r) >> ;
build(rt << , l , mid);
build(rt << |,mid+,r);
push_up(rt);
} void updata(ll rt,ll x,ll v){
if(x == t[rt].l && t[rt].r == x){
t[rt].val += v;
return;
}
ll mid = (t[rt].l + t[rt].r) >> ;
if(x <= mid) updata(rt << , x , v);
else updata(rt << |, x , v);
push_up(rt);
} ll query(ll rt,ll l,ll r){
if(l <= t[rt].l && r >= t[rt].r) return t[rt].val;
ll ret = ;
ll mid = (t[rt].l + t[rt].r) >> ;
// if(r <= mid) return query(rt << 1, l, r);
// else if(l > mid) return query(rt << 1|1,l,r);
// else return query(rt << 1,l,mid) + query(rt << 1|1,mid + 1,r);
if (l <= mid) ret += query(rt << , l ,r);
if( r > mid) ret += query(rt << |,l,r);
push_up(rt);
return ret;
} struct node
{
ll x,y,id;
}nod[ + ];
map<ll,ll> mp;
bool cmp(node s1 ,node s2){
//if(s1.y == s2.y)
// return s1.x < s2.x;
return s1.y < s2.y; }
ll ans[ + ];
int main(int argc, char const *argv[])
{
ll tt;
scanf("%lld",&tt);
while(tt--){
ll n;
memset(a,,sizeof a);
memset(t,,sizeof t);
memset(ans,,sizeof ans);
mp.clear();
memset(nod,,sizeof nod);
scanf("%lld",&n);
for(ll i = ;i <= n ; i++){
scanf("%lld",&a[i]);
}
build(,,n);
ll q;
scanf("%lld",&q);
for(ll i = ;i <= q; i++){
ll a,b;
scanf("%lld%d",&a,&b);
//printf("%lld\n",query(1,a,b));
nod[i].x = a,nod[i].y = b,nod[i].id = i;
}
ll i = ;
sort(nod + ,nod + q + ,cmp); for(ll j = ; j <= q;j ++){
for( ;i <= nod[j].y ; i++){
if(mp[a[i]])
//mp[a[i]] = 1; updata(,mp[a[i]],-a[i]);
// mp[a[i]] =
//cout << 1 ;
mp[a[i]] = i;
updata(,i,a[i]);
}
// cout << 3 << endl ;
// cout << nod[j].id << " " << nod[j].x << " " << nod[j].y <<endl;
ans[nod[j].id] = query(,nod[j].x,nod[j].y);
// cout << 1 << endl;
}
// cout << 1 ;
//cout << q << endl;
for(ll i = ;i <= q;i ++){
printf("%lld\n",ans[i] );
}
}
return ;
}

线段树

最新文章

  1. Java for LeetCode 211 Add and Search Word - Data structure design
  2. yum的使用及配置
  3. 后台向前台传递entity的list然后构筑combobox的方法(easyui)
  4. 详解android.mk-2016.01.18
  5. ASP.NET MVC实现多个按钮提交事件
  6. PHP 单一入口
  7. 【从零开始,从内核驱动驱动到用户空间调用】编写第一个linux驱动,通过端口访问I/O寄存器。
  8. applicationContext.xml文件配置模板
  9. bootstrap file input 官方文档翻译
  10. JS对时间的操作
  11. 实用技巧:如何通过IP地址进行精准定位
  12. bzoj2823[AHOI2012]信号塔
  13. TCP/IP入门(2) --网络层
  14. BZOJ2618[Cqoi2006]凸多边形——半平面交
  15. Codeforces Round #441 D. Sorting the Coins(模拟)
  16. C++调用API获取当前时间
  17. 使用 maven 搭建web开发基本架构
  18. python 抓取搜狗微信出现的问题,求大神解决
  19. UBUNTU下MONGODB出现PHP Fatal error: Uncaught exception &#39;MongoConnectionException&#39; with message 和 Authentication failed on database &#39;admin&#39; with username
  20. MsSql 游标 修改字段两个表关联 表向另个表插入记录

热门文章

  1. 【leetcode】1161. Maximum Level Sum of a Binary Tree
  2. C 实现 创建多个txt文件,并以自然数列命名,然后将产生的十进制数据写入txt文档
  3. js-将传来的数据排序,让(全部)这个小按钮小圈圈,始终排列在最前面
  4. 如何在Web页面里使用高拍仪扫描上传图像
  5. php开发IDE选择
  6. RESTful风格编程
  7. ASYNC_NETWORK_IO等待事件和调优
  8. 内存地址 Memory Management
  9. 【转】python---方法解析顺序MRO(Method Resolution Order)&lt;以及解决类中super方法&gt;
  10. vue-element-template模板项目使用记录(持续更新)