Description
After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again…
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.
Input
The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below.
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).
Output
For each Query, print the sum of distinct values of the specified subsequence in one line.
Sample Input
2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
Sample Output
1
5
6
3
6
是区间gcd的弱化版
没啥好优化的,裸的水题
就是看看前边有没有,有更新坐标,没有就加上。
感觉树状数组用的比线段树舒服
没有递归感觉舒服的很
#include<iostream>
#include<algorithm>
#include<memory.h>
#include<vector>
using namespace std;
long long n, q, lisan[30001], lixian[100001],shuru[30001],tu[30001],zuihou[30001];
long long lowb(long long x) { return x&-x; }
void gengxin(long long zuobiao, long long zhi)
{
for (long long a = zuobiao;a <= n;a += lowb(a))
{
tu[a] += zhi;
}
}
struct p
{
long long zuo,tihao;
};
long long gs(long long zuobiao)
{
long long s = 0;
for (long long qq = zuobiao;qq > 0;qq -= lowb(qq))
{
s += tu[qq];
}
return s;
}
vector<p> dd[30001];
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
memset(lixian, 0, sizeof(lixian));
memset(lisan, 0, sizeof(lisan));
memset(shuru, 0, sizeof(shuru));
memset(tu, 0, sizeof(tu));
memset(zuihou, 0, sizeof(zuihou));
for (int a = 1;a <= n;a++)
{
scanf("%lld", &shuru[a]);
lisan[a] = shuru[a];
dd[a].clear();
}
sort(lisan + 1, lisan + 1 + n);
int m;
cin >> m;
long long qw, we;
for (int a = 1;a <= m;a++)
{
scanf("%lld%lld", &qw, &we);
dd[we].push_back({ qw,a });
}
for (int a = 1;a <= n;a++)
{
long long zuobiao = lower_bound(lisan + 1, lisan + n + 1, shuru[a])-lisan;
if (a > zuihou[zuobiao])
{
if (zuihou[zuobiao])gengxin(zuihou[zuobiao], -shuru[a]);
gengxin(a, shuru[a]);
zuihou[zuobiao] = a;
}
for (int b = 0;b < dd[a].size();b++)
{
lixian[dd[a][b].tihao] = gs(a) - gs(dd[a][b].zuo - 1);
}
}
for (int a = 1;a <= m;a++)printf("%lld\n", lixian[a]);
}
return 0;
}
505



被折叠的 条评论
为什么被折叠?



