静态区间第K大值。主席树和划分树都可解。

 /* 3727 */
#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 typedef struct {
int q;
int l, r, k;
} Ques_t; const int maxq = ;
const int maxn = 1e5+;
const int maxm = maxn * ;
int a[maxn], m;
Ques_t Q[maxq];
int T[maxn];
int lson[maxm], rson[maxm], c[maxm];
int tot, cur, q; void init() {
tot = cur = m = ;
} int Build(int l, int r) {
int rt = tot++; c[rt] = ;
if (l == r)
return rt; int mid = (l + r) >> ; lson[rt] = Build(l, mid);
rson[rt] = Build(mid+, r); return rt;
} int Insert(int rt, int x, int delta) {
int nrt = tot++, ret = nrt;
int l = , r = m - , mid; c[nrt] = c[rt] + delta;
while (l < r) {
mid = (l + r) >> ;
if (x <= mid) {
lson[nrt] = tot++;
rson[nrt] = rson[rt];
nrt = lson[nrt];
rt = lson[rt];
r = mid;
} else {
lson[nrt] = lson[rt];
rson[nrt] = tot++;
nrt = rson[nrt];
rt = rson[rt];
l = mid + ;
}
c[nrt] = c[rt] + delta;
} return ret;
} int Query_kth(int lrt, int rrt, int k) {
int l = , r = m - , mid;
int tmp; while (l < r) {
mid = (l + r) >> ;
tmp = c[lson[rrt]] - c[lson[lrt]];
if (tmp >= k) {
lrt = lson[lrt];
rrt = lson[rrt];
r = mid;
} else {
k -= tmp;
lrt = rson[lrt];
rrt = rson[rrt];
l = mid + ;
}
} return l;
} int Query_Rank(int rt, int L, int R, int l, int r) {
if (L==l && R==r)
return c[rt]; int mid = (l + r) >> ; if (R <= mid)
return Query_Rank(lson[rt], L, R, l, mid);
else if (L > mid)
return Query_Rank(rson[rt], L, R, mid+, r);
else
return Query_Rank(lson[rt], L, mid, l, mid) + Query_Rank(rson[rt], mid+, R, mid+, r);
} void solve() {
__int64 ans1 = , ans2 = , ans3 = ;
int tmp, id; sort(a, a+m);
m = unique(a, a+m) - a;
T[cur] = Build(, m-); rep(i, , q) {
if (Q[i].q == ) {
id = lower_bound(a, a+m, Q[i].k) - a;
T[cur+] = Insert(T[cur], id, );
++cur;
continue;
} if (Q[i].q == ) {
id = Query_kth(T[Q[i].l-], T[Q[i].r], Q[i].k);
tmp = a[id];
ans1 += tmp;
} else if (Q[i].q == ) {
id = lower_bound(a, a+m, Q[i].k) - a;
tmp = Query_Rank(T[cur], , id, , m-);
ans2 += tmp;
} else {
id = Query_kth(T[], T[cur], Q[i].k);
tmp = a[id];
ans3 += tmp;
} #ifndef ONLINE_JUDGE
// printf("tmp = %d\n", tmp);
#endif
} printf("%I64d\n%I64d\n%I64d\n", ans1, ans2, ans3);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t = ;
char op[]; while (scanf("%d", &q)!=EOF) {
init();
rep(i, , q) {
scanf("%s", op);
if (op[] == 'I') {
scanf("%d", &Q[i].k);
a[m++] = Q[i].k;
Q[i].q = ;
} else if (op[] == '') {
scanf("%d %d %d", &Q[i].l, &Q[i].r, &Q[i].k);
Q[i].q = ;
} else if (op[] == '') {
scanf("%d", &Q[i].k);
Q[i].q = ;
} else {
scanf("%d", &Q[i].k);
Q[i].q = ;
}
}
printf("Case %d:\n", ++t);
solve();
} #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
for tt in xrange(t):
n = randint(200, 300)
fout.write("%d\n" % (n))
L = range(1, n+1)
shuffle(L)
cur = 0
has = []
for i in xrange(n):
op = randint(0, 3)
if cur==0:
op=0
if op==0:
idx = randint(0, len(L)-1)
x = L[idx]
L.remove(x)
has.append(x)
cur += 1
fout.write("Insert %d\n" % x)
elif op==1:
l = randint(1, cur)
r = randint(l, cur)
k = randint(1, r-l+1)
fout.write("Query_1 %d %d %d\n" % (l, r, k))
elif op==2:
idx = randint(0, len(has)-1)
x = has[idx]
fout.write("Query_2 %d\n" % x)
else:
x = randint(1, cur)
fout.write("Query_3 %d\n" % x) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()

【HDOJ】3727 Jewel的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. python(三)一个文件读写操作的小程序

    我们要实现一个文件读写操作的小程序 首先我们有一个文件 我们要以"============"为界限,每一个角色分割成一个独立的txt文件,按照分割线走的话是分成 xiaoNa_1. ...

  2. htmlcleaner

    String xpath = "//div"; Object[] myNodes = node.evaluateXPath(xpath); for (Object obj : my ...

  3. ECSHOP添加购物车加图片飞入效果

    为ECSHOP的添加购物车,加入图片飞入效果. 首先: 在goods.dwt中查找添加购物车按钮: 为添加购物车按钮加上id: 例如: <a id="iproduct_{$goods. ...

  4. 购买 CDRTools 2 正式版

    联系方式: Email:396390927@qq.com QQ: 396390927    QQ群: 26326434  组件价格: ¥50元/用户,免费更新: 此物为数字商品,并经过测试完全可用,谢 ...

  5. OFBiz进阶之HelloWorld(五)创建新实体

    参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guid ...

  6. 通过telnet命令进行网络邮件发送

    1.建立smtp邮箱服务连接 open smtp.sina.com 2.连接上邮箱服务后进行握手操作 helo smtp.sina.com 3.输入帐号密码进行验证::此步后缓冲区会输出一些字符,你只 ...

  7. GNU_makefile_template

    #g++ compiler: options # -std=c++0x enables ISO C++ 11 standard # -I.. pulls in the Version_test.h f ...

  8. phpstorm运行在浏览器中执行php文件报502错误

    原因是之前mac自带的php5.5版本被我升级到了5.6 通过phpinfo()查看到目前php5.6的安装目录 重新制定一些interpreter的路径 /usr/local/php5/bin 就可 ...

  9. Asp.net MVC 如何向webform一样在IIS里添加虚拟目录

    相信很多用webform的程序猿都习惯性的使用虚拟目录的形式来对一个程序添加新的功能,那么在mvc下该如何来弄呢? 首先得有一个项目基层的项目,然后我们在这个项目的基础上新增一个功能模块,例如信息发布 ...

  10. CLR.via.C#第三版 读书笔记

    第一章 CLR的执行模型 1.1将源代码编译成托管代码 决定将.NET Framework作为自己的开发平台之后,第一步是决定要生成什么类型的应用程序或组件.假定你已经完成了这些次要的细节:一切都已经 ...