nicholas

nicholas

公告

统计

2008年11月19日

SRM 424 div1 900 ProductOfPrices 翻译

为了解决这样的问题,我们需要关于一个整型数组A[1..N]的数据结构,它支持这样的操作,起初,数组中的元素都是0,而且这样的数据结构支持下面两种操作:
1.更新A[pos] = A[pos] + value (其中value,pos是任意给定的,且可能为负值)
2.计算sum(A, l, r) = A[l]+A[l+1]+A[l+2]+...+A[r-1]+A[r] (其中l, r是任意给定的)

我们需要这样的操作都在O(log N)的时间内完成。能够实现这样操作的数据结构是 Binary Idexed Trees (BIT)。 

在我们的解决方案中我们需要两个树状数组(BIT), 下标从1..L。其中第一个叫做cnt, cnt[i] 是当前种在i这个位置中树的个数(我们增加将所有树的下标增加1,使得所有的下标在1..L内)。第二个数组叫做dst, dst[i] = cnt[i]*i。换句话说,dst[i]是所有种在位置i的树的距离和。

假设我们要在x[i]处种一棵树, 并且想要尽快计算出他的price。我们有CL=sum(cnt, 1, x[i]-1) 棵树在x[i]的左边。每个种在x0处的树距离第i棵树的距离是x[i]-x0。所以左边所有树到第i棵树的距离和就是 CL*x[i]-sum(all x0) = CL*x[i]-sum(dst, 1, x[i]-1)。同样的有CR=sum(cnt, x[i]+1, L)棵树在第i棵树的右边,他们到第i棵树的距离和是 sum(dst, x[i]+1, L)-CR*x[i]。所以总的price 可以由下式计算:

 price = x[i] * (sum(cnt, 1, x[i]-1- sum(cnt, x[i]+1, L)) +
        sum(dst, x[i]
+1, L) - sum(dst, 1, x[i]-1

计算好price后我们只需要更新cnt, dst数组, cnt[x[i]]=cnt[x[i]]+1, dst[x[i]]=dst[x[i]]+x[i]



class ProductOfPrices {
public:
    
int product(intintintintint);
};
int L;
int64 cnt[
200001], dst[200001];
int64 x[
200001];
void add(int64 a[], int64 i, int64 x)
{
    
while (i <= L)
    {
        a[i] 
+= x;
        i 
|= i-1;
        i
++;
    }
}
int64 getSum(int64 a[], int64 i)
{
    int64 res 
= 0;
    
while (i > 0)
    {
        res 
+= a[i];
        i 
&=i-1;
    }
    
return res;
}
int64 getSum(int64 a[], int64 i, int64 j)
{
    
if (j >= i)
        
return getSum(a, j) - getSum(a, i-1);
    
else
        
return 0;
}
int ProductOfPrices::product(int N, int l, int X0, int A, int B) 
{
    L 
= l;
    x[
0= X0%L;
    
for (int i = 1; i < N; i++)
        x[i] 
= ((int64(x[i-1])*int64(A))+int64(B)) % int64(L);
    memset(cnt, 
0sizeof(cnt));
    memset(dst, 
0sizeof(dst));
    add(cnt, x[
0]+11);
    add(dst, x[
0]+1, x[0]+1);
    int64 res 
= 1;
    
for (int i = 1; i < N; i++)
    {
        int64 price 
= (x[i]+1* getSum(cnt, 1, x[i]) - getSum(dst, 1, x[i])
                       
+ getSum(dst, x[i]+1, L) - getSum(cnt, x[i]+1, L) * (x[i]+1);
        price 
%= 1000000007;
        res 
= (res * price) % 1000000007;
        add(cnt, x[i]
+11);
        add(dst, x[i]
+1, x[i]+1);
    }

    
return (int)res;

posted @ 2008-11-19 16:00 zy_nic 阅读(100) 评论(0) 编辑

2008年11月12日

..

Code

posted @ 2008-11-12 20:07 zy_nic 阅读(28) 评论(0) 编辑

2008年10月22日

emacs的c++mode

C/C++ 模式
指定为C++模式的方法:一般根据扩展名自动设定,不用指定,不过有时候你希望.h文件是C++模式的(缺省是C模式),
在文件第一行(或其末尾)上加入 
// -- C++ -- 
语法高亮:不是C模式专有,顺便提一下,M-x global-font-lock-mode RET 或在.emacs中加入(global-font-lock-mode t)。
在console下,Emacs21才支持语法高亮。(语法高亮,不同关键字用不同的颜色来突出) 
子模式: 
  • auto-state 当你输入时自动缩进,自动换行 
  • hungry-state 当你Backspace时,自动删除尽可能多的空白和空行 
  • C-c C-t 同时转换(开/关)auto-state和hungry-state子模式 
  • C-c C-a 转换 auto-state 子模式 
  • C-c C-d 转换 hungry-state 子模式 
  • C-c . 设置缩进风格(按TAB键可列出可用的风格,缺省的为gnu,其缩进为2个字符;linux为8个;k&r为5个...) 
  • TAB 重新缩进当前行 
  • M-/ 自动补齐(缓冲区中能找得到的串) 
  • M-; 行尾加入注释 
  • C-c C-e 扩展宏 
  • C-c C-c 注释掉整个区域 
  • C-c C-" 将区域中的每一行结尾都加入一个'"'字符 
编译和调试
  • -x compile RET 编译 
  • M-x gdb RET 调试 
  • C-x ` (出错信息中)下一个错误,一个窗口显示错误信息,另一个显示源码的出错位置 
  • C-c C-c 转到出错位置 
启动gdb调试器后,光标在源码文件缓冲区中时: 
  • C-x SPC 在当前行设置断点 
  • C-x C-a C-s step 
  • C-x C-a C-n next 
  • C-x C-a C-t tbreak 
  • C-x C-a C-r continue 

posted @ 2008-10-22 16:50 zy_nic 阅读(363) 评论(0) 编辑
contest


ural

Schedule

25 October 2008. Autumn school contest 2008

1 November 2008. NEERC 2008, Eastern subregion quarterfinals

Also look at the contests page at Valladolid University.


sgu

26 2008-10-26 11:00:00 2008-10-26 16:00:00 5:00:00 Saratov School Regional Contest 2008


 

 

 


 

posted @ 2008-10-22 11:38 zy_nic 阅读(42) 评论(0) 编辑
contest


ural

Schedule

25 October 2008. Autumn school contest 2008

1 November 2008. NEERC 2008, Eastern subregion quarterfinals

Also look at the contests page at Valladolid University.


sgu

26 2008-10-26 11:00:00 2008-10-26 16:00:00 5:00:00 Saratov School Regional Contest 2008


 

 

 


 

posted @ 2008-10-22 09:05 zy_nic 阅读(28) 评论(0) 编辑

2008年9月18日

我的2-sat模板

Code

posted @ 2008-09-18 00:11 zy_nic 阅读(231) 评论(0) 编辑

2008年9月15日

Solution to GCJ Practice Contest Problem C, Cycles

GCJ Practice Contest Problem C:

 

In this problem, u r asked to calculate the number of all the Hamiltonian Circles in complete graph only with at most 15 forbidden edges.

We know, finding the Hamiltonian Circles is a NP problem, how can we count all the Hamiltonian Circles in a graph with so many edges?

First, let us think about this problem. Give u a complete graph with N vertexes , can u tell me how many Hamiltonian Circles exist in it?

We can easily find that number will be (N-1)! / 2.

And back to the original problem, we find that the forbidden edges are very small. So we come up with an idea, we can use the number of Hamiltonian Circles in a complete graph minus those circles using forbidden edges.

 

Code

posted @ 2008-09-15 22:31 zy_nic 阅读(73) 评论(0) 编辑

2008年9月11日

约瑟夫问题的数学方法

posted @ 2008-09-11 22:44 zy_nic 阅读(869) 评论(0) 编辑

2008年5月17日

我的模板

posted @ 2008-05-17 16:32 zy_nic 阅读(68) 评论(0) 编辑

2007年10月21日

图的应用

posted @ 2007-10-21 20:06 zy_nic 阅读(88) 评论(0) 编辑