【离散化树状数组】Nordic Collegiate Programming Contest G.Galactic Collegiate Programming Contest

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int n,m;
 5 const int maxn=1e5+3;
 6 struct node
 7 {
 8     int team;
 9     int num;
10     int time;
11     int id;
12 }a[maxn];
13 int ans[maxn];
14 int b[maxn];
15 int c[maxn];
16 int s[maxn];
17 int team[maxn];
18 int tree[maxn];
19 int pre[maxn];
20 int lowbit(int x)
21 {
22     return x&(-x);
23 }
24 void add(int k,int x)
25 {
26     while(k<=m)
27     {
28         tree[k]+=x;
29         k+=lowbit(k);    
30     }
31 }
32 int query(int k)
33 {
34     int res=0;
35     while(k)
36     {
37         res+=tree[k];
38         k-=lowbit(k);
39     }
40     return res;
41 }
42 
43 bool cmp(node x,node y)
44 {
45     if(x.num!=y.num) return x.num>y.num;
46     else if(x.time!=y.time) return x.time<y.time;
47     else if(x.team!=1) return false;
48     //else return true;
49 }
50 int main()
51 {
52     while(~scanf("%d%d",&n,&m))
53     {
54         memset(b,0,sizeof(b));//题数 
55         memset(c,0,sizeof(c));//罚时数 
56         memset(tree,0,sizeof(tree));
57         memset(ans,0,sizeof(ans));
58         memset(pre,0,sizeof(pre));
59         int tmp;
60         for(int i=1;i<=m;i++)
61         {
62             scanf("%d%d",&a[i].team,&tmp);
63             a[i].num=b[a[i].team]+1;
64             b[a[i].team]=a[i].num;
65             a[i].time=c[a[i].team]+tmp;
66             c[a[i].team]=a[i].time;
67             a[i].id=i;
68             team[i]=a[i].team;
69         }
70         sort(a+1,a+1+m,cmp);
71         for(int i=1;i<=m;i++)
72         {
73             s[a[i].id]=i;
74         }
75         for(int i=1;i<=m;i++) pre[team[i]]=-1;
76         int r=m;
77         for(int i=1;i<=m;i++)
78         {
79             if(pre[team[i]]!=-1)
80                 add(pre[team[i]],-1);
81             pre[team[i]]=s[i];
82             if(team[i]==1)
83                 r=s[i]-1;
84             add(s[i],1);
85             ans[i]=query(r)+1;
86         }    
87         for(int i=1;i<=m;i++) cout<<ans[i]<<endl;
88             
89     }
90     return 0;
91 }
View Code

注意自定义cmp,最后没有return 导致wa

http://codeforces.com/gym/101572/attachments/download/6091/20172018-acmicpc-nordic-collegiate-programming-contest-ncpc-2017-en.pdf

posted @ 2017-11-02 13:29  shulin15  阅读(253)  评论(0编辑  收藏  举报