题意:
这和hdu 3333 根本就是一道题 链接:http://blog.csdn.net/u013382399/article/details/45689977
思路:
同hdu 3333
code:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<set> #include<cmath> #include<cstdlib> using namespace std; #define INF 0x3f3f3f3f #define PI acos(-1.0) #define mem(a, b) memset(a, b, sizeof(a)) #define mod 1000000007 typedef pair<int,int> pii; typedef long long LL; //------------------------------ const int maxn = 50005; const int maxq = 200005; struct node{ int s, e, id; bool operator < (const node nt) const{ if(e != nt.e) return e < nt.e; else return s < nt.s; } }qujian[maxq]; int n,q; LL a[maxn]; LL ans[maxq]; struct BIT { long long C[maxn]; void init() { memset(C, 0, sizeof(C)); } int lowbit(int x) { return -x&x; } long long get_Sum(int x) { long long ret = 0; while(x > 0) { ret += C[x]; x -= lowbit(x); } return ret; } void add(int x,long long v) { while(x <= n) { C[x] += v; x += lowbit(x); } } }bit; void init(){ scanf("%d",&n); for(int i = 1; i <= n; i++){ scanf("%I64d",&a[i]); } scanf("%d",&q); int st, ed; for(int i = 0; i < q; i++){ scanf("%d%d",&st, &ed); qujian[i].s = st; qujian[i].e = ed; qujian[i].id = i; } } map<long long, int> ma; void solve(){ sort(qujian, qujian+q); // for(int i = 0; i < q; i++){ // printf("==%d %d==\n",qujian[i].s, qujian[i].e); // // } ma.clear(); bit.init(); int start_ = 1; for(int i = 0; i < q; i++){ for(int j = start_; j <= qujian[i].e; j++){ if(ma.count(a[j])){ bit.add(ma[a[j]],-a[j]); } bit.add(j,a[j]); ma[a[j]] = j; } ans[qujian[i].id] = bit.get_Sum(qujian[i].e) - bit.get_Sum(qujian[i].s-1); start_ = qujian[i].e + 1; } for(int i = 0; i < q; i++){ printf("%I64d\n",ans[i]); } } int main(){ int t; scanf("%d",&t); while(t--){ init(); solve(); } return 0; }
用cin cout 会超时
时间: 2024-10-08 04:31:44