1650. Billionaires(线段树)

1650

简单题 线段树的单点更新 就是字符串神马的 有点小繁琐 开两个map 一个存城市 一个存名字

  1 #include <iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<algorithm>

  5 #include<cstdlib>

  6 #include<string>

  7 #include<map>

  8 using namespace std;

  9 #define N 60010

 10 #define LL long long

 11 LL s[N<<2];

 12 LL a[N],pp[N];

 13 map<string,int>q;

 14 map<string,int>qq;

 15 char pq[N][25];

 16 struct node

 17 {

 18     int d;

 19     char s1[25];

 20     char s2[25];

 21 }bi[N];

 22 struct mode

 23 {

 24     char ss[25];

 25     LL vv;

 26 }ct[N];

 27 int sd[N],g,gg,lo[N];

 28 void up(int w)

 29 {

 30     s[w] = max(s[w<<1],s[w<<1|1]);

 31 }

 32 void build(int l,int r,int w)

 33 {

 34     if(l==r)

 35     {

 36         s[w] = a[l];

 37         return ;

 38     }

 39     int m = (l+r)>>1;

 40     build(l,m,w<<1);

 41     build(m+1,r,w<<1|1);

 42     up(w);

 43 }

 44 void update(int p,LL d,int l,int r,int w)

 45 {

 46     if(l==r)

 47     {

 48         s[w]+=d;

 49         return ;

 50     }

 51     int m = (l+r)>>1;

 52     if(p<=m)

 53     update(p,d,l,m,w<<1);

 54     else

 55     update(p,d,m+1,r,w<<1|1);

 56     up(w);

 57 }

 58 int query(int l,int r,int w)

 59 {

 60     if(l==r)

 61     {

 62         return l;

 63     }

 64     int m = (l+r)>>1;

 65     if(s[w<<1]>s[w<<1|1])

 66     return query(l,m,w<<1);

 67     else if(s[w<<1]<s[w<<1|1])

 68     return query(m+1,r,w<<1|1);

 69     else

 70     return 0;

 71 }

 72 bool cmp(mode a,mode b)

 73 {

 74     return strcmp(a.ss,b.ss)<0;

 75 }

 76 int main()

 77 {

 78     int n,k,m,i;

 79     char s1[25],s2[25];

 80     LL p;

 81     scanf("%d",&n);

 82     for(i = 1; i <= n ; i++)

 83     {

 84         scanf("%s%s %lld",s1,s2,&p);

 85         if(!q[s2])

 86         {

 87             g++;

 88             q[s2] = g;

 89             a[q[s2]]+=p;

 90             strcpy(pq[g],s2);

 91         }

 92         else

 93         a[q[s2]]+=p;

 94         if(!qq[s1])

 95         {

 96             gg++;

 97             qq[s1] = gg;

 98             pp[gg] = p;

 99         }

100         lo[qq[s1]] = q[s2];

101     }

102     scanf("%d%d",&m,&k);

103     for(i = 1; i <= k ; i++)

104     {

105         scanf("%d%s%s",&bi[i].d,bi[i].s1,bi[i].s2);

106         if(!q[bi[i].s2])

107         {

108             g++;

109             q[bi[i].s2] = g;

110             strcpy(pq[g],bi[i].s2);

111         }

112     }

113     build(1,g,1);

114     int o = query(1,g,1);

115     int t = 0;

116     for(i = 1 ; i <= k ; i++)

117     {

118         int o1 = qq[bi[i].s1];

119         int o2 = q[bi[i].s2];

120         int kk = bi[i].d;

121         if(o)

122         {

123             sd[o]+=(kk-t);

124         }

125         t = kk;

126         update(lo[o1],-pp[o1],1,g,1);

127         update(o2,pp[o1],1,g,1);

128         lo[o1] = o2;

129         o = query(1,g,1);

130     }

131     if(o)

132     sd[o] += m-t;

133     o = 0;

134     for(i = 1; i <= g ; i++)

135     {

136         if(sd[i])

137         {

138             strcpy(ct[o].ss,pq[i]);

139             ct[o].vv = sd[i];

140             o++;

141         }

142     }

143     sort(ct,ct+o,cmp);

144     for(i = 0 ; i < o ; i++)

145     printf("%s %lld\n",ct[i].ss,ct[i].vv);

146     return 0;

147 }
View Code

 

 

你可能感兴趣的:(Lion)