Codeforces 755D(思维+线段树)

  http://codeforces.com/problemset/problem/755/D

从X到X+k点,其实只要求从X到X+k之间的点有多少条线超过X——X+K这条线就行,一开始直接暴力,就时间超时了,而用线段树维护就快多了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 #define N 1000010
 5 #define INF 0x3f3f3f3f
 6 #define lson rt<<1, l, m
 7 #define rson rt<<1|1, m+1, r
 8 LL tree[N<<2];
 9
10 void pushup(int rt) { tree[rt] = tree[rt<<1] + tree[rt<<1|1]; }
11
12 void update(int rt, int l, int r, int id) {
13     if(l == r && l == id) {
14         tree[rt]++;
15         return ;
16     }
17     int m = (l + r) >> 1;
18     if(id <= m) update(lson, id);
19     else update(rson, id);
20     pushup(rt);
21 }
22
23 LL query(int rt, int l, int r, int L, int R) {
24     LL ans = 0;
25     if(L <= l && r <= R) return tree[rt];
26     int m = (l + r) >> 1;
27     if(L <= m) ans += query(lson, L, R);
28     if(m < R) ans += query(rson, L, R);
29     return ans;
30 }
31
32 LL getsum(int l, int r, int n) {
33     LL ans = 0;
34     if(l > r) {
35         ans += query(1, 1, n, l, n);
36         ans += query(1, 1, n, 1, r);
37     } else {
38         ans += query(1, 1, n, l, r);
39     }
40     return ans;
41 }
42
43 int main()
44 {
45     int n, k;
46     cin >> n >> k;
47     int now = 1 + k, pre;
48     if(k > n / 2) k = n - k;
49     LL res = 1;
50     for(int i = 1; i <= n; i++) {
51         pre = now; update(1, 1, n, pre);
52         now += k; if(now > n) now -= n;
53         res += getsum(pre, now, n) - 1;
54         printf("%lld ",(i==n) ? res : res+1);
55         update(1, 1, n, now);
56     }
57     printf("\n");
58     return 0;
59 }
时间: 2024-08-23 14:56:11

Codeforces 755D(思维+线段树)的相关文章

CodeForces 46DParking Lot线段树

#include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <qu

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

Codeforces 444C(线段树)

区间颜色不一致就更新到底,否则lazy标记 #include<cstdio> #include<cstring> #include<cmath> #include<queue> using namespace std; #define lc l,m,index<<1 #define rc m+1,r,index<<1|1 #define N 100005 #define ll __int64 struct node { bool sa

Buy Tickets POJ - 2828 思维+线段树

Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最后输出编号就好了. 解题思路 这个题要倒着处理,因为最后一个人插队完成后,别人就不能影响他了.他前面有n个人,那么他就是n+1号位置上,这样来的话,我们只需要知道那个位置,他前面有n个人就行.默认每个位置都没有人. 详细看代码. 代码实现 #include<cstdio> #include<

New Year and Old Subsequence CodeForces - 750E(线段树 + 矩阵)

New Year and Old Subsequence (CodeForces - 750E) 题意: 给出一个长度为\(N\)的数字串,\(q\)次询问.每次询问一段区间.在区间内删除尽量少的字符,使得区间内含有序列"\(2017\)",且不含有"\(2016\)". \(n,q<=200000\). 题解: 用\(01234\)五种状态分别表示"". "\(2\)"."\(20\)"."

Sum Queries? CodeForces - 1217E (线段树)

Sum Queries? CodeForces - 1217E (线段树) 题意: 定义一个集合为\(balanced\)的,当且仅当集合内数字之和的每个十进制位,都与集合中某个数该位相同.否则,称该集合为\(unbalanced\)的. 给定一个长度为\(n\)的序列,\(q\)次询问一个区间内数字之和最小的\(unbalanced\)集合,输出数字之和.若没有输出\(-1\). \(n,q<=200000\). 题解: 可以发现,如果存在\(unbalanced\)集合,那么最小的一定是只两

51nod 1272 思维/线段树

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等于该元素的数,则这两个数可以组成一对.每个元素和自己也可以组成一对.例如:{5, 3, 6, 3, 4, 2},可以组成11对,如下(数字为下标):

CodeForces 383C-dfs序-线段树

题意:一棵根为1的多叉树有n个点,题目有m次询问.第一行输入n和m,第二行输入n-1条边, 以后m行输入操作,操作有两种:1 x val 表示 节点的值x+val,同时它的儿子层节点的值-val,孙子层节点的值+val...如此往下直到叶子节点:2 x 表示输出x节点的当前值. 思路:类似poj3321,用dfs序重新表示每个节点,这样更新子树的操作就变成更新区间了,区间是:[i, i+cnt][当前节点的dfs序为 i, 儿子数为cnt],查询同理,单点查询当前节点的dfs序.但是这道题的df

【题解】Berland.Taxi Codeforces 883L 模拟 线段树 堆

Prelude 题目传送门:ヾ(?ω?`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\(i\)个房子前面有没有停放出租车,这样在有人需要打车的时候可以快速找到离她最近的车的位置. 对每个房子维护一个堆,里面装的是停在这个房子前面的出租车,关键字是出租车的编号和上一个乘客下车的时间,上一个乘客下车越早,等待时间越长. 然后模拟时间的流逝就可以了,代码非常好写. Code #includ