Codeforces 19D Points(树状数组)

题目链接:Codeforces 19D Points

题目大意:N中操作,每次添加一个点,或者删除一个点,以及找到给定x,y坐标最近的一个坐标,并且保证xi,yi在x,y的右上角。

解题思路:这题的解法还是很机智的。

y坐标离散化,然后树状数组的每个单位用一个set代替,set记录的是点集。

剩下的操作就像树状数组一样,每次添加就等于是+w的操作,移除就等于是-w,只是w是一个点,那么find操作就等于是在sum操作生成的点集中二分查找。

#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 2 * 1e5 + 5;
const int INF = 0x3f3f3f3f;
#define lowbit(x) ((x)&(-x))
typedef pair<int,int> pii;
typedef set<pii>::iterator iter;

int N, M, pos[maxn];
set<pii> fenw[maxn];

struct Camd {
    int x, y;
    char op[10];
    void read() { scanf("%s%d%d", op, &x, &y); }
    void add() {
        for (int i = maxn - y; i < maxn; i += lowbit(i))
            fenw[i].insert(make_pair(x, y));
    }
    void del() {
        for (int i = maxn - y; i < maxn; i += lowbit(i))
            fenw[i].erase(make_pair(x, y));
    }
    void find() {
        pii ans(INF, INF);
        for (int i = maxn-y-1; i; i -= lowbit(i)) {
            iter it = fenw[i].lower_bound(make_pair(x+1, y));
            if (it != fenw[i].end())
                ans = min(ans, *it);
        }
        if (ans == make_pair(INF,INF))
            printf("-1\n");
        else
            printf("%d %d\n", ans.first, pos[ans.second]);
    }
    void solve() {
        if (op[0] == ‘a‘) add();
        else if (op[0] == ‘r‘) del();
        else find();
    }
}p[maxn];

void init () {
    M = 0;
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) {
        p[i].read();
        pos[i] = p[i].y;
    }
    sort(pos + 1, pos + 1 + N);
    M = unique(pos+1, pos+1+N) - (pos+1);

    for (int i = 1; i <= N; i++)
        p[i].y = lower_bound(pos+1, pos+1+M, p[i].y) - pos;
}

int main () {
    init();

    for (int i = 1; i <= N; i++)
        p[i].solve();
    return 0;
}
时间: 2024-08-06 11:56:28

Codeforces 19D Points(树状数组)的相关文章

Codeforces 12D Ball 树状数组模拟3个元素的排序

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

Codeforces 597C. Subsequences (树状数组+dp)

题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长度为j的上升子序列个数,但是dp数组是在树状数组的update函数中进行更新. update(i, val, j)函数表示在i的位置加上val,更新dp[i][j]. sum(i, j)就是求出末尾数字小于等于i 且长度为j的子序列有多少个. 1 //#pragma comment(linker,

CodeForces 301D(树状数组)

题目链接:codeforces 301D 题意分析: 给你n , m两个数,1?≤?n,?m?≤?2e5,n代表n个不同数字,且这些数字都在区间[ 1 , n ]之间,这就说明1~n每个数出现一次.m代表m次查询,查询格式为两个整数x , y,问你区间[ x , y ]之间有多少对数a , b满足a%b==0. 解题思路: 考察点是区间的频繁访问,马上想到线段树和树状数组,线段树太难写了没考虑过,就说说树状数组的思路吧. 1)离线处理:把所有的插叙全部读进来再按特定顺序处理.为了让树状数组求的和

CodeForces 371D Vessels(树状数组)

树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #i

Garlands CodeForces - 707E (离线树状数组)

大意: 给定n*m矩阵, k条链, 链上每个点有权值, 每次操作可以关闭或打开一条链或询问一个子矩阵内未关闭的权值和. 关键询问操作比较少, 可以枚举每条链, 暴力算出该条链对每个询问的贡献. 最后再按询问顺序切换链的状态, 只记录打开的链的贡献即可. #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #inclu

UVA 10869 - Brownie Points II(树状数组)

UVA 10869 - Brownie Points II 题目链接 题意:平面上n个点,两个人,第一个人先选一条经过点的垂直x轴的线,然后另一个人在这条线上穿过的点选一点作垂直该直线的线,然后划分出4个象限,第一个人得到分数为1,3象限,第二个人为二四象限,问第一个个人按最优取法,能得到最小分数的最大值,和这个值下另一个人的得分可能情况 思路:树状数组,可以枚举一点,如果能求出右上和左下点的个数就好办了,其实用一个树状数组,把y坐标离散化掉,然后记录进来,然后把点按x从左往右,每次删掉点后查询

CodeForces 380C Sereja and Brackets(扫描线+树状数组)

[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括号,其匹配的左括号是固定的, 我们保存每个右括号匹配的左括号位置, 对区间询问进行线扫描,将扫描的区间右端点及其之前所有的右括号对应的左括号位置做标记, 只要查询询问区间的标记个数就是答案,这个可以用树状数组维护. [代码] #include <cstdio> #include <algor

Codeforces Round #277 E. LIS of Sequence(486E) 树状数组乱搞

http://codeforces.com/contest/486/problem/E E. LIS of Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The next "Data Structures and Algorithms" lesson will be about Longest I

Codeforces 19D Points 线段树+set

题目链接:点击打开链接 线段树维护y值大于val的最小x值 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf