版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/SCNU_Jiechao/article/details/24406391

题意:Wiskey招女友,每一个女生看其身高、活泼度和缘分值。如今运行两种操作,1、I。增加一位女生的身高。活泼度和缘分值;2、Q,查询身高在H1, H2之间,活泼度在A1, A2之间的女生的最高缘分值。

题目链接:

pid=1823" rel="nofollow">http://acm.hdu.edu.cn/showproblem.php?pid=1823

——>>查询某个区间的最值,若是一维,可用RMQ解法。。也可用线段树解法。。

如今要查身高限一个区间。活泼度限一个区间,是一个二维的情景。

。将线段树扩至二维。。时间复杂度:O(N+M*log(N)^2)。。

——>>坑1:G++的BUG。。

。以G++提交数次皆WA。

改交C++即过。。有朋友指出是代码触发了没有定义行为,也有朋友说是由于G++的O2 BUG。

——>>坑2:题目中说是1位小数。那么。处理方法用scanf("%d.%d", &A1, &A2)。再进行A1 * 10 + A2,会比用scanf("%lf", &A),再进行(int)(A * 10)更精确(自己測下0.0到0.9就能够发现)。。但是,用第一种处理方式却会WA,偏偏要用另外一种不那么精确的处理方式才会AC。。

第一道二维线段树题目,做到想哭了。

#include <cstdio>
#include <algorithm> using namespace std; #define lc (o<<1)
#define rc ((o<<1)|1) const int maxh = 100 + 10;
const int maxa = 1000 + 10;
int mmax[maxh<<2][maxa<<2]; void build_A(int O, int o, int L, int R) { //活泼度建树
mmax[O][o] = -1;
if(L == R) return;
int M = (L + R) >> 1;
build_A(O, lc, L, M);
build_A(O, rc, M+1, R);
} void build_H(int o, int L, int R) { //身高建树
build_A(o, 1, 0, 1000);
if(L == R) return;
int M = (L + R) >> 1;
build_H(lc, L, M);
build_H(rc, M+1, R);
} void Insert_A(int O, int o, int L, int R, int A, int D) { //依据活泼度建树
if(L == R) {
mmax[O][o] = max(mmax[O][o], D);
return;
}
int M = (L + R) >> 1;
if(A <= M) Insert_A(O, lc, L, M, A, D);
else Insert_A(O, rc, M+1, R, A, D);
mmax[O][o] = max(mmax[O][lc], mmax[O][rc]);
} void Insert(int o, int L, int R, int H, int A, int D) {
Insert_A(o, 1, 0, 1000, A, D);
if(L == R) return;
int M = (L + R) >> 1;
if(H <= M) Insert(lc, L, M, H, A, D);
else Insert(rc, M+1, R, H, A, D);
} int query_A(int O, int o, int L, int R, int A1, int A2) { //依据活泼度查询
if(A1 <= L && R <= A2) return mmax[O][o];
int M = (L + R) >> 1;
int Max1 = -1, Max2 = -1;
if(A1 <= M) Max1 = query_A(O, lc, L, M, A1, A2);
if(A2 > M) Max2 = query_A(O, rc, M+1, R, A1, A2);
return (Max1 > Max2) ? Max1 : Max2;
} int query(int o, int L, int R, int H1, int H2, int A1, int A2) {
if(H1 <= L && R <= H2) return query_A(o, 1, 0, 1000, A1, A2);
int M = (L + R) >> 1;
int Max1 = -1, Max2 = -1;
if(H1 <= M) Max1 = query(lc, L, M, H1, H2, A1, A2);
if(H2 > M) Max2 = query(rc, M+1, R, H1, H2, A1, A2);
return (Max1 > Max2) ? Max1 : Max2;
} int main()
{
int M;
char op;
while(scanf("%d", &M) == 1 && M) {
build_H(1, 100, 200);
for(int i = 0; i < M; i++) {
getchar();
op = getchar();
if(op == 'I') {
// int H, A, A1, A2, D, D1, D2;
// scanf("%d %d.%d %d.%d", &H, &A1, &A2, &D1, &D2);
// A = A1 * 10 + A2;
// D = D1 * 10 + D2;
int H, A, D;
double AA, DD;
scanf("%d%lf%lf", &H, &AA, &DD);
A = (int)(AA * 10);
D = (int)(DD * 10);
Insert(1, 100, 200, H, A, D);
}
else {
// int H1, H2, A[6];
// scanf("%d %d %d.%d %d.%d", &H1, &H2, A+2, A+3, A+4, A+5);
// A[0] = A[2] * 10 + A[3];
// A[1] = A[4] * 10 + A[5];
int H1, H2, A1, A2;
double AA, BB;
scanf("%d%d%lf%lf", &H1, &H2, &AA, &BB);
A1 = (int)(AA * 10);
A2 = (int)(BB * 10);
if(A1 > A2) swap(A1, A2);
if(H1 > H2) swap(H1, H2);
// if(A[0] > A[1]) swap(A[0], A[1]);
// int ret = query(1, 100, 200, H1, H2, A[0], A[1]);
int ret = query(1, 100, 200, H1, H2, A1, A2);
ret != -1 ? printf("%.1f\n", ret/10.0) : puts("-1");
}
}
}
return 0;
}

