划分树解。
主席树解MLE。
/* 3473 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define LL __int64 const int maxn = 1e5+;
int order[maxn];
int val[][maxn];
int toLeft[][maxn];
LL sum[][maxn];
int n;
LL ans; void Build(int l, int r, int dep) {
if (l == r) {
toLeft[dep][l] = toLeft[dep][l-] + ;
sum[dep][l] = sum[dep][l-] + val[dep][l];
return ;
} int mid = (l + r) >> ;
int same = mid - l + ;
rep(i, l, r+) {
if (val[dep][i] < order[mid])
--same;
} int lpos = l, rpos = mid + ;
rep(i, l, r+) {
if (val[dep][i] < order[mid]) {
val[dep+][lpos++] = val[dep][i];
} else if (val[dep][i]==order[mid] && same>) {
val[dep+][lpos++] = val[dep][i];
--same;
} else {
val[dep+][rpos++] = val[dep][i];
} sum[dep][i] = sum[dep][i-] + val[dep][i];
toLeft[dep][i] = toLeft[dep][l-] + lpos - l;
} Build(l, mid, dep+);
Build(mid+, r, dep+);
} int Query(int l, int r, int k, int L, int R, int dep) {
if (l == r)
return val[dep][l]; int mid = (L + R) >> ;
int tmp = toLeft[dep][r] - toLeft[dep][l-]; if (tmp >= k) {
int s = mid++l-L-(toLeft[dep][l-] - toLeft[dep][L-]);
int e = mid+r-L+ - (toLeft[dep][r] - toLeft[dep][L-]); ans += sum[dep+][e] - sum[dep+][s-]; int ll = L + toLeft[dep][l-] - toLeft[dep][L-];
int rr = ll + tmp - ; return Query(ll, rr, k, L, mid, dep+);
} else {
int s = L + toLeft[dep][l-] - toLeft[dep][L-];
int e = s + tmp - ; ans -= sum[dep+][e] - sum[dep+][s-]; k -= tmp;
int rr = r + toLeft[dep][R] - toLeft[dep][r];
int ll = rr - (r-l+ - tmp) + ; return Query(ll, rr, k, mid+, R, dep+);
}
} void solve() {
int q; sort(order+, order++n);
Build(, n, ); int l, r, kth;
int tmp; scanf("%d", &q);
while (q--) {
scanf("%d %d", &l, &r);
++l;
++r;
kth = ((l + r) >> ) - l + ;
ans = ;
tmp = Query(l, r, kth, , n, );
if (((r-l+) & ) == )
ans -= tmp;
printf("%I64d\n", ans);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
rep(tt, , t+) {
scanf("%d", &n);
rep(i, , n+) {
scanf("%d", &val[][i]);
order[i] = val[][i];
}
printf("Case #%d:\n", tt);
solve();
putchar('\n');
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
数据发生器。
from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 10
bound = 10**5
fout.write("%d\n" % (t))
for tt in xrange(t):
n = bound
q = randint(10, 20)
fout.write("%d\n" % (n))
L = []
for i in xrange(n):
x = randint(1, bound)
L.append(x)
fout.write(" ".join(map(str, L)) + "\n")
fout.write("%d\n" % (q))
for i in xrange(q):
l = randint(0, n-1)
r = randint(l, n-1)
fout.write("%d %d\n" % (l, r)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()