题意:求一个序列中所有数字的和,其中数值相同的只能计算一次。
做法:可以先储存所有的请求,然后按照它们的右边界排序,在查询的同时更新区间。这里其实有一点点DP的味道,在它进行某个查询之前,保证所有的重复数字都被删除光了,并且有不能影响其他查询,所以呢,只能从最近的那个操作进行计算。1次query即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#define left l,m,x<<1
#define right m+1,r,x<<1|1
#define LL __int64
const int LMT=50005;
using namespace std;
int a[LMT],pre[1000003];
LL ans[200003],sum[LMT<<2];
struct line
{
int l,r,id;
bool operator<(const line &y)const
{
return r<y.r;
}
}e[200003];
void init(void)
{
memset(pre,-1,sizeof(pre));
memset(sum,0,sizeof(sum));
}
void update(int op,int pos,int l,int r,int x)
{
if(l==r)
{
sum[x]=op;
return;
}
int m=(l+r)>>1;
if(pos<=m)update(op,pos,left);
if(pos>m)update(op,pos,rig