hdu - 1823 - Luck and Love(线段树)的更多相关文章

  1. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  5. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  6. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  7. hdu 1754 I Hate It 线段树 点改动

    // hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...

  8. hdu 1166 敌兵布阵 线段树 点更新

    // hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...

  9. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

随机推荐

  1. codeforces 576a//Vasya and Petya's Game// Codeforces Round #319 (Div. 1)

    题意:猜数游戏变种.先选好猜的数,对方会告诉你他想的那个数(1-n)能不能整除你猜的数,问最少猜几个数能保证知道对方想的数是多少? 对一个质数p,如果p^x不猜,那么就无法区分p^(x-1)和p^x, ...

  2. android--------listview之适配器

    ListView之适配器的使用,包含了ArrayAdapter,SimpleAdapter ,BaseAdapter等适配器. 1:ArrayAdapter /**** * * * ArrayAdap ...

  3. 『Kaggle』Sklearn中几种分类器的调用&词袋建立

    几种分类器的基本调用方法 本节的目的是基本的使用这些工具,达到熟悉sklearn的流程而已,既不会设计超参数的选择原理(后面会进行介绍),也不会介绍数学原理(应该不会涉及了,打公式超麻烦,而且近期也没 ...

  4. 基础的shell脚本

    #! /bin/sha="hello world"echo "A is "   echo $a echo "<br />" ec ...

  5. UVA-1374 Power Calculus (迭代加深搜索)

    题目大意:问最少经过几次乘除法可以使x变成xn. 题目分析:迭代加深搜索. 代码如下: # include<iostream> # include<cstdio> # incl ...

  6. 申请和使用github共享代码

    1.申请github帐号 https://github.com/join?source=header-home 2.创建项目 2.1 或者: 2.2 输入信息 2.3创建成功,地址及基本命令提示 3. ...

  7. asp.net MVC html.ActionLink的几种参数格式

    一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...

  8. 主席树模板(poj 2104&&poj2761)

    主席树,就是n个线段树,用nlonn的空间实现 首先建立第一个线段树 把要查询的值离散化,建立值的线段树 每一次加入一个点 显然每一次只会修改logn个点 把其他的点直接建边连接即可 代码: #inc ...

  9. 抓取错误之onerror

    一处定义,可以抓取全局的错误,相当于一个全局的try catch呀. <html> <head> <script type="text/javascript&q ...

  10. L1-001 Hello World

    这道超级简单的题目没有任何输入. 你只需要在一行中输出著名短句“Hello World!”就可以了. 输入样例: 无 输出样例: Hello World! #include<stdio.h> ...