惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Schneier on Security
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
Latest news
Latest news
S
Schneier on Security
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
Spread Privacy
Spread Privacy
Cisco Talos Blog
Cisco Talos Blog
T
Troy Hunt's Blog
S
Secure Thoughts
C
Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
O
OpenAI News
L
LINUX DO - 最新话题
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园_首页
月光博客
月光博客
博客园 - 【当耐特】
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
D
DataBreaches.Net
美团技术团队
B
Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
有赞技术团队
有赞技术团队
D
Docker
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AI
AI
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
小众软件
小众软件

博客园 - saintqdd

hdu 1102 pku 2421 解题报告 石子合并问题 nkoj1139和乘积最大那题一样. A Tour in Loquat Orchard (FZU 2007 ICPC Qualification Round I tzw) 最大黑区域 滑雪 一道经典题,humble number 今天碰到了一个很诡异的题,Alphacode (zoj 2202) 这两天经常碰到dp题,就写了一个0-1背包 实训以来,到这里的次数少了! 郁闷,乘积最大那题WA原来只是因为我用了pow函数引起的! Smith Number POJ强烈推荐50题 JOJ 2391 words POJ 1014 三十分钟掌握STL STL学习小记 POJ1006,中国剩余定理 POJ1003,简单题
pku 2777 Count Color 解体报告
saintqdd · 2007-11-04 · via 博客园 - saintqdd

这到题的题意很明确,给你L长的离散的带子,初始时都图色1。然后给出O个查询和更新条件。对于这样的题一般用线段树比较好做,而且效率很好。由于颜色比较少,可以用二进制的形式来表示(即所有出入的颜色值都要做这样的处理,把它转化为1左移这么多位所对应的值,即1<<(c-1)),这样在进行颜色合并时就方便多了,直接 A|B。这点搞清楚后,线段树就好写了。
这是我写的代码。

 1 
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 #define MAXN 100001
 6 
 7 struct Seg_Tree{
 8   Seg_Tree *leftptr,*rightptr;
 9   int left,right;
10   int col;
11 }*Root;
12 
13 int L,T,O,cnt;
14 int nmem;
15 Seg_Tree mem[MAXN*10];
16 
17 Seg_Tree* CreateNode(){
18   Seg_Tree* p=&mem[nmem++];
19   memset(p,0,sizeof(Seg_Tree));
20   return p;
21 }
22 
23 Seg_Tree* CreateTree(int s,int e){
24   Seg_Tree* root=CreateNode();
25   root->left=s;
26   root->right=e;
27   if(s!=e){
28     int mid=(s+e)/2;
29     root->leftptr=CreateTree(s,mid);
30     root->rightptr=CreateTree(mid+1,e);
31   }
32   return root;
33 }
34 
35 bool odd(int n){//判断奇偶的,其实就判断是不是只有一种颜色。
36   return (n&(n-1))==0;
37 }
38 
39 void UpdateTree(Seg_Tree* root,int s,int e,int c){
40     if(s<=root->left&&e>=root->right){
41       root->col=c;
42       return;
43     }
44     if(root->col==c) return;
45     if(odd(root->col)){
46       root->leftptr->col=root->col;
47       root->rightptr->col=root->col;
48     }
49     int mid=(root->left+root->right)/2;
50     if(s<=mid) UpdateTree(root->leftptr,s,e,c);
51     if(e>mid) UpdateTree(root->rightptr,s,e,c);
52     root->col=root->leftptr->col|root->rightptr->col;
53 }
54 void Query(Seg_Tree* root,int s,int e,int &cnt){
55     if(s<=root->left&&e>=root->right){
56         cnt=cnt|root->col;
57         return;
58     }
59     if(odd(root->col)){
60         cnt=cnt|root->col;
61         return;
62     }
63   int mid=(root->left+root->right)/2;
64   if(s<=mid) Query(root->leftptr,s,e,cnt);
65   if(e>mid) Query(root->rightptr,s,e,cnt);
66 }
67 int cal(int n){
68   int cnt=0;
69   while(n>0){
70     if(n%2) cnt++;
71     n>>=1;
72   }
73   return cnt;
74 }
75 int main(){
76   nmem=0;
77   scanf("%d%d%d",&L,&T,&O);
78   Root=CreateTree(1,L);
79   UpdateTree(Root,1,L,1);
80   char cmd;
81   int s,e,c;
82   while(O--){
83     scanf(" %c",&cmd);
84     if(cmd=='C'){
85       scanf("%d%d%d",&s,&e,&c);
86       if(s>e) swap(s,e);
87       UpdateTree(Root,s,e,1<<(c-1));
88     }
89     else{
90       cnt=0;
91       scanf("%d%d",&s,&e);
92       if(s>e) swap(s,e);
93       Query(Root,s,e,cnt);
94       printf("%d\n",cal(cnt));
95     }
96   }
97   return 0;
98 }
99