数三角形 bzoj 3505

要知道一个公式就是(a,b)和(x,y)两点所成线段上面的整点数是gcd(a-x,b-y)-1,通过枚举原点到map上任意一点所能成的三角形,再平移,得到要去掉的三点共线的点对。

我当时弱智地弄了个O(n^6)的枚举,不过好歹还是对的拿了三十分。

= =满分程序和30分程序几乎一样长。

program triangle;
var m,n,i,j:integer;
ans,t:qword; function gcd(a,b:integer):integer;
begin
if a= then exit(b);
if b= then exit(a);
if a mod b= then exit(b) else exit(gcd(b,a mod b));
end; begin{main}
readln(m,n);
t:=(m+)*(n+);
ans:=t*(t-)*(t-) div ;
for i:= to m do
for j:= to n do
if not ((i=) and (j=)) then
begin
if (i=) or (j=) then ans:=ans-(gcd(i,j)-)*(m-i+)*(n-j+)
else ans:=ans-*(gcd(i,j)-)*(m-i+)*(n-j+);
end;
writeln(ans);
end.

Triangle

机械排序臂 bzoj 3506

那天早上还是写的O(n^2)骗的30分,不过这回30和100的代码长度就差远了…
7/2开始写这题,7/2当天晚上学习&码完splay,是对的,但是之后的lazy tag,没什么资料,C++的程序又看不太懂,我调了这么多天简直崩溃了好吗!最后发现是左子树注入tag的时候不应该直接赋值true而应该赋反,这一点在switch里面想到了,但是在主程序里没想到。对着sort1.in这个n=1000的输入文件我愣了好久啊,为什么输出的前80%是对的但是之后就有的对有的不对了呢。一开始我以为是树的fa啊,lc、rc啊没消除干净,导致被删的结点又重新回来,后来发现这也不是回事…

不过还好找到了错误所在,现在整个人心情爆好!!

200行的程序,况且这还不是完全的splay…弱渣的我到时候要好好背一下…

program sort3;
var n,i,root,c,x,t,temp:longint;
f,ff,loc,l,fa,lc,rc,da,tot:array[..] of longint;
lazy:array[..] of boolean; function dd(x,xloc,y,yloc:longint):boolean;
begin
if x<y then exit(true);
if (x=y) and (xloc<yloc) then exit(true);
exit(false);
end; function gg(x,xloc,y,yloc:longint):boolean;
begin
if x>y then exit(true);
if (x=y) and (xloc>yloc) then exit(true);
exit(false);
end; procedure qsort(l,r:longint);
var mid,mid_loc,i,j,temp:longint;
begin
i:=l;j:=r;mid:=f[(l+r) div ];mid_loc:=loc[(l+r) div ];
while i<=j do
begin
while dd(f[i],loc[i],mid,mid_loc) do inc(i);
while gg(f[j],loc[j],mid,mid_loc) do dec(j);
if i<=j then
begin
temp:=f[i];f[i]:=f[j];f[j]:=temp;
temp:=loc[i];loc[i]:=loc[j];loc[j]:=temp;
inc(i);dec(j);
end;
end;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end; procedure lrot(p:longint);
var q:longint;
begin
q:=fa[p];fa[p]:=fa[q];
if q=root then root:=p else
if q=lc[fa[q]] then lc[fa[q]]:=p else rc[fa[q]]:=p;
lc[q]:=rc[p];
if rc[p]<> then fa[rc[p]]:=q;
rc[p]:=q;
fa[q]:=p;
tot[q]:=tot[lc[q]]+tot[rc[q]]+;
tot[p]:=tot[lc[p]]+tot[rc[p]]+;
end; procedure rrot(p:longint);
var q:longint;
begin
q:=fa[p];fa[p]:=fa[q];
if q=root then root:=p else
if lc[fa[q]]=q then lc[fa[q]]:=p else
rc[fa[q]]:=p;
rc[q]:=lc[p];
if lc[p]<> then fa[lc[p]]:=q;
lc[p]:=q;
fa[q]:=p;
tot[q]:=tot[lc[q]]+tot[rc[q]]+;
tot[p]:=tot[lc[p]]+tot[rc[p]]+;
end; procedure switch(p:longint);
var t:longint;
begin
if lazy[p]=false then exit;
t:=lc[p];lc[p]:=rc[p];rc[p]:=t;
lazy[p]:=false;
lazy[lc[p]]:=not lazy[lc[p]];
lazy[rc[p]]:=not lazy[rc[p]];
end; procedure clear(p:longint);
begin
if p=root then
begin
if lazy[p] then switch(p);
exit;
end;
clear(fa[p]);
if lazy[p] then switch(p);
end; procedure splay(p:longint);
var t,tt:longint;
begin
while p<>root do
begin
if p=lc[fa[p]] then //p is lc
begin
if fa[p]=root then lrot(p) else
if fa[p]=lc[fa[fa[p]]] then
begin
lrot(fa[p]);lrot(p);
end
else
begin
lrot(p);rrot(p);
end;
end
else
begin
if fa[p]=root then rrot(p) else
if fa[p]=rc[fa[fa[p]]] then
begin
rrot(fa[p]);rrot(p);
end
else
begin
rrot(p);lrot(p);
end;
end;
end;
fa[p]:=;
end; procedure insert(x:longint);
var t,tt:longint;
begin
inc(c);
fa[c]:=;lc[c]:=;rc[c]:=;da[c]:=f[x];
if root= then
begin
root:=c;
tot[root]:=;
end
else
begin
t:=root;
repeat
begin
tt:=t;
if loc[x]<loc[t] then
begin
inc(tot[t]);
t:=lc[t];
end
else
begin
inc(tot[t]);
t:=rc[t];
end;
end;
until t=;
tot[x]:=;
if loc[x]<loc[tt] then lc[tt]:=c else rc[tt]:=c;
fa[c]:=tt;
splay(c);
end;
end; begin
//assign(input,'sort10.in');reset(input);
//assign(output,'sort10.out');rewrite(output);
fillchar(lazy,sizeof(lazy),false);
readln(n);
for i:= to n do
begin
read(f[i]);
loc[i]:=i;
end;
ff:=f;
qsort(,n);
for i:= to n do
begin
f[i]:=i;
end;
for i:= to n do
insert(i); //buildtree
for i:= to n do
begin
clear(i);
splay(i);
write(tot[lc[i]]+i,' ');
lazy[lc[i]]:=not lazy[lc[i]]; //oh no I wrote':=true' and debuged for days..
t:=lc[i];
if lazy[t] then switch(t);
while rc[t]<> do
begin
t:=rc[t];
if lazy[t] then switch(t);
end;
fa[rc[i]]:=;fa[lc[i]]:=;root:=lc[i];temp:=rc[i];tot[i]:=;
if lc[i]= then root:=rc[i];
rc[i]:=;lc[i]:=;fa[i]:=;
if t<> then
begin
clear(t);//from down to top then top to down
splay(t);
fa[temp]:=t;rc[t]:=temp;tot[t]:=tot[t]+tot[rc[t]];
end
end;
//close(input);close(output);
end.

Sort

[CQOI 2014] 数三角形 & 机械排序臂的更多相关文章

  1. [ CQOI 2014 ] 数三角形

    \(\\\) Description 求 \(N\times M\) 的网格图上有多少个格点构成的三角形. 当三点共线的时候我们不认为这是一个三角形. \(n,m\le 10^4\) \(\\\) S ...

  2. BZOJ 3505 CQOI 2014 数三角形 数学

    标题效果:到m*n该网络格,问:有网络格是一个三角形的顶点的数目. 思维:数学.首先计算所有三个相同的,不.然后,在上线的一个点失去了三个点是其中.需要注意的是,答案开放long long. CODE ...

  3. bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形——极角排序

    Description 在一只大灰狼偷偷潜入Farmer Don的牛群被群牛发现后,贝西现在不得不履行着她站岗的职责.从她的守卫塔向下瞭望简直就是一件烦透了的事情.她决定做一些开发智力的小练习,防止她 ...

  4. 【BZOJ1914】数三角形(组合数,极角排序)

    [BZOJ1914]数三角形(组合数,极角排序) 题面 BZOJ权限题 良心洛谷 题解 这种姿势很吼啊,表示计算几何啥的一窍不通来着. 题目就是这样,正难则反,所以我们不考虑过原点的三角形, 反过来, ...

  5. 数三角形 bzoj 1201

    数三角形(1s 128MB)triangle [题目描述] 小苏看到一个这样的等边三角形:该等边三角形每边的长度为n且被分成n等份,于是每条边就有n-1个等分点.而整个三角形被连接两个不同边的等分点且 ...

  6. [Usaco2010 OPen]Triangle Counting 数三角形

    [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 394  Solved: 1 ...

  7. bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形 计算机和

    [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 526  Solved: 2 ...

  8. 【bzoj3505】[Cqoi2014]数三角形

    [bzoj3505][Cqoi2014]数三角形 2014年5月15日3,5230 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4×4的网格上的一个三角 ...

  9. BZOJ 3505: [Cqoi2014]数三角形 数学

    3505: [Cqoi2014]数三角形 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

随机推荐

  1. iOS开发Hessian

    HessianKit使用参考资料比较少,通过摸索,把测试过程贴出来,代码很乱,未整理,先实现功能,再应用到项目中.供新手参考.如有问题,跟帖指正... HessianService与Java Serv ...

  2. wex5 教程 之 图文讲解 可观察对象的集群应用与绑定技术

    一 前言: wex5官方教程里,开篇即以一个input输入,output即时输出的例子,为我们展现了一个概念:可观察对象.在以后我的项目开发中,将大量运用可观察对象. 那么,问题来了: 1. 可观察对 ...

  3. NaN

    not a number 全称, 任何数/0 js会出现NaN alert(NaN==NaN); // false isNaN(NaN); // true alert(isNaN(10)); // f ...

  4. T-SQL 的简单查询语句

    通配符: “_”: 代表匹配一个字符 “%”: 代表匹配多个字符: []:表示范围,可以包含多个数据 [^] 表示取反 “-“  表示范围 逻辑与 and 逻辑或 or  逻辑非 not 聚会函数 : ...

  5. android 蓝牙串口通讯使用简介

    需要的权限 <uses-permission android:name="android.permission.BLUETOOTH" />  <uses-perm ...

  6. 《R语言实战》读书笔记-- 第六章 基本图形

    首先写第二部分的前言. 第二部分用来介绍获取数据基本信息的图形技术和统计方法. 本章主要内容 条形图.箱型图.点图 饼图和扇形图 直方图和核密度图 分析数据第一步就是要观察它,用可视化的方式是最好的. ...

  7. poj 1091 跳蚤

    跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8482   Accepted: 2514 Description Z城 ...

  8. python之rabbitMQ篇

    一.RabbitMQ安装 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统,它遵循Mozilla Pulic License开源协议. MQ全称为Message Queue,消息队列 ...

  9. jQuery验证元素是否为空的两种常用方法

    这篇文章主要介绍了jQuery验证元素是否为空的两种常用方法,实例分析了两种常用的判断为空技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了jQuery验证元素是否为空的两种常用方法.分享给 ...

  10. 实操canvas

    我觉得仅仅靠看书是不会学好canvas的,经过前几天对canvas画布属性的阅读和了解,然后我觉得还是要实际操作才会学的更好. <canvas width="" height ...