Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3103 Accepted Submission(s): 1063
Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
2 6 1 2 3 4 3 5 3 1 2 3 5 2 6 6 1 1 1 2 3 5 3 1 1 2 4 3 5
3 7 14 1 3 6
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int mn=50000+100;
const int mm=200000+100;
struct node{
int l,id;
};
ll f[mn],res[mm];
ll a[mn],pre[1000010];
vector<node> p[mn];
int n,m;
inline int lowbit(int i)
{
return i&(-i);
}
void add(int i,ll x)
{
while(i<=n)
{
f[i]+=x;
i+=lowbit(i);
}
}
ll query(int i)
{
ll ret=0;
while(i>0)
{
ret+=f[i];
i-=lowbit(i);
}
return ret;
}
void solve()
{
node temp;
MST(pre,-1);
CLR(f);
FOR(i,1,n)
{
if(pre[a[i]]!=-1)
add(pre[a[i]],-a[i]);
pre[a[i]]=i;
add(i,a[i]);
REP(j,p[i].size())
{
temp=p[i][j];
res[temp.id]=query(i)-query(temp.l-1);
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&n);
FOR(i,1,n)
{
scanf("%I64d",&a[i]);
p[i].clear();
}
node temp;
int r;
scanf("%d",&m);
REP(i,m)
{
scanf("%d%d",&temp.l,&r);
temp.id=i;
p[r].PB(temp);
}
solve();
REP(i,m)
printf("%I64d\n",res[i]);
}
return 0;
}
304




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



