http://codeforces.com/problemset/problem/356/A

首先理解题意

每次给出l 和r  在l - r之间还有资格的选手中得出一个胜者

暴力思路:

首先维护还有资格的选手的集合

用一个数组 表示 这个选手被谁击败

每次遍历 l - r 然后把不是胜者 且 还在集合中的选手踢出 并更新这个选手的数组值

最终 输出这个数组即可

这样会TLE

1、 如果用数组维护这个集合的话 每次遍历都是这样就是O(n^2) -->> 所以用set维护这个集合

2、使用set后 如果每次依然从l - r去遍历找在集合中的元素 去find的话 那么就会在 (l, r)的区间两端有浪费的运算 如果每次l, r 都是1 和 n的话 那就浪费得非常得多

   所以使用set :: lower_bound() 直接得到第一个大于l 并在集合中的元素 O(logn)

这样优化后 即可

 #include <iostream>
#include <stdio.h>
#include <set>
using namespace std; int Par[];
int tmp[];
set<int> s; int find(int x)
{
if (Par[x] == x) return x;
else return find(Par[x]);
} int main()
{
int n, m;
freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
Par[i] = i;
s.insert(i);//加入set中
}
for(int i = ; i < m; i++)
{
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
set<int> :: iterator pit = s.lower_bound(l);//返回第一个>= l的位置
int num = ;
while (pit != s.end() && (*pit) <= r )
{
if ((*pit) != x)
{
Par[*pit] = x;
//s.erase(*pit); 不能在这里直接删除 删除后set结构发生改变 pit就失效了
tmp[num++] = *pit;
}
pit++;
}
for (int j = ; j < num; j++) s.erase(tmp[j]);
}
for (int i = ; i <= n; i++)
{
if (Par[i] == i) printf("0 ");
else printf("%d ", Par[i]);
}
putchar('\n');
}

CodeForces - 356A Knight Tournament的更多相关文章

  1. CodeForce 356A Knight Tournament(set应用)

     Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  2. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  3. codeforces 357C Knight Tournament(set)

    Description Hooray! Berl II, the king of Berland is making a knight tournament. The king has already ...

  4. Knight Tournament 合并区间

    Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...

  5. Knight Tournament (set)

    Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...

  6. D - Knight Tournament(set)

    Problem description Hooray! Berl II, the king of Berland is making a knight tournament. The king has ...

  7. 【Codeforces 356A】Knight Tournament

    [链接] 我是链接,点我呀:) [题意] n个人矩形m场比赛 每场比赛由编号为li~ri且之前没有被淘汰的人进行. 已知第i场的winner是xi winner会把这一场其他所有的人都淘汰. 问你n个 ...

  8. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

  9. Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)

    脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ...

随机推荐

  1. Mysql多表联合更新、删除

    UPDATE snyts_user u INNER JOIN snyts_order o ON u.user_id = o.user_id INNER JOIN snyts_refund_order ...

  2. [转]java注解与APT技术

    下面是一个简单的自定义注解的栗子: package annotation; import java.lang.annotation.Documented; import java.lang.annot ...

  3. arttemplate模板引擎有假数据返回数据多层内嵌的渲染方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. whereis参数

    -b  只找二进制文件 -m 只找在帮助文件manual路径下的文件 -s 只找原文件 -u 没有帮助文件的文件 whereis passwd

  5. 如何处理Docker的错误消息request canceled:Docker代理问题

    在本地安装Kubernetes时,遇到错误消息: request canceled while waiting for connection(Client.Timeout exceeded while ...

  6. SEO 第九章

    SEO第九章 本次课目标: 1.  外部优化之平台优化 2.  如何撰写SEO诊断方案 一.外部平台优化——百度系列平台 百度系列的平台都是属于百度自己的产品,排名都是比较高的,所以我们在做外部推广的 ...

  7. vue-gemini-scrollbar(vue组件-自定义滚动条)

    vue-gemini-scrollbar(vue组件-自定义滚动条) https://segmentfault.com/a/1190000013338560

  8. GC相关的面试题

    问题:Object的finaliz()方法 的作用是否与C++的析构函数作用相同? --->不同的 1.C++的析构函数调用确定,就是对象离开作用域之后就马上被删除.而java Object的f ...

  9. 「 Luogu P2574 」 XOR的艺术——线段树

    # 解题思路 这题不难,但是原谅我一开始的傻逼想法,一会儿再给大家透露透露. 先说怎么做这题. 显然对于 $0$ 和 $1$ 来说,异或无非也就只有两种变化 异或了奇数次,$0$ 就会变成 $1$,$ ...

  10. luogu P4752

    给定一个数字 A ,这个 A 由 a1,a2,...,aN 相乘得到. 给定一个数字 B ,这个 B 由 b1,b2,⋯,bM 相乘得到. 如果 A/B​ 是一个质数,请输出YES,否则输出NO. 输 ...