チューリングツリーは簡単に解決策を持っている可能性があるため、間隔に関する問題を解決する際にチューリングツリーを発明した後、3xianは常に退屈を感じました。同様に、狡猾な3xianは間隔に関する新たな問題の多くを作りました。だから、今日、この病気の事は再び起こります...
今、N番号A1のシーケンス与えられ 、A2、...、ANとクエリの数(i、j)は(1≤i≤j≤Nを)。各クエリ(i、j)のために、あなたがcaculateになっていますサブアイにおける異なる値の和 、愛+ 1、...、Ajは。
与えられたm個のクエリのために、長さnの配列が与えられると、各クエリ区間[L、R]と異なる要素の範囲。
INPUT
最初の行Tは整数のうち、以下のテストケースの数indecating。(1≤T≤10)。
各ケースについて、フォーマットはINPUTは次のようになります:
- 1行目:N(1≤N≤3万)。
- 2行目:Nの整数A1、A2、...、AN(0≤≤愛10億)。
- 3行目:Q(1≤Q≤10万)、クエリの数。
- 次Q線:各ラインは2つの含有整数クエリを表すI、J(1≤iはj個の≤Nを≤)を最初の整数T、データセットの数を表します。
各試験のために、整数nの最初の行、配列の要素数。
n個の要素の第2行。
整数qの3行目、疑問それらのグループの数。
二つの整数LとR&LTの次Qライン
出力
クエリごとに、1ライン目に指定DISTINCTサブシーケンスの和の値を出力します。
各クエリについて、結果の出力を。
入力サンプル
2
。3
。1 4 1
2
1 2
2 3
。5
。1. 1. 1. 3 2
。3
。1 5
2 4
3 5
サンプル出力
1
。5
。6
。3
。6
イタリア語タイトル:
中国の下の英語翻訳
アイデア:
最初のセクションの情報を読み込み、その後、右のポイントを並べ替え、その後、各セクションの情報オフライン処理、およびxの値の最後に出現する位置を維持するためにマップを使用しています。
詳細コードを参照してください。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 30010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int q;
int n;
ll tree[maxn];
int lowbit(int x)
{
return -x&x;
}
void add(int x,ll val)
{
while(x<maxn)
{
tree[x]+=val;
x+=lowbit(x);
}
}
ll ask(int x)
{
ll res=0;
while(x)
{
res+=tree[x];
x-=lowbit(x);
}
return res;
}
ll a[maxn];
map<ll,int> vis;
struct node
{
int l,r;
int id;
}b[100010];
bool cmp(node aa,node bb)
{
return aa.r<bb.r;
}
bool cmp2(node aa,node bb)
{
return aa.id<bb.id;
}
ll ans[100010];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
int t;
cin>>t;
while(t--)
{
MS0(tree);
vis.clear();
cin>>n;
repd(i,1,n)
{
cin>>a[i];
}
int q;
cin>>q;
repd(i,1,q)
{
cin>>b[i].l;
cin>>b[i].r;
b[i].id=i;
}
sort(b+1,b+1+q,cmp);
int p=1;
repd(i,1,q)
{
while(p<=b[i].r)
{
if(vis[a[p]])
{
add(vis[a[p]],-a[p]);
add(p,a[p]);
vis[a[p]]=p;
}else
{
vis[a[p]]=p;
add(p,a[p]);
}
p++;
}
ans[b[i].id]=ask(b[i].r)-ask(b[i].l-1);
}
sort(b+1,b+1+q,cmp2);
repd(i,1,q)
{
cout<<ans[b[i].id]<<endl;
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}