튜링 트리 솔루션을 쉽게 할 수 있기 때문에, 간격에 대한 문제를 해결할 때 튜링 트리를 발명 한 후, 3xian는 항상 지루 느꼈다. 뿐만 아니라, 교활 3xian은 간격에 대한 새로운 문제를 많이했다. 그래서, 오늘이 아픈 일이 다시 발생 ...
이제 N 번호 A1의 순서를 주어 , A2, ..., 각 쿼리 (I, J)에 대한 및 쿼리의 숫자 (I, J) (1≤i≤j≤N)., 당신은을 caculate 할 수 있습니다 서브 시퀀스 아이 구별 값의 합 , 아이 + 1, ...에서 Aj는.
주어진 m 질의 들어 길이 n의 시퀀스를 소정의 각 쿼리 간격 [L, R]과 다른 요소의 범위.
INPUT
. 첫 번째 행의 T는 아래의 테스트 케이스의 수 indecating 정수. (1 ≤ T ≤ 10) IS
각각의 경우에, 포맷이 입력은 다음과 같이 될 것이다를 들어 :
- 행 1 : N (30,000 ≤ 1 ≤ N).
- 2 호선 : N 정수 A1, A2, ..., (0 ≤ 아이 ≤ 1,000,000,000).
- 라인 3 : Q (1 ≤ Q ≤ 10), 질의의 수.
- 다음 Q 라인은 각 라인은 2 개 포함 정수 I, 쿼리 (1 ≤ i가 j 개의 ≤ N의 하로)을 나타내는 J 제 정수 T는 데이터 세트의 수를 나타낸다.
각 시험, 정수 (N)의 첫번째 행, 시퀀스 요소의 수.
N 개의 요소의 두 번째 행.
정수 Q의 세 번째 줄, 의문을 제기 사람들의 그룹의 수.
두 개의 정수 L 및 R LT의 다음 라인 Q
출력
각각에 대한 쿼리, 줄 하나로 지정할 DISTINCT 순서의, SUM 값을 출력한다.
각각의 질의 결과 출력을 위해.
입력 샘플
(2)
. (3)
. (1) 제 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';
}
}
}