线段树的简单题目,做一个离散化,O(lgn)可以找到id。RE了一晚上,额,后来找到了原因。

 /* 555C */
#include <iostream>
#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 x, y;
char op[];
} node_t; const int maxn = 2e5+;
const int maxm = 4e5+;
node_t Q[maxn];
int rn[maxm];
int mxr[maxm<<];
int mxc[maxm<<];
int rL[maxm<<];
int cT[maxm<<];
int L, R; void build(int l, int r, int rt) {
rL[rt] = ;
cT[rt] = ;
if (l == r)
return ; int mid = (l+r)>>;
build(lson);
build(rson);
} inline void PushDownR(int rt) {
int lb = rt<<;
int rb = lb | ; if (mxr[rt]) {
rL[rt] = max(rL[rt], mxr[rt]);
mxr[lb] = max(mxr[lb], mxr[rt]);
mxr[rb] = max(mxr[rb], mxr[rt]);
mxr[rt] = ;
}
} inline void PushDownC(int rt) {
int lb = rt<<;
int rb = lb | ; if (mxc[rt]) {
cT[rt] = max(cT[rt], mxc[rt]);
mxc[lb] = max(mxc[lb], mxc[rt]);
mxc[rb] = max(mxc[rb], mxc[rt]);
mxc[rt] = ;
}
} inline void PushDown(int rt) {
int lb = rt<<;
int rb = lb | ; if (mxr[rt]) {
rL[rt] = max(rL[rt], mxr[rt]);
mxr[lb] = max(mxr[lb], mxr[rt]);
mxr[rb] = max(mxr[rb], mxr[rt]);
mxr[rt] = ;
} if (mxc[rt]) {
cT[rt] = max(cT[rt], mxc[rt]);
mxc[lb] = max(mxc[lb], mxc[rt]);
mxc[rb] = max(mxc[rb], mxc[rt]);
mxc[rt] = ;
}
} void updateR(int delta, int l, int r, int rt) {
if (L<=l && R>=r) {
mxr[rt] = max(mxr[rt], delta);
return ;
} int mid = (l+r)>>; PushDownR(rt);
if (mid >= R) {
updateR(delta, lson);
} else if (mid < L) {
updateR(delta, rson);
} else {
updateR(delta, lson);
updateR(delta, rson);
}
} void updateC(int delta, int l, int r, int rt) {
if (L<=l && R>=r) {
mxc[rt] = max(mxc[rt], delta);
return ;
} int mid = (l+r)>>; PushDownC(rt);
if (mid >= R) {
updateC(delta, lson);
} else if (mid < L) {
updateC(delta, rson);
} else {
updateC(delta, lson);
updateC(delta, rson);
}
} pii query(int l, int r, int rt) {
if (l == r) {
rL[rt] = max(rL[rt], mxr[rt]);
cT[rt] = max(cT[rt], mxc[rt]);
mxr[rt] = ;
mxc[rt] = ; pii p(rL[rt], cT[rt]);
return p;
}
int mid = (l+r)>>; PushDown(rt);
if (mid >= R) {
return query(lson);
} else {
return query(rson);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, q;
mpii tb;
sti st; scanf("%d %d", &n, &q);
rep(i, , q) {
scanf("%d %d %s", &Q[i].y, &Q[i].x, Q[i].op);
st.insert(Q[i].x);
st.insert(Q[i].y);
}
st.insert(); int m = ;
int i, j, k; for (sti::iterator siter=st.begin(); siter!=st.end(); ++siter) {
k = *siter;
++m;
rn[m] = k;
tb[k] = m;
} int x, y, idx, idy;
int ans;
pii p; build(, m, );
rep(i, , q) {
x = Q[i].x;
y = Q[i].y;
if (Q[i].op[] == 'U') {
idy = tb[y];
L = R = idy;
p = query(, m, );
updateC(x+, , m, );
ans = x - p.sec + ;
idx = tb[x];
k = lower_bound(rn+, rn++m, p.sec) - rn;
L = k;
R = idx;
if (L <= R)
updateR(y+, , m, );
} else {
idx = tb[x];
L = R = idx;
p = query(, m, );
updateR(y+, , m, );
ans = y - p.fir + ;
idy = tb[y];
k = lower_bound(rn+, rn++m, p.fir) - rn;
L = k;
R = idy;
if (L <= R)
updateC(x+, , m, );
}
#ifndef ONLINE_JUDGE
printf("L = %d, R = %d\n", L, R);
#endif
if (ans < )
ans = ;
printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】310 Div.1 C. Case of Chocolate的更多相关文章

  1. Codeforces Round #310 (Div. 1) C. Case of Chocolate set

    C. Case of Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/ ...

  2. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  3. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  4. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  5. 【CF】207 Div.1 B.Xenia and Hamming

    这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...

  6. 【CF】142 Div.1 B. Planes

    SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...

  7. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

  8. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  9. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

随机推荐

  1. linux mysql 安装(rpm)

    linux上安装mysql, 就需要两个文件, xx.client.xx.rpm和 xx.server.xx.rpm 如 MySQL-client-community-5.1.72-1.rhel5.i ...

  2. virtualbox安装ubuntu出现“The system is running in low-graphics mode”

    cd /etc/X11 sudo mv xorg.conf.failsafe xorg.conf sudo reboot 即可.

  3. OS X环境下SVN回滚工程到指定版本,回滚指定文件到指定版本

    1.打开命令行终端 2.cd + 工程或文件目录 3.svn update 工程目录或文件目录 -r 版本号 在Xcode中选中文件,右键选择''show in finder''(也可以用快捷键,不过 ...

  4. mongodb write 【摘自网上,只为记录,学习】

    mongodb有一个write concern的设置,作用是保障write operation的可靠性.一般是在client driver里设置的,和db.getLastError()方法关系很大 一 ...

  5. bzoj1004:[HNOI2008]Cards

    思路:由于题目给出了置换,又要求本质不同的方案数,考虑使用Burnside引理,Burnside引理即通过所有置换和原来相同的方案数之和除以方案数总数,而对于某一个置换要使置换后得到的与原来的相同,就 ...

  6. Javascript执行环境、作用域链

    执行环境 可以把执行环境想象为一个圆圈,里面包含了一些变量.函数. 执行环境定义了变量或函数的有权访问的其他数据,决定了它们各自的行为.还有一个顶部执行环境.在浏览器中,顶部执行环境既为window, ...

  7. Winform打包发布图解

    最近,机房收费系统的个人版接近尾声,到了打包发布的时刻.VB.NET的打包发布与VB6.0的打包发布存在不小的差别.下面我们来详细看一下如果打包发布. 第一步: 打开VS,新建项目,选择其他项目类型- ...

  8. 尝试一下用MARKDOWN嵌入代码

    public void test(){ // }

  9. 限制apache错误日志大小

    ①配置错误日志 在http.conf配置: ErrorLog "| /opt/lampp/bin/rotatelogs /opt/lampp/logs/%Y_%m_%d_error_log  ...

  10. LeetCode【第一题】Two Sum

    准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...