【树状数组】Codeforces Round #755 D. PolandBall and Polygon
http://codeforces.com/problemset/problem/755/D
每次新画一条对角线的时候,考虑其跨越了几条原有的对角线。
可以用树状数组区间修改点查询来维护多边形的顶点。答案每次增加 新对角线的左端点在多少个区间内+右端点在多少个区间内+1,每次把新画的对角线所覆盖的较小区间内部的每个点加1即可。注意,一定要是较小的区间,因为这样才能保证其左右端点不同时在区间内,不会重复统计。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,m;
ll ans=1ll;
int d[1000010];
int Query(int p){int res=0; for(;p;p-=(p&(-p))) res+=d[p]; return res;}
void add(int p,int v){for(;p<=n;p+=(p&(-p))) d[p]+=v;}
void Update(int l,int r)
{
if(l<r)
{
if(l+1==r) return;
add(l+1,1);
if(r<=n) add(r,-1);
}
else
{
if(l<n)
add(l,1);
add(1,1);
add(r,-1);
}
}
int main()
{
// freopen("d.in","r",stdin);
scanf("%d%d",&n,&m);
if(m>n/2)
m=n-m;
int U=1;
for(int i=1;i<=n;++i)
{
int pre=U;
U+=m;
if(U>n)
U-=n;
ans+=((ll)Query(pre));
ans+=((ll)Query(U)+1ll);
Update(pre,U);
printf("%I64d%c",ans,i==n ? '\n' : ' ');
}
return 0;
}
【树状数组】Codeforces Round #755 D. PolandBall and Polygon的更多相关文章
- [离散化+树状数组]CodeForces - 652D Nested Segments
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块
题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
随机推荐
- sshd_conf配置
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ # This is the sshd server system-w ...
- 【洛谷 P2515】 [HAOI2010]软件安装 (缩点+树形背包)
题目链接 看到代价和价值这两个关键词,肯定是首先要想到背包的. 但是图中并没有说这是棵树,所以先要\(Tarjan\)缩点,然后就是选课了,跑一遍树形背包就好了. 注意:缩点后应该是一个森林,应该用一 ...
- NYOJ 115 城市平乱 (最短路)
* 题目链接* 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市分别编号从1到M. 现在,小工军师告诉南将军,第K号城市发生了暴乱,南 ...
- 转: Java安全停止线程方法
转: http://blog.csdn.net/flyingpig4/article/details/7675557 1.早期Java提供java.lang.Thread类型包含了一些列的方法star ...
- SUSE LINUX 11忘记密码的解决方法
忘记ROOT的密码 1.重新启动机器,在出现grub引导界面后,在启动linux的选项里加上init=/bin/bash,通过给内核传递init=/bin/bash参数使得OS在运行login程序之前 ...
- kthread_create与kernel_thread的区别【栈】
转自:http://blog.chinaunix.net/uid-25513153-id-2888903.html kthread_create与kernel_thread的区别 kernel thr ...
- python mysql插入多条数据
#!/usr/bin/env python # encoding: utf-8 import pymysql from config.config import * import datetime d ...
- net core服务器缺包,如何在线安装?
Install -package命令不行. 下面命令也不行.求助大家,怎么安装?
- 【python】抄写大神的百度贴吧代码
原文链接:http://cuiqingcai.com/993.html 划重点: 1.提取帖子内容时,对图片,贴吧自动增加的超链接,制表符,换行符要做删除或替换处理 2.decode是把bytes转换 ...
- tcpdump学习(2):基本使用
安装好tcpdump之后,运行tcpdump: 1. tcpdump -D 获取网络适配器列表,以下是在Ubuntu上获取到的结果: root@holmesian-laptop:~# tcpdump ...