forked from derekhh/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candles-2.cpp
72 lines (62 loc) · 1.03 KB
/
candles-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<cstring>
using namespace std;
int h[50000], c[50000];
const int MAXN = 50010;
const int MOD = 1000000007;
class FenwickTree
{
private:
long long tree[MAXN];
public:
FenwickTree()
{
memset(tree, 0, sizeof(tree));
}
long long get(int idx)
{
return query(idx) - query(idx - 1);
}
void set(int idx, long long val)
{
long long curr = get(idx);
update(idx, val - curr);
}
void update(int idx, long long val)
{
while (idx <= MAXN)
{
tree[idx] = (tree[idx] + val) % MOD;
idx += (idx & -idx);
}
}
long long query(int idx)
{
long long sum = 0;
while (idx > 0)
{
sum = (sum + tree[idx]) % MOD;
idx -= (idx & -idx);
}
return sum;
}
};
FenwickTree s[128];
int main()
{
int n, k;
cin >> n >> k;
s[0].update(1, 1);
for (int i = 0; i < n; i++)
{
cin >> h[i] >> c[i];
h[i]++; c[i]--;
for (int j = 0; j < (1 << k); j++)
{
long long val = s[j].query(h[i] - 1);
s[j | (1 << c[i])].update(h[i], val);
}
}
cout << s[(1 << k) - 1].query(50001) << endl;
return 0;
